From 1385fcccc10ef2f9fbcb6520e03ccee9523c0595 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Tue, 23 Apr 2024 21:19:39 +0200 Subject: [PATCH 1/7] Only apply metadata manifest credentials for preview deployment --- packages/next/src/lib/metadata/generate/basic.tsx | 4 +++- test/e2e/app-dir/metadata/metadata.test.ts | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/next/src/lib/metadata/generate/basic.tsx b/packages/next/src/lib/metadata/generate/basic.tsx index c2f5460d62e0a..6f01b294fcd5b 100644 --- a/packages/next/src/lib/metadata/generate/basic.tsx +++ b/packages/next/src/lib/metadata/generate/basic.tsx @@ -64,7 +64,9 @@ export function BasicMeta({ metadata }: { metadata: ResolvedMetadata }) { ) : null, Meta({ name: 'generator', content: metadata.generator }), diff --git a/test/e2e/app-dir/metadata/metadata.test.ts b/test/e2e/app-dir/metadata/metadata.test.ts index 6f776ff41e525..0b9e1db6519b7 100644 --- a/test/e2e/app-dir/metadata/metadata.test.ts +++ b/test/e2e/app-dir/metadata/metadata.test.ts @@ -231,7 +231,7 @@ createNextDescribe( // Manifest link should have crossOrigin attribute await matchDom('link', 'rel="manifest"', { href: '/api/manifest', - crossOrigin: 'use-credentials', + crossOrigin: isNextDeploy ? 'use-credentials' : undefined, }) await matchDom('meta', 'name="theme-color"', { @@ -266,7 +266,7 @@ createNextDescribe( // Manifest link should have crossOrigin attribute await matchDom('link', 'rel="manifest"', { href: '/api/manifest', - crossOrigin: 'use-credentials', + crossOrigin: isNextDeploy ? 'use-credentials' : undefined, }) }) From 908ae208cf87d380cd33f0475ba3d3c6c7ae4faf Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Tue, 23 Apr 2024 21:29:53 +0200 Subject: [PATCH 2/7] compare domain --- packages/next/src/lib/metadata/generate/basic.tsx | 14 +++++++++++++- packages/next/src/lib/metadata/generate/utils.ts | 13 ++++++++++++- packages/next/src/lib/metadata/metadata.tsx | 2 +- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/packages/next/src/lib/metadata/generate/basic.tsx b/packages/next/src/lib/metadata/generate/basic.tsx index 6f01b294fcd5b..d27620a676bb1 100644 --- a/packages/next/src/lib/metadata/generate/basic.tsx +++ b/packages/next/src/lib/metadata/generate/basic.tsx @@ -8,6 +8,7 @@ import type { ViewportLayout } from '../types/extra-types' import React from 'react' import { Meta, MetaFilter, MultiMeta } from './meta' import { ViewportMetaKeys } from '../constants' +import { getOrigin } from './utils' // convert viewport object to string for viewport meta tag function resolveViewportLayout(viewport: Viewport) { @@ -45,6 +46,11 @@ export function ViewportMeta({ viewport }: { viewport: ResolvedViewport }) { } export function BasicMeta({ metadata }: { metadata: ResolvedMetadata }) { + const { metadataBase } = metadata + const manifestOrigin = metadata.manifest + ? getOrigin(metadata.manifest) + : undefined + return MetaFilter([ , metadata.title !== null && metadata.title.absolute ? ( @@ -64,8 +70,14 @@ export function BasicMeta({ metadata }: { metadata: ResolvedMetadata }) { ) : null, diff --git a/packages/next/src/lib/metadata/generate/utils.ts b/packages/next/src/lib/metadata/generate/utils.ts index e904e8595b049..1c3e07fcf1997 100644 --- a/packages/next/src/lib/metadata/generate/utils.ts +++ b/packages/next/src/lib/metadata/generate/utils.ts @@ -14,4 +14,15 @@ function resolveAsArrayOrUndefined( return resolveArray(value) as any } -export { resolveAsArrayOrUndefined, resolveArray } +function getOrigin(url: string | URL): string { + if (typeof url === 'string') { + try { + url = new URL(url) + } catch { + url = new URL('http://n') + } + } + return url.origin +} + +export { resolveAsArrayOrUndefined, resolveArray, getOrigin } diff --git a/packages/next/src/lib/metadata/metadata.tsx b/packages/next/src/lib/metadata/metadata.tsx index 1be2eb2e20717..89812d77516ac 100644 --- a/packages/next/src/lib/metadata/metadata.tsx +++ b/packages/next/src/lib/metadata/metadata.tsx @@ -120,7 +120,7 @@ export function createMetadataComponents({ const elements = MetaFilter([ ViewportMeta({ viewport: viewport }), - BasicMeta({ metadata }), + BasicMeta({ metadata, metadataBase: resolvedMetadata.metadataBase }), AlternatesMetadata({ alternates: metadata.alternates }), ItunesMeta({ itunes: metadata.itunes }), FormatDetectionMeta({ formatDetection: metadata.formatDetection }), From a559c1c8ee191dd037ed8f5afe0b99cb5404fc8c Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Tue, 23 Apr 2024 21:33:36 +0200 Subject: [PATCH 3/7] revert --- packages/next/src/lib/metadata/metadata.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/next/src/lib/metadata/metadata.tsx b/packages/next/src/lib/metadata/metadata.tsx index 89812d77516ac..1be2eb2e20717 100644 --- a/packages/next/src/lib/metadata/metadata.tsx +++ b/packages/next/src/lib/metadata/metadata.tsx @@ -120,7 +120,7 @@ export function createMetadataComponents({ const elements = MetaFilter([ ViewportMeta({ viewport: viewport }), - BasicMeta({ metadata, metadataBase: resolvedMetadata.metadataBase }), + BasicMeta({ metadata }), AlternatesMetadata({ alternates: metadata.alternates }), ItunesMeta({ itunes: metadata.itunes }), FormatDetectionMeta({ formatDetection: metadata.formatDetection }), From 46a7a04abfd3b13239af9d48497c7a7cf79da558 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Tue, 23 Apr 2024 21:43:25 +0200 Subject: [PATCH 4/7] update test --- 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 0b9e1db6519b7..0f82f61d290d5 100644 --- a/test/e2e/app-dir/metadata/metadata.test.ts +++ b/test/e2e/app-dir/metadata/metadata.test.ts @@ -231,7 +231,7 @@ createNextDescribe( // Manifest link should have crossOrigin attribute await matchDom('link', 'rel="manifest"', { href: '/api/manifest', - crossOrigin: isNextDeploy ? 'use-credentials' : undefined, + crossOrigin: isNextDeploy ? 'use-credentials' : null, }) await matchDom('meta', 'name="theme-color"', { @@ -266,7 +266,7 @@ createNextDescribe( // Manifest link should have crossOrigin attribute await matchDom('link', 'rel="manifest"', { href: '/api/manifest', - crossOrigin: isNextDeploy ? 'use-credentials' : undefined, + crossOrigin: isNextDeploy ? 'use-credentials' : null, }) }) From ac3496fa0fc76d43681137729127db44196b7a00 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Tue, 30 Apr 2024 20:44:22 +0200 Subject: [PATCH 5/7] tweak --- packages/next/src/lib/metadata/generate/basic.tsx | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/next/src/lib/metadata/generate/basic.tsx b/packages/next/src/lib/metadata/generate/basic.tsx index d27620a676bb1..554a2f0df03f8 100644 --- a/packages/next/src/lib/metadata/generate/basic.tsx +++ b/packages/next/src/lib/metadata/generate/basic.tsx @@ -73,9 +73,7 @@ export function BasicMeta({ metadata }: { metadata: ResolvedMetadata }) { // If it's same origin, and it's a preview deployment, // including credentials for manifest request. crossOrigin={ - metadataBase && - metadataBase.origin === manifestOrigin && - process.env.VERCEL_ENV === 'preview' + !manifestOrigin && process.env.VERCEL_ENV === 'preview' ? 'use-credentials' : undefined } From 6603fa5bae0de419b137d4ee824d832132c441a8 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Tue, 30 Apr 2024 20:59:26 +0200 Subject: [PATCH 6/7] fix origin util --- packages/next/src/lib/metadata/generate/utils.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/next/src/lib/metadata/generate/utils.ts b/packages/next/src/lib/metadata/generate/utils.ts index 1c3e07fcf1997..dd12d3a7a1efd 100644 --- a/packages/next/src/lib/metadata/generate/utils.ts +++ b/packages/next/src/lib/metadata/generate/utils.ts @@ -14,15 +14,15 @@ function resolveAsArrayOrUndefined( return resolveArray(value) as any } -function getOrigin(url: string | URL): string { +function getOrigin(url: string | URL): string | undefined { + let origin = undefined if (typeof url === 'string') { try { url = new URL(url) - } catch { - url = new URL('http://n') - } + origin = url.origin + } catch {} } - return url.origin + return origin } export { resolveAsArrayOrUndefined, resolveArray, getOrigin } From 101764de96fbf84073130573eee3312597134abd Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Tue, 30 Apr 2024 21:06:15 +0200 Subject: [PATCH 7/7] fix lint --- packages/next/src/lib/metadata/generate/basic.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/next/src/lib/metadata/generate/basic.tsx b/packages/next/src/lib/metadata/generate/basic.tsx index 554a2f0df03f8..1d9dfb40200d6 100644 --- a/packages/next/src/lib/metadata/generate/basic.tsx +++ b/packages/next/src/lib/metadata/generate/basic.tsx @@ -46,7 +46,6 @@ export function ViewportMeta({ viewport }: { viewport: ResolvedViewport }) { } export function BasicMeta({ metadata }: { metadata: ResolvedMetadata }) { - const { metadataBase } = metadata const manifestOrigin = metadata.manifest ? getOrigin(metadata.manifest) : undefined