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: canonical should allow relative urls #46584

Merged
merged 4 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 4 additions & 4 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 Expand Up @@ -418,7 +418,7 @@ createNextDescribe(
)
await checkMetaPropertyContentPair(browser, 'og:locale', 'en-US')
await checkMetaPropertyContentPair(browser, 'og:type', 'website')
await checkMetaPropertyContentPair(browser, 'og:image', [
await checkMetaPropertyContentPair(browser, 'og:image:url', [
'https://example.com/image.png',
'https://example.com/image2.png',
])
Expand Down Expand Up @@ -464,7 +464,7 @@ createNextDescribe(

it('should pick up opengraph-image and twitter-image as static metadata files', async () => {
const $ = await next.render$('/opengraph/static')
expect($('[property="og:image"]').attr('content')).toMatch(
expect($('[property="og:image:url"]').attr('content')).toMatch(
/https:\/\/example.com\/_next\/static\/media\/metadata\/opengraph-image.\w+.png/
)
expect($('[property="og:image:type"]').attr('content')).toBe(
Expand Down