Managing canonical links #785
-
I've been going over the docs and found the example of managing canonical links as part of Next's metadata: https://next-intl-docs.vercel.app/docs/routing/navigation#getpathname import {getPathname} from '../navigation';
export async function generateMetadata({params: {locale}}) {
return {
alternates: {
canonical: '/' + locale + getPathname({
locale,
href: {
pathname: '/users/[userId]',
params: {userId: '5'}
}
})
}
};
} Let's look at two problems i see with it:
I suppose, if we had a function similar to Sidenote: According to Google's docs each language is its own canonical URL except in rare, manually managed cases: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 20 replies
-
Note that the example is about this use case: "This can for example be useful to retrieve a canonical link for a page that accepts search params." It's probably a bit easy to miss, I've added a code comment in 38f305d — hope this helps!
This is unfortunately not supported in Next.js, at least not without tradeoffs: vercel/next.js#43704
Yep, canonical links shouldn't be used for connecting localized pages. Instead, alternate links should be used (which is what next-intl does). |
Beta Was this translation helpful? Give feedback.
-
Does this package extend the default metadata function to include the canonical and alternate parameters? export const metadata = {
alternates: {
canonical: 'https://nextjs.org',
languages: {
'en-US': 'https://nextjs.org/en-US',
'de-DE': 'https://nextjs.org/de-DE',
},
media: {
'only screen and (max-width: 600px)': 'https://nextjs.org/mobile',
},
types: {
'application/rss+xml': 'https://nextjs.org/rss',
},
},
} |
Beta Was this translation helpful? Give feedback.
Ah yes, I think we're on the same page now—thank you for explaining!
I've updated #444 to incorporate your feedback, I think this is a push in a good direction where the navigation APIs will become more useful in general.