vercel / next.js

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

querystring is not working to alternates canonical and languages in generateMetadata #72810

Open richg0ld opened 1 day ago

richg0ld commented 1 day ago

Link to the code that reproduces this issue

https://github.com/richg0ld/generate-metadata-bug

To Reproduce

In Next.js 15, I am trying to use the query string in generateMetadata's alternates.canonical and alternates.languages, but it is not being applied.

How can I ensure that querystring is not removed?

The sample code is as follows.

export async function generateMetadata(): Promise<Metadata> {
  return {
    metadataBase: new URL('http://localhost:3000'),
    title: t('title'),
    alternates: {
      canonical: '/',
      languages: {
        'x-default': '/',
        en: '/?hl=en_US',
        ko: '/?hl=ko_KR',
      },
    },
  };
}

Current vs. Expected behavior

The expected behavior is as follows,

<link rel="alternate" hreflang="x-default" href="http://localhost:3000">
<link rel="alternate" hreflang="en" href="http://localhost:3000/?hl=en_US">
<link rel="alternate" hreflang="ko" href="http://localhost:3000/?hl=ko_KR">

However, the result is as follows,

<link rel="alternate" hreflang="x-default" href="http://localhost:3000">
<link rel="alternate" hreflang="en" href="http://localhost:3000">
<link rel="alternate" hreflang="ko" href="http://localhost:3000">

Provide environment information

nextjs:15.0.3
reactjs:19.0.0-rc
react-dom:19.0.0-rc

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

Metadata

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

next dev (local), next build (local), next start (local), Other (Deployed)

Additional context

It would be helpful to have an 'other' option for link tags, similar to the 'other' option for handling meta tags. For example, having a method like this would be useful.

...,
other: [
  {
    hreflang: "en",
    rel: "alternate",
    href="/?hl=en_US"
  },
  ...
]

having a method like this would be useful.

richg0ld commented 21 hours ago

@huozhi It’s not exact, but to help find the issue, I’m adding more information that seems related to the content of the link I shared. Based on the comments, it seems that the query handling part is missing.

https://github.com/vercel/next.js/blame/aef9154e06b235268a7cfe3f3a9aaebe79c5da18/packages/next/src/lib/metadata/resolvers/resolve-url.ts#L117