vercel / next.js

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

Docs: Doc is lying about query params for canonical segments #68272

Open ScreamZ opened 1 month ago

ScreamZ commented 1 month ago

What is the update you wish to see?

https://nextjs.org/docs/app/api-reference/functions/generate-metadata#url-composition

It is written here that

Trailing slashes between metadataBase and metadata fields are normalized.

This works well in most case as defined in example, but with query params not.

{
  "alternate": {
    "canonical": "./?someparams=true"
  }
}

end up with https://someURL.fr/mypage/?someparams=true when I want https://someURL.fr/mypage?someparams=true I guess it consider the next string to be a path segment where it's just query params.

Current workaround (using in my case next-intl)

import { getPathname } from "@modules/navigation";

// ...
 alternates: {
      canonical: `/${locale}/${getPathname({ locale, href: "/search" })}${searchParamsSerializer(params)}`, // Keep params
    },

Is there any context that might help us understand?

When playing with canonical URL, sometimes you might want to exclude some query parameters (in search for example) and only generate a canonical URL with allowed parameters.

If URL contains query params A,B,C

And only A and B have effects on the search, I want to strip C from canonical URL.

Does the docs page already exist? Please link to it.

https://nextjs.org/docs/app/api-reference/functions/generate-metadata#url-composition

samcx commented 1 month ago

@ScreamZ Can you create a minimal :repro: that highlights this issue? :frog-eyes: