-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add: produce a sitemap on request and tell in robots.txt we have that
This should improve the speed search engines pick up new pages.
- Loading branch information
Showing
4 changed files
with
107 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import click | ||
import os | ||
import urllib.parse | ||
|
||
from aiohttp import web | ||
from openttd_helpers import click_helper | ||
|
||
from . import page as page_view | ||
from .. import metadata | ||
|
||
FRONTEND_URL = None | ||
|
||
|
||
def view() -> web.Response: | ||
if FRONTEND_URL is None: | ||
raise web.HTTPNotFound | ||
|
||
if page_view.CACHE_PAGE_FOLDER: | ||
cache_filename = f"{page_view.CACHE_PAGE_FOLDER}/sitemap.xml" | ||
else: | ||
cache_filename = None | ||
|
||
# Check if we have this file in cache first. | ||
if cache_filename and os.path.exists(cache_filename): | ||
with open(cache_filename) as fp: | ||
body = fp.read() | ||
else: | ||
body = '<?xml version="1.0" encoding="UTF-8"?>\n' | ||
body += ( | ||
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' | ||
'xmlns:xhtml="http://www.w3.org/1999/xhtml">\n' | ||
) | ||
|
||
for page, page_data in metadata.PAGES.items(): | ||
if page.startswith("Page/"): | ||
page = page[len("Page/") :] | ||
page = urllib.parse.quote(page) | ||
|
||
body += "<url>\n" | ||
body += f"<loc>{FRONTEND_URL}/{page}</loc>\n" | ||
|
||
if len(page_data["translations"]) == 1: | ||
en_page = page_data["translations"][0] | ||
|
||
if len(metadata.TRANSLATIONS[en_page]) > 1: | ||
for translation in metadata.TRANSLATIONS[en_page]: | ||
if translation.startswith("Page/"): | ||
language = translation.split("/")[1] | ||
translation = translation[len("Page/") :] | ||
else: | ||
language = translation.split("/")[1] | ||
|
||
translation = urllib.parse.quote(translation) | ||
body += ( | ||
f'<xhtml:link rel="alternate" hreflang="{language}" ' | ||
f'href="{FRONTEND_URL}/{translation}" />\n' | ||
) | ||
|
||
body += "</url>\n" | ||
|
||
body += "</urlset>\n" | ||
|
||
if cache_filename: | ||
# Store in cache for next time it is requested. | ||
with open(cache_filename, "w") as fp: | ||
fp.write(body) | ||
|
||
return web.Response(body=body, content_type="application/xml") | ||
|
||
|
||
def invalidate_cache() -> None: | ||
cache_filename = f"{page_view.CACHE_PAGE_FOLDER}/sitemap.xml" | ||
|
||
if os.path.exists(cache_filename): | ||
os.unlink(cache_filename) | ||
|
||
|
||
@click_helper.extend | ||
@click.option( | ||
"--frontend-url", | ||
help="URL of the frontend, used for creating absolute links in the sitemap.xml", | ||
) | ||
def click_sitemap(frontend_url): | ||
global FRONTEND_URL | ||
|
||
FRONTEND_URL = frontend_url |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters