Skip to content

Commit 6bbb52e

Browse files
authored
Allow generateStaticParams to be a synchronous function in app directory (#42942)
<!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change that you're making: --> ## Summary In `appDir`, currerntly `generateStaticParams` must be an `async` function, even if no data fetching or asynchronous operations are involved. For example, with this ```ts // app/[slug]/page.tsx export async function generateStaticParams() { return [{ slug: "Hello" }]; } ``` if I remove the `async` keyword, `next build` type checking will fail with a rather unclear error message ``` Type error: Page "app/[slug]/page.tsx" does not match the required types of a Next.js Page. ``` However `next dev` still works fine, and after applying the type change in this PR, `next build` and `next start` will also work fine. Considering that `getStaticPaths` can be synchronous, this requirement of `async` is pretty confusing. Many people have reported this type error for not marking `generateStaticParams` as `async` (me included). This PR lifts the restriction and allows `generateStaticParams` to be synchronous. (If it's intentional that `generateStaticParams` must be asynchronous, feel free to close this PR, but I don't think that restriction is a good idea...) Since I cannot find any test cases on the type checking process, I'm a bit unsure in how to write a test case for this... or if a test case is even necessary at all. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a helpful link attached, see `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` - [ ] Integration 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` ## 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)
1 parent fdc07c0 commit 6bbb52e

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

packages/next/build/webpack/plugins/flight-types-plugin.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ interface IEntry {
4545
: `default: PageComponent`
4646
}
4747
config?: {}
48-
generateStaticParams?: (params?: PageParams) => Promise<any[]>
48+
generateStaticParams?: (params?: PageParams) => any[] | Promise<any[]>
4949
revalidate?: RevalidateRange<TEntry> | false
5050
dynamic?: 'auto' | 'force-dynamic' | 'error' | 'force-static'
5151
dynamicParams?: boolean

0 commit comments

Comments
 (0)