The issue at hand
My current task involves generating a sitemap with hreflang links for different languages. Following the guidelines provided in the NextJs documentation (https://nextjs.org/docs/app/api-reference/file-conventions/metadata/sitemap#generate-a-localized-sitemap), I have attempted to create a sitemap with alternate URLs, or hreflangs, as shown below:
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 outcome
The desired result should be an XML structure as follows:
<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, it seems that the MetadaRoute.Sitemap object does not incorporate a definition for alternates. Upon implementing the provided code snippet, or my own version, I am able to retrieve the <loc>
and <lastmod>
properties but the alternates are missing:
type SitemapFile = Array<{
url: string;
lastModified?: string | Date;
changeFrequency?: 'always' | 'hourly' | 'daily' | 'weekly' | 'monthly' | 'yearly' | 'never'; priority?:
number;
}>;
Has anyone encountered and successfully resolved this challenge while generating a sitemap with alternate URLs? My environment includes Next.js version ^14.1.4 and next-intl version ^3.1.0.