Next.js Discord

Discord Forum

Running into errors when trying to use localized sitemap

Unanswered
Atlantic herring posted this in #help-forum
Open in Discord
Atlantic herringOP
If I just use the plain sitemap from the Next.js docs, it works perfectly fine
import type { MetadataRoute } from 'next'
 
export default function sitemap(): MetadataRoute.Sitemap {
  return [
    {
      url: 'https://acme.com',
      lastModified: new Date(),
      changeFrequency: 'yearly',
      priority: 1,
    },
    {
      url: 'https://acme.com/about',
      lastModified: new Date(),
      changeFrequency: 'monthly',
      priority: 0.8,
    },
    {
      url: 'https://acme.com/blog',
      lastModified: new Date(),
      changeFrequency: 'weekly',
      priority: 0.5,
    },
  ]
}


This is how it looks in the browser:
<urlset>
<url>
<loc>https://acme.com</loc>
<lastmod>2025-05-21T19:23:14.711Z</lastmod>
<changefreq>yearly</changefreq>
<priority>1</priority>
</url>
<url>
<loc>https://acme.com/about</loc>
<lastmod>2025-05-21T19:23:14.711Z</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>https://acme.com/blog</loc>
<lastmod>2025-05-21T19:23:14.711Z</lastmod>
<changefreq>weekly</changefreq>
<priority>0.5</priority>
</url>
</urlset>


As soon as I implement the localized sitemap with alternates and languages:
import type { MetadataRoute } from 'next'
 
export default function sitemap(): MetadataRoute.Sitemap {
  return [
    {
      url: 'https://acme.com',
      lastModified: new Date(),
      alternates: {
        languages: {
          es: 'https://acme.com/es',
          de: 'https://acme.com/de',
        },
      },
    },
    {
      url: 'https://acme.com/about',
      lastModified: new Date(),
      alternates: {
        languages: {
          es: 'https://acme.com/es/about',
          de: 'https://acme.com/de/about',
        },
      },
    },
    {
      url: 'https://acme.com/blog',
      lastModified: new Date(),
      alternates: {
        languages: {
          es: 'https://acme.com/es/blog',
          de: 'https://acme.com/de/blog',
        },
      },
    },
  ]
}

5 Replies

Atlantic herringOP
It looks like that:
 https://acme.com 2025-05-21T19:25:15.689Z 

https://acme.com/about 2025-05-21T19:25:15.689Z 

https://acme.com/blog 2025-05-21T19:25:15.689Z 


So it’s basically missing the entire XML structure
What’s your nextjs version?
I’ll try it out locally tomorrow
@Anay-208 | Ping in replies What’s your nextjs version?
Atlantic herringOP
15.2.4
@Anay-208 | Ping in replies I’ll try it out locally tomorrow
Atlantic herringOP
Sounds good. Yeah, I’ve been trying a bunch of different ways to fix the error, but I just can’t get it to work the way I want