Skip to content

Commit

Permalink
Merge pull request #509 from iamvishnusankar/fix/xml-formatting-issue
Browse files Browse the repository at this point in the history
[Fix] Sitemap fails when adding alternate refs.
  • Loading branch information
iamvishnusankar authored Oct 17, 2022
2 parents c034f41 + d978e39 commit 7131cf6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
10 changes: 10 additions & 0 deletions examples/i18n/next-sitemap.config.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 7131cf6

Please sign in to comment.