From 2520e918099dec69cf20ae5ac4ff9cff059e8d37 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Wed, 1 Mar 2023 00:17:34 +0100 Subject: [PATCH 1/3] Fix: canonical should allow relative urls --- .../next/src/lib/metadata/resolvers/resolve-basics.ts | 4 +++- packages/next/src/lib/metadata/resolvers/resolve-url.ts | 6 +++--- .../next/src/lib/metadata/types/alternative-urls-types.ts | 8 ++++---- test/e2e/app-dir/metadata/app/alternate/page.tsx | 2 +- test/e2e/app-dir/metadata/metadata.test.ts | 6 +++--- 5 files changed, 14 insertions(+), 12 deletions(-) diff --git a/packages/next/src/lib/metadata/resolvers/resolve-basics.ts b/packages/next/src/lib/metadata/resolvers/resolve-basics.ts index 9a7458f7c02c5..9a7a48c68faf0 100644 --- a/packages/next/src/lib/metadata/resolvers/resolve-basics.ts +++ b/packages/next/src/lib/metadata/resolvers/resolve-basics.ts @@ -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, diff --git a/packages/next/src/lib/metadata/resolvers/resolve-url.ts b/packages/next/src/lib/metadata/resolvers/resolve-url.ts index 0f1e8f0f68b19..2236283a5b09c 100644 --- a/packages/next/src/lib/metadata/resolvers/resolve-url.ts +++ b/packages/next/src/lib/metadata/resolvers/resolve-url.ts @@ -33,11 +33,11 @@ function resolveUrl( function resolveUrlValuesOfObject( obj: Record | null | undefined, metadataBase: ResolvedMetadata['metadataBase'] -): null | Record { +): null | Record { if (!obj) return null - const result: Record = {} + const result: Record = {} 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 } diff --git a/packages/next/src/lib/metadata/types/alternative-urls-types.ts b/packages/next/src/lib/metadata/types/alternative-urls-types.ts index 3018c1e46ceb6..1df4cf92ad49b 100644 --- a/packages/next/src/lib/metadata/types/alternative-urls-types.ts +++ b/packages/next/src/lib/metadata/types/alternative-urls-types.ts @@ -434,12 +434,12 @@ export type AlternateURLs = { } export type ResolvedAlternateURLs = { - canonical: null | URL - languages: null | Languages + canonical: null | string | URL + languages: null | Languages media: null | { - [media: string]: null | URL + [media: string]: null | string | URL } types: null | { - [types: string]: null | URL + [types: string]: null | string | URL } } diff --git a/test/e2e/app-dir/metadata/app/alternate/page.tsx b/test/e2e/app-dir/metadata/app/alternate/page.tsx index ac0bd68f88509..d7a6d1f179470 100644 --- a/test/e2e/app-dir/metadata/app/alternate/page.tsx +++ b/test/e2e/app-dir/metadata/app/alternate/page.tsx @@ -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', diff --git a/test/e2e/app-dir/metadata/metadata.test.ts b/test/e2e/app-dir/metadata/metadata.test.ts index 14cc949c5bfb6..8b6779bdd6ff4 100644 --- a/test/e2e/app-dir/metadata/metadata.test.ts +++ b/test/e2e/app-dir/metadata/metadata.test.ts @@ -224,7 +224,7 @@ createNextDescribe( await checkMeta( browser, 'only screen and (max-width: 600px)', - 'https://example.com/mobile', + '/mobile', 'media', 'link', 'href' @@ -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', ]) @@ -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( From 745763aaba2ad111a7dedc34164a6dded9f69ad2 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Wed, 1 Mar 2023 00:29:12 +0100 Subject: [PATCH 2/3] fix test --- test/e2e/app-dir/metadata/metadata.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/e2e/app-dir/metadata/metadata.test.ts b/test/e2e/app-dir/metadata/metadata.test.ts index 8b6779bdd6ff4..45533594f154d 100644 --- a/test/e2e/app-dir/metadata/metadata.test.ts +++ b/test/e2e/app-dir/metadata/metadata.test.ts @@ -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', From 55e045fe96d3fc2620a24d2b5c0f5bb20c6431b5 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Wed, 1 Mar 2023 00:50:34 +0100 Subject: [PATCH 3/3] fix tests --- test/e2e/app-dir/metadata/metadata.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/e2e/app-dir/metadata/metadata.test.ts b/test/e2e/app-dir/metadata/metadata.test.ts index 45533594f154d..e43e36cd4053b 100644 --- a/test/e2e/app-dir/metadata/metadata.test.ts +++ b/test/e2e/app-dir/metadata/metadata.test.ts @@ -418,7 +418,7 @@ createNextDescribe( ) await checkMetaPropertyContentPair(browser, 'og:locale', 'en-US') await checkMetaPropertyContentPair(browser, 'og:type', 'website') - await checkMetaPropertyContentPair(browser, 'og:image:url', [ + await checkMetaPropertyContentPair(browser, 'og:image', [ 'https://example.com/image.png', 'https://example.com/image2.png', ]) @@ -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:url"]').attr('content')).toMatch( + expect($('[property="og:image"]').attr('content')).toMatch( /https:\/\/example.com\/_next\/static\/media\/metadata\/opengraph-image.\w+.png/ ) expect($('[property="og:image:type"]').attr('content')).toBe(