Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(resolve-metadata): allow for search params in canonical URL #65366

Merged
merged 3 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 78 additions & 0 deletions packages/next/src/lib/metadata/resolve-metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,84 @@ describe('accumulateMetadata', () => {
})
})

it('should support string alternate canonical with search params', async () => {
const metadataItems: MetadataItems = [
[
{
alternates: {
canonical: 'https://localhost:3000/test?foo=bar',
languages: {
'en-US': 'https://example.com/en-US',
'de-DE': 'https://example.com/de-DE',
},
media: {
'only screen and (max-width: 600px)': '/mobile',
},
types: {
'application/rss+xml': 'https://example.com/rss',
},
},
},
null,
],
]
const metadata = await accumulateMetadata(metadataItems)
expect(metadata).toMatchObject({
alternates: {
canonical: { url: 'https://localhost:3000/test?foo=bar' },
languages: {
'en-US': [{ url: 'https://example.com/en-US' }],
'de-DE': [{ url: 'https://example.com/de-DE' }],
},
media: {
'only screen and (max-width: 600px)': [{ url: '/mobile' }],
},
types: {
'application/rss+xml': [{ url: 'https://example.com/rss' }],
},
},
})
})

it('should support URL alternate canonical with search params', async () => {
const metadataItems: MetadataItems = [
[
{
alternates: {
canonical: new URL('https://localhost:3000/test?foo=bar'),
languages: {
'en-US': 'https://example.com/en-US',
'de-DE': 'https://example.com/de-DE',
},
media: {
'only screen and (max-width: 600px)': '/mobile',
},
types: {
'application/rss+xml': 'https://example.com/rss',
},
},
},
null,
],
]
const metadata = await accumulateMetadata(metadataItems)
expect(metadata).toMatchObject({
alternates: {
canonical: { url: 'https://localhost:3000/test?foo=bar' },
languages: {
'en-US': [{ url: 'https://example.com/en-US' }],
'de-DE': [{ url: 'https://example.com/de-DE' }],
},
media: {
'only screen and (max-width: 600px)': [{ url: '/mobile' }],
},
types: {
'application/rss+xml': [{ url: 'https://example.com/rss' }],
},
},
})
})

it('should support alternate descriptors', async () => {
const metadataItems: MetadataItems = [
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ function resolveAlternateUrl(
// If alter native url is an URL instance,
// we treat it as a URL base and resolve with current pathname
if (url instanceof URL) {
url = new URL(metadataContext.pathname, url)
const newUrl = new URL(metadataContext.pathname, url)
url.searchParams.forEach((value, key) =>
newUrl.searchParams.set(key, value)
)
url = newUrl
}
return resolveAbsoluteUrlWithPathname(url, metadataBase, metadataContext)
}
Expand Down