Skip to content

Commit

Permalink
Fix: canonical should allow relative urls (#46584)
Browse files Browse the repository at this point in the history
alternate urls should allow string type for relative paths

## Bug

Fixes #45824

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

---------
  • Loading branch information
huozhi authored Mar 1, 2023
1 parent 5532b6a commit b5f92c4
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 11 deletions.
4 changes: 3 additions & 1 deletion packages/next/src/lib/metadata/resolvers/resolve-basics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ export const resolveAlternates: FieldResolverWithMetadataBase<'alternates'> = (
) => {
if (!alternates) return null
const result: ResolvedAlternateURLs = {
canonical: resolveUrl(alternates.canonical, metadataBase),
canonical: metadataBase
? resolveUrl(alternates.canonical, metadataBase)
: alternates.canonical || null,
languages: null,
media: null,
types: null,
Expand Down
6 changes: 3 additions & 3 deletions packages/next/src/lib/metadata/resolvers/resolve-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ function resolveUrl(
function resolveUrlValuesOfObject(
obj: Record<string, string | URL | null> | null | undefined,
metadataBase: ResolvedMetadata['metadataBase']
): null | Record<string, URL | null> {
): null | Record<string, string | URL | null> {
if (!obj) return null
const result: Record<string, URL | null> = {}
const result: Record<string, URL | string | null> = {}
for (const [key, value] of Object.entries(obj)) {
result[key as keyof typeof obj] = resolveUrl(value, metadataBase)
result[key] = metadataBase ? resolveUrl(value, metadataBase) : value
}
return result
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,12 +434,12 @@ export type AlternateURLs = {
}

export type ResolvedAlternateURLs = {
canonical: null | URL
languages: null | Languages<null | URL>
canonical: null | string | URL
languages: null | Languages<null | string | URL>
media: null | {
[media: string]: null | URL
[media: string]: null | string | URL
}
types: null | {
[types: string]: null | URL
[types: string]: null | string | URL
}
}
2 changes: 1 addition & 1 deletion test/e2e/app-dir/metadata/app/alternate/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const metadata = {
'de-DE': 'https://example.com/de-DE',
},
media: {
'only screen and (max-width: 600px)': 'https://example.com/mobile',
'only screen and (max-width: 600px)': '/mobile',
},
types: {
'application/rss+xml': 'https://example.com/rss',
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/app-dir/metadata/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ createNextDescribe(

it('should support alternate tags', async () => {
const browser = await next.browser('/alternate')
await checkLink(browser, 'canonical', 'https://example.com/')
await checkLink(browser, 'canonical', 'https://example.com')
await checkMeta(
browser,
'en-US',
Expand All @@ -224,7 +224,7 @@ createNextDescribe(
await checkMeta(
browser,
'only screen and (max-width: 600px)',
'https://example.com/mobile',
'/mobile',
'media',
'link',
'href'
Expand Down

0 comments on commit b5f92c4

Please sign in to comment.