Skip to content

Commit

Permalink
Replace metadata clone with custom handler in dev (#49343)
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed May 8, 2023
1 parent e6cfc15 commit 5b5c4c3
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 12 deletions.
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

0 comments on commit 5b5c4c3

Please sign in to comment.