agreloe
agreloe

Reputation: 61

Adding alternates to NextJS sitemap causes error

This is my sitemap.ts file which I took from Next documentation: https://nextjs.org/docs/app/api-reference/file-conventions/metadata/sitemap#generate-a-localized-sitemap

import { MetadataRoute } from 'next'

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

but when I visit the url/sitemap.xml it returns this:

https://acme.com 2024-06-12T09:41:57.545Z

Is there any way to fix this? Thank you!

When I remove the "alternates" snippet and keep the rest, the sitemap works as expected.

Upvotes: 6

Views: 890

Answers (1)

Y H R
Y H R

Reputation: 735

I have been experiencing this same problem on several browsers when visiting /sitemap.xml

Downloading the sitemap.xml page then opening it as a .txt file shows the correct format

According to this post the sitemap is properly generated (and valid if run through a validator), but just poorly displayed

Proof: https://pysubs.vercel.app/sitemap.xml (shows a bad format)

Yet is valid:

enter image description here

Upvotes: 2

Related Questions