vercel / next.js

The React Framework
https://nextjs.org
MIT License
124.71k stars 26.61k forks source link

Issue with adding "alternates" in the sitemap #66574

Closed thibault-chausson closed 1 month ago

thibault-chausson commented 3 months ago

Link to the code that reproduces this issue

https://github.com/thibault-chausson/bug-sitemap-next

To Reproduce

To reproduce the bug, I copied and pasted the documentation on creating a sitemap with "alternates: language", but during execution with ppm dev or ppm build then ppm start, when I navigated to localhost:3000/sitemap.xml or localhost:3000/sitemap, the sitemap no longer worked correctly. I only got a string returned.

import { 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',
        },
      },
    },
  ]
}

Current vs. Expected behavior

The expected result is :

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

But I get

image

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.5.0: Wed May  1 20:12:58 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6000
  Available memory (MB): 16384
  Available CPU cores: 8
Binaries:
  Node: 20.9.0
  npm: 10.1.0
  Yarn: 1.22.19
  pnpm: 9.0.4
Relevant Packages:
  next: 14.2.3 // Latest available version is detected (14.2.3).
  eslint-config-next: 13.4.9
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.4.5
Next.js Config:
  output: standalone

Which area(s) are affected? (Select all that apply)

Metadata

Which stage(s) are affected? (Select all that apply)

next dev (local), next start (local)

Additional context

No response

darthmaim commented 2 months ago

This just looks like the browser displaying the XML wrong, have you tried to inspect the source?

agreloe commented 2 months ago

I'm having the same issue and when I inspect the sitemap it looks correct, but without the alternates it looks like it should

agreloe commented 2 months ago

You can see the behaviour here: website I checked on Google Search Console and the sitemap's status is Success so I guess it's working even though it's not being displayed like the usual sitemap. But without the alternates, the sitemap is being displayed correctly.

thibault-chausson commented 2 months ago

@damien-schneider You can check this issue, alternate sitemaps seem to be working

ijjk commented 1 month ago

Closing per-above as this is working as expected, it seems some XML viewers just have trouble displaying it properly.

github-actions[bot] commented 3 weeks ago

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.