From 6bbb52edfe3e969b4a80dc2d32c961303bb8c848 Mon Sep 17 00:00:00 2001 From: Vu Van Dung Date: Thu, 17 Nov 2022 06:25:20 +0800 Subject: [PATCH] Allow generateStaticParams to be a synchronous function in app directory (#42942) ## 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) --- packages/next/build/webpack/plugins/flight-types-plugin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/next/build/webpack/plugins/flight-types-plugin.ts b/packages/next/build/webpack/plugins/flight-types-plugin.ts index 54d31b078a7453c..15371d5337a2b75 100644 --- a/packages/next/build/webpack/plugins/flight-types-plugin.ts +++ b/packages/next/build/webpack/plugins/flight-types-plugin.ts @@ -45,7 +45,7 @@ interface IEntry { : `default: PageComponent` } config?: {} - generateStaticParams?: (params?: PageParams) => Promise + generateStaticParams?: (params?: PageParams) => any[] | Promise revalidate?: RevalidateRange | false dynamic?: 'auto' | 'force-dynamic' | 'error' | 'force-static' dynamicParams?: boolean