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

Replace metadata clone with custom handler in dev #49343

Merged
merged 5 commits into from
May 8, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/next/src/lib/metadata/clone-metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { ResolvedMetadata } from './types/metadata-interface'

const TYPE_URL = '__METADATA_URL'

function replacer(_key: string, val: any) {
// clone URL as string but recover it as URL
if (val instanceof URL) {
return { _type: TYPE_URL, value: val.href }
}
return val
}

function reviver(_key: string, val: any) {
if (typeof val === 'object' && val !== null && val._type === TYPE_URL) {
return new URL(val.value)
}
return val
}

export function cloneMetadata(metadata: ResolvedMetadata): ResolvedMetadata {
const jsonString = JSON.stringify(metadata, replacer)
return JSON.parse(jsonString, reviver)
}
4 changes: 1 addition & 3 deletions packages/next/src/lib/metadata/resolve-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,7 @@ export async function accumulateMetadata(
const currentResolvedMetadata: ResolvedMetadata =
process.env.NODE_ENV === 'development'
? Object.freeze(
require('next/dist/compiled/@edge-runtime/primitives/structured-clone').structuredClone(
resolvedMetadata
)
require('./clone-metadata').cloneMetadata(resolvedMetadata)
)
: resolvedMetadata

Expand Down
14 changes: 14 additions & 0 deletions test/e2e/app-dir/metadata/app/opengraph/article/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default function Layout({ children }) {
return children
}

export const metadata = {
openGraph: {
title: 'Layout open graph title',
description: 'My custom description',
type: 'article',
publishedTime: '2023-01-01T00:00:00.000Z',
authors: ['author1', 'author2', 'author3'],
images: new URL('https://example.com/og-image.jpg'),
},
}
18 changes: 10 additions & 8 deletions test/e2e/app-dir/metadata/app/opengraph/article/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ export default function Page() {
return 'opengraph-article'
}

export const metadata = {
openGraph: {
title: 'My custom title',
description: 'My custom description',
type: 'article',
publishedTime: '2023-01-01T00:00:00.000Z',
authors: ['author1', 'author2', 'author3'],
},
export async function generateMetadata(_props, parentResolvingMetadata) {
const parentMetadata = await parentResolvingMetadata
return {
openGraph: {
...parentMetadata.openGraph,
title: `My custom title | ${parentMetadata.openGraph.title.absolute}`,
// merging parent images URL instance should work
images: [...parentMetadata.openGraph.images],
},
}
}
3 changes: 2 additions & 1 deletion test/e2e/app-dir/metadata/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,10 @@ createNextDescribe(
const browser = await next.browser('/opengraph/article')
const matchMultiDom = createMultiDomMatcher(browser)
await matchMultiDom('meta', 'property', 'content', {
'og:title': 'My custom title',
'og:title': 'My custom title | Layout open graph title',
'og:description': 'My custom description',
'og:type': 'article',
'og:image': 'https://example.com/og-image.jpg',
'article:published_time': '2023-01-01T00:00:00.000Z',
'article:author': ['author1', 'author2', 'author3'],
})
Expand Down