Unverified Commit 7654d7bc authored by Vu Van Dung's avatar Vu Van Dung Committed by GitHub

Allow `generateMetadata` to be synchronous (#45696)

The very-newly-released `generateMetadata` function currently does not
allow itself to be synchronous. It leads to situations like this

```ts
export async function generateMetadata() {
  return { title: "Hello, world!" };
}
```

where there's evidently no reason to use `async`, but it's forced by
Next.js.

This PR aims to remove this restriction: `generateMetadata` can be
synchronous now.

## Bug

- [x] Related issues linked using `fixes #45685`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [x]
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
Signed-off-by: 's avatarVu Van Dung <me@joulev.dev>
Co-authored-by: 's avatarJiachi Liu <inbox@huozhi.im>
parent a25c32a9
...@@ -66,7 +66,7 @@ interface IEntry { ...@@ -66,7 +66,7 @@ interface IEntry {
: '' : ''
} }
metadata?: any metadata?: any
generateMetadata?: (props: PageProps, parent: ResolvingMetadata) => Promise<any> generateMetadata?: (props: PageProps, parent: ResolvingMetadata) => any | Promise<any>
} }
// ============= // =============
......
...@@ -328,7 +328,9 @@ function merge( ...@@ -328,7 +328,9 @@ function merge(
} }
} }
type MetadataResolver = (_parent: ResolvingMetadata) => Promise<Metadata> type MetadataResolver = (
_parent: ResolvingMetadata
) => Metadata | Promise<Metadata>
export type MetadataItems = [ export type MetadataItems = [
Metadata | MetadataResolver | null, Metadata | MetadataResolver | null,
Metadata | null Metadata | null
...@@ -410,10 +412,11 @@ export async function accumulateMetadata( ...@@ -410,10 +412,11 @@ export async function accumulateMetadata(
for (const item of metadataItems) { for (const item of metadataItems) {
const [metadataExport, staticFilesMetadata] = item const [metadataExport, staticFilesMetadata] = item
const layerMetadataPromise = const layerMetadataPromise = Promise.resolve(
typeof metadataExport === 'function' typeof metadataExport === 'function'
? metadataExport(parentPromise) ? metadataExport(parentPromise)
: Promise.resolve(metadataExport) : metadataExport
)
parentPromise = parentPromise.then((resolved) => { parentPromise = parentPromise.then((resolved) => {
return layerMetadataPromise.then((exportedMetadata) => { return layerMetadataPromise.then((exportedMetadata) => {
......
export default function Page() {
return 'synchronous generateMetadata'
}
export function generateMetadata() {
return {
title: 'synchronous generateMetadata',
}
}
...@@ -316,6 +316,11 @@ createNextDescribe( ...@@ -316,6 +316,11 @@ createNextDescribe(
/params - blog query - xxx/ /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', () => { describe('opengraph', () => {
......
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