next-sitemap
next-sitemap copied to clipboard
Sitemap fails when adding alternate refs.
Describe the bug
When adding alternate refs the sitemap returns "broken" and with an error message in console saying: Uncaught TypeError: Cannot read properties of null (reading 'childNodes')
To Reproduce Install as documentation stated and add `const fields = [ { loc: "/additional-page-2", changefreq: "yearly", priority: 0.7, lastmod: new Date().toISOString(),
// acts only on '/additional-page-2'
alternateRefs: [
{
href: "https://es.example.com",
hreflang: "es",
},
{
href: "https://fr.example.com",
hreflang: "fr",
},
],
},
];`
Expected behavior Show sitemap with alternate refs (xhtml:link ref="alternate") IT seems like the xmlns props is wrong. See https://shop.change.com/sitemap.xml for a working version.
With alternate refs:

Without alternate refs:

Additional context "next": "^12.2.5", "next-sitemap": "^3.1.21",
I am suspecting a bug from the browser. Similar to that: https://stackoverflow.com/questions/60564479/sitemap-xml-with-xhtmllink-rel-alternate-hreflang-href-for-multilang
I tried on my end and I have the exact same thing as you. Using a chrome extension to view XML was able to display correctly formated XML though
I am facing a similar issue with the pretty print of xml files in the browser. here https://bugs.chromium.org/p/chromium/issues/detail?id=580033 it says that:
"the alternate ref declaration puts the parsers to the XHTML mode and handles the document as an XHTML document. The rendering results makes sense if browsers parsed the document as an XHTML."
even though it is not a major problem, any suggestions for a temporary work around.
According to this thread https://stackoverflow.com/questions/45274063/xml-format-in-browsers-when-added-xhtml if change xmlns:* links from http to https everything works fine. I tried modifying the sitemap.xml file and it works as expected. However, I couldn't find a way to modify sitemap files from configs. Any suggestions?
@olehkhalin Thanks for pointing this out. Fixed: #509
Cool, thanks 👍
@iamvishnusankar for some reason this fix is not included in the most recent (for the current moment) release v3.1.32
Same as @Serhii-the-Dev mentioned.
The fix works in v3.1.29, but fail again after v3.1.30
Hi @Kamigami55
I applied the schema manually (by replacing it with a regex inside a generated XML body), however it turned out it was an incorrect approach: Google Search Console is labeling such a sitemap as one containing invalid namespaces. So despite it's not rendering visually correct in a browser, I did a rollback to http schema in order to make it work with SEO tools.
The bottom line is: correct namespace for the sitemap file should use http schema as defined in the protocol.
@iamvishnusankar Also failing for me after version 3.1.30