diff --git a/packages/next/src/build/webpack/plugins/flight-types-plugin.ts b/packages/next/src/build/webpack/plugins/flight-types-plugin.ts index 50ba0bb8dcc66..c530248ac0260 100644 --- a/packages/next/src/build/webpack/plugins/flight-types-plugin.ts +++ b/packages/next/src/build/webpack/plugins/flight-types-plugin.ts @@ -66,7 +66,7 @@ interface IEntry { : '' } metadata?: any - generateMetadata?: (props: PageProps, parent: ResolvingMetadata) => Promise + generateMetadata?: (props: PageProps, parent: ResolvingMetadata) => any | Promise } // ============= diff --git a/packages/next/src/lib/metadata/resolve-metadata.ts b/packages/next/src/lib/metadata/resolve-metadata.ts index 0f74d9696f164..734f5d91819d1 100644 --- a/packages/next/src/lib/metadata/resolve-metadata.ts +++ b/packages/next/src/lib/metadata/resolve-metadata.ts @@ -328,7 +328,9 @@ function merge( } } -type MetadataResolver = (_parent: ResolvingMetadata) => Promise +type MetadataResolver = ( + _parent: ResolvingMetadata +) => Metadata | Promise export type MetadataItems = [ Metadata | MetadataResolver | null, Metadata | null @@ -410,10 +412,11 @@ export async function accumulateMetadata( for (const item of metadataItems) { const [metadataExport, staticFilesMetadata] = item - const layerMetadataPromise = + const layerMetadataPromise = Promise.resolve( typeof metadataExport === 'function' ? metadataExport(parentPromise) - : Promise.resolve(metadataExport) + : metadataExport + ) parentPromise = parentPromise.then((resolved) => { return layerMetadataPromise.then((exportedMetadata) => { diff --git a/test/e2e/app-dir/metadata/app/basic/sync-generate-metadata/page.tsx b/test/e2e/app-dir/metadata/app/basic/sync-generate-metadata/page.tsx new file mode 100644 index 0000000000000..6d6d370de9a38 --- /dev/null +++ b/test/e2e/app-dir/metadata/app/basic/sync-generate-metadata/page.tsx @@ -0,0 +1,9 @@ +export default function Page() { + return 'synchronous generateMetadata' +} + +export function generateMetadata() { + return { + title: 'synchronous generateMetadata', + } +} diff --git a/test/e2e/app-dir/metadata/metadata.test.ts b/test/e2e/app-dir/metadata/metadata.test.ts index c65bf01058947..ade1390693023 100644 --- a/test/e2e/app-dir/metadata/metadata.test.ts +++ b/test/e2e/app-dir/metadata/metadata.test.ts @@ -316,6 +316,11 @@ createNextDescribe( /params - blog query - xxx/ ) }) + + it('should support synchronous generateMetadata export', async () => { + const browser = await next.browser('/basic/sync-generate-metadata') + expect(await getTitle(browser)).toBe('synchronous generateMetadata') + }) }) describe('opengraph', () => {