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

Only apply metadata manifest credentials for preview deployment #64940

Merged
merged 9 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 12 additions & 1 deletion packages/next/src/lib/metadata/generate/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -45,6 +46,10 @@ export function ViewportMeta({ viewport }: { viewport: ResolvedViewport }) {
}

export function BasicMeta({ metadata }: { metadata: ResolvedMetadata }) {
const manifestOrigin = metadata.manifest
? getOrigin(metadata.manifest)
: undefined

return MetaFilter([
<meta charSet="utf-8" />,
metadata.title !== null && metadata.title.absolute ? (
Expand All @@ -64,7 +69,13 @@ export function BasicMeta({ metadata }: { metadata: ResolvedMetadata }) {
<link
rel="manifest"
href={metadata.manifest.toString()}
crossOrigin="use-credentials"
// If it's same origin, and it's a preview deployment,
// including credentials for manifest request.
crossOrigin={
!manifestOrigin && process.env.VERCEL_ENV === 'preview'
? 'use-credentials'
: undefined
}
/>
) : null,
Meta({ name: 'generator', content: metadata.generator }),
Expand Down
13 changes: 12 additions & 1 deletion packages/next/src/lib/metadata/generate/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@ function resolveAsArrayOrUndefined<T>(
return resolveArray(value) as any
}

export { resolveAsArrayOrUndefined, resolveArray }
function getOrigin(url: string | URL): string | undefined {
let origin = undefined
if (typeof url === 'string') {
try {
url = new URL(url)
origin = url.origin
} catch {}
}
return origin
}

export { resolveAsArrayOrUndefined, resolveArray, getOrigin }
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 @@ -228,7 +228,7 @@ describe('app dir - metadata', () => {
// Manifest link should have crossOrigin attribute
await matchDom('link', 'rel="manifest"', {
href: '/api/manifest',
crossOrigin: 'use-credentials',
crossOrigin: isNextDeploy ? 'use-credentials' : null,
})

await matchDom('meta', 'name="theme-color"', {
Expand Down Expand Up @@ -263,7 +263,7 @@ describe('app dir - metadata', () => {
// Manifest link should have crossOrigin attribute
await matchDom('link', 'rel="manifest"', {
href: '/api/manifest',
crossOrigin: 'use-credentials',
crossOrigin: isNextDeploy ? 'use-credentials' : null,
})
})

Expand Down
Loading