Skip to content

Commit 90d129b

Browse files
committed
Emit pages.xml — a simplified fallback sitemap
mkdocs-static-i18n adds xhtml:link hreflang alternates to sitemap.xml. Some Google Search Console fetches fail on that variant (known issue with GSC + GitHub Pages + bilingual sitemaps), and once GSC caches the failure it doesn't re-test for days. pages.xml is a flat urlset with the same 40 URLs, no alternates, no namespaces beyond the base sitemap protocol. Lets us re-submit to GSC under a fresh name and bypass the cached failure.
1 parent be41b54 commit 90d129b

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

‎mkdocs_hooks.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from __future__ import annotations
44

55
import logging
6+
import xml.etree.ElementTree as ET
7+
from pathlib import Path
68
from typing import Any
79

810

@@ -14,3 +16,45 @@ def on_config(config: Any, **kwargs: Any) -> Any:
1416
# Silence only that logger; every other warning still fails strict mode.
1517
logging.getLogger("mkdocs.plugins.mkdocs_autorefs").setLevel(logging.ERROR)
1618
return config
19+
20+
21+
def on_post_build(config: Any, **kwargs: Any) -> None:
22+
"""Emit a simplified fallback sitemap at ``pages.xml``.
23+
24+
The standard ``sitemap.xml`` generated by mkdocs-static-i18n includes
25+
``xhtml:link`` alternates for the bilingual setup. Google Search Console
26+
sometimes fails to fetch that sitemap on GitHub Pages (cached failure,
27+
hreflang quirks). This hook produces a second, plain ``urlset`` sitemap
28+
under a different filename so the GSC can be re-submitted without
29+
hitting its failure cache.
30+
"""
31+
site_dir = Path(config["site_dir"])
32+
src = site_dir / "sitemap.xml"
33+
if not src.exists():
34+
return
35+
36+
ns = {"s": "http://www.sitemaps.org/schemas/sitemap/0.9"}
37+
tree = ET.parse(src)
38+
root = tree.getroot()
39+
40+
lines = [
41+
'<?xml version="1.0" encoding="UTF-8"?>',
42+
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
43+
]
44+
for url in root.findall("s:url", ns):
45+
loc = url.find("s:loc", ns)
46+
lastmod = url.find("s:lastmod", ns)
47+
changefreq = url.find("s:changefreq", ns)
48+
if loc is None or loc.text is None:
49+
continue
50+
lines.append(" <url>")
51+
lines.append(f" <loc>{loc.text}</loc>")
52+
if lastmod is not None and lastmod.text:
53+
lines.append(f" <lastmod>{lastmod.text}</lastmod>")
54+
if changefreq is not None and changefreq.text:
55+
lines.append(f" <changefreq>{changefreq.text}</changefreq>")
56+
lines.append(" </url>")
57+
lines.append("</urlset>")
58+
59+
out = site_dir / "pages.xml"
60+
out.write_text("\n".join(lines) + "\n", encoding="utf-8")

0 commit comments

Comments
 (0)