Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Sitemap fails when adding alternate refs. #509

Merged
merged 4 commits into from Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/i18n/next-sitemap.config.js
Expand Up @@ -10,6 +10,16 @@ const config = {
'https://example.com/my-custom-sitemap-3.xml',
],
},
alternateRefs: [
{
href: 'https://es.example.com',
hreflang: 'es',
},
{
href: 'https://fr.example.com',
hreflang: 'fr',
},
],
}

export default config
Expand Up @@ -30,7 +30,7 @@ describe('SitemapBuilder', () => {
// Expect the generated sitemap to match snapshot.
expect(content).toMatchInlineSnapshot(`
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>
<urlset xmlns=\\"http://www.sitemaps.org/schemas/sitemap/0.9\\" xmlns:news=\\"http://www.google.com/schemas/sitemap-news/0.9\\" xmlns:xhtml=\\"http://www.w3.org/1999/xhtml\\" xmlns:mobile=\\"http://www.google.com/schemas/sitemap-mobile/1.0\\" xmlns:image=\\"http://www.google.com/schemas/sitemap-image/1.1\\" xmlns:video=\\"http://www.google.com/schemas/sitemap-video/1.1\\">
<urlset xmlns=\\"https://www.sitemaps.org/schemas/sitemap/0.9\\" xmlns:news=\\"https://www.google.com/schemas/sitemap-news/0.9\\" xmlns:xhtml=\\"https://www.w3.org/1999/xhtml\\" xmlns:mobile=\\"https://www.google.com/schemas/sitemap-mobile/1.0\\" xmlns:image=\\"https://www.google.com/schemas/sitemap-image/1.1\\" xmlns:video=\\"https://www.google.com/schemas/sitemap-video/1.1\\">
<url><loc>https://example.com</loc></url>
<url><loc>https://example.com</loc><lastmod>some-value</lastmod><xhtml:link rel=\\"alternate\\" hreflang=\\"en\\" href=\\"https://example.com/en\\"/><xhtml:link rel=\\"alternate\\" hreflang=\\"fr\\" href=\\"https://example.com/fr\\"/></url>
</urlset>"
Expand Down
4 changes: 2 additions & 2 deletions packages/next-sitemap/src/builders/sitemap-builder.ts
Expand Up @@ -11,7 +11,7 @@ export class SitemapBuilder {
* @returns
*/
withXMLTemplate(content: string): string {
return `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">\n${content}</urlset>`
return `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="https://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="https://www.w3.org/1999/xhtml" xmlns:mobile="https://www.google.com/schemas/sitemap-mobile/1.0" xmlns:image="https://www.google.com/schemas/sitemap-image/1.1" xmlns:video="https://www.google.com/schemas/sitemap-video/1.1">\n${content}</urlset>`
}

/**
Expand All @@ -22,7 +22,7 @@ export class SitemapBuilder {
buildSitemapIndexXml(allSitemaps: string[]) {
return [
'<?xml version="1.0" encoding="UTF-8"?>',
'<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
'<sitemapindex xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">',
...(allSitemaps?.map((x) => `<sitemap><loc>${x}</loc></sitemap>`) ?? []),
'</sitemapindex>',
].join('\n')
Expand Down