-
Goals
Non-GoalsBackgroundThis feature is essential for websites in multiple languages. Although we can generate the sitemap manually would we good to also add this new functionality to the NextJs sitemap generator ProposalAccording to the Google documentation we need to generate a sitemap with the following tag for each language: <xhtml:link rel="alternate" hreflang="es" href="https://www.example.com/es/page.html"/> I think we can follow the same approach that is already available in the Metadata object so the following code // app/sitemap.xml
export default function sitemap(): MetadataRoute.Sitemap {
return [
{
url: 'https://www.example.com/page.html',
alternates: {
languages: {
es: 'https://www.example.com/es/page.html',
de: 'https://www.example.com/de/page.html'
}
},
lastModified: new Date(),
},
]
} Should generate the following output <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url>
<loc>https://www.example.com/page.html</loc>
<xhtml:link
rel="alternate"
hreflang="es"
href="https://www.example.com/es/page.html"/>
<xhtml:link
rel="alternate"
hreflang="de"
href="https://www.example.com/de/page.html"/>
</url>
</urlset> In the future we could use the same |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 3 replies
-
Imho this is very sensible and should be added. |
Beta Was this translation helpful? Give feedback.
-
when will it be added? |
Beta Was this translation helpful? Give feedback.
-
Also need this! |
Beta Was this translation helpful? Give feedback.
-
This is already described in the documentation, but it doesn't work in the version 14.1.4 |
Beta Was this translation helpful? Give feedback.
-
Looks like it was just implemented e4cd547 |
Beta Was this translation helpful? Give feedback.
-
Still the issue is persist, please update this feature ASAP.! |
Beta Was this translation helpful? Give feedback.
-
For everyone who is looking for a temporary solution: https://www.reddit.com/r/nextjs/comments/161cbms/creating_a_sitemap_with_translated_alternate/ Worked like a charm for me |
Beta Was this translation helpful? Give feedback.
-
I must be missing something, but the file in your example does not render correct html, since the link seems to be missing from the http://www.w3.org/1999/xhtml namespace. Changing it to http://www.w3.org/TR/xhtml11/xhtml11_schema.html however works. Does not render in a brower and is not recognied by google search console: Renders in a browser and is recognized by google search console: This creates a problem, since next 14.2 prints out "http://www.w3.org/1999/xhtml" from sitemap.ts. |
Beta Was this translation helpful? Give feedback.
-
This is available on the Next.js 15 canary release, which will be stable soon. |
Beta Was this translation helpful? Give feedback.
This is available on the Next.js 15 canary release, which will be stable soon.
https://rc.nextjs.org/docs/app/api-reference/file-conventions/metadata/sitemap#generate-a-localized-sitemap