Unverified Commit 5b5c4c32 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Replace metadata clone with custom handler in dev (#49343)

parent e6cfc155
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)
}
...@@ -408,9 +408,7 @@ export async function accumulateMetadata( ...@@ -408,9 +408,7 @@ export async function accumulateMetadata(
const currentResolvedMetadata: ResolvedMetadata = const currentResolvedMetadata: ResolvedMetadata =
process.env.NODE_ENV === 'development' process.env.NODE_ENV === 'development'
? Object.freeze( ? Object.freeze(
require('next/dist/compiled/@edge-runtime/primitives/structured-clone').structuredClone( require('./clone-metadata').cloneMetadata(resolvedMetadata)
resolvedMetadata
)
) )
: resolvedMetadata : resolvedMetadata
......
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'),
},
}
...@@ -2,12 +2,14 @@ export default function Page() { ...@@ -2,12 +2,14 @@ export default function Page() {
return 'opengraph-article' return 'opengraph-article'
} }
export const metadata = { export async function generateMetadata(_props, parentResolvingMetadata) {
openGraph: { const parentMetadata = await parentResolvingMetadata
title: 'My custom title', return {
description: 'My custom description', openGraph: {
type: 'article', ...parentMetadata.openGraph,
publishedTime: '2023-01-01T00:00:00.000Z', title: `My custom title | ${parentMetadata.openGraph.title.absolute}`,
authors: ['author1', 'author2', 'author3'], // merging parent images URL instance should work
}, images: [...parentMetadata.openGraph.images],
},
}
} }
...@@ -469,9 +469,10 @@ createNextDescribe( ...@@ -469,9 +469,10 @@ createNextDescribe(
const browser = await next.browser('/opengraph/article') const browser = await next.browser('/opengraph/article')
const matchMultiDom = createMultiDomMatcher(browser) const matchMultiDom = createMultiDomMatcher(browser)
await matchMultiDom('meta', 'property', 'content', { 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:description': 'My custom description',
'og:type': 'article', 'og:type': 'article',
'og:image': 'https://example.com/og-image.jpg',
'article:published_time': '2023-01-01T00:00:00.000Z', 'article:published_time': '2023-01-01T00:00:00.000Z',
'article:author': ['author1', 'author2', 'author3'], 'article:author': ['author1', 'author2', 'author3'],
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment