Paul McCarthy
Paul McCarthy

Reputation: 11

NextJs MetadataRoute.Sitemap does not contain definition for alternates

The problem

I'm trying to generate a sitemap with hreflang links for alternative languages. The nextJs documentation (https://nextjs.org/docs/app/api-reference/file-conventions/metadata/sitemap#generate-a-localized-sitemap) advises to create a sitemap with alternate urls, ie hreflangs, like this:

import { MetadataRoute } from 'next';

export default function sitemap(): MetadataRoute.Sitemap {
return [
{
url: 'https://example.com',
lastModified: new Date(),
alternates: {
languages: {
es: 'https://example.com/es',
de: 'https://example.com/de',
},
},
},...]}

Expected results

This should return the following xml:

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">   
<url>    
 <loc>https://example.com</loc>     
<xhtml:link  rel="alternate" hreflang="es" href="https://example.com/es"/>     
<xhtml:link  rel="alternate" hreflang="de" href="https://example.com/de"/>     
<lastmod>2023-04-06T15:02:24.021Z</lastmod>   
</url>

However, the MetadaRoute.Sitemap object contains no definition of alternates. When I try the above code, or my own, I get all the <loc> and <lastmod> properties but not the alternates:

type SitemapFile = Array<{     
url: string;     
lastModified?: string | Date;     
changeFrequency?: 'always' | 'hourly' | 'daily' | 'weekly' | 'monthly' | 'yearly' | 'never';     priority?: 
number; 
}>;

Has anyone successfully created a sitemap with alternate urls like this? I am using next at ^14.1.4 and next-intl at ^3.1.0.

Upvotes: 1

Views: 60

Answers (1)

Y H R
Y H R

Reputation: 735

I have been having this issue as well. Even thought the sitemap doesn't show properly on one's browser, it is actually valid.

Proof and details here: https://stackoverflow.com/a/78965707/9902344

Upvotes: 0

Related Questions