Skip to content

Commit

Permalink
Allow generateStaticParams to be a synchronous function in app direct…
Browse files Browse the repository at this point in the history
…ory (#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)
  • Loading branch information
joulev committed Nov 16, 2022
1 parent fdc07c0 commit 6bbb52e
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion packages/next/build/webpack/plugins/flight-types-plugin.ts
Expand Up @@ -45,7 +45,7 @@ interface IEntry {
: `default: PageComponent`
}
config?: {}
generateStaticParams?: (params?: PageParams) => Promise<any[]>
generateStaticParams?: (params?: PageParams) => any[] | Promise<any[]>
revalidate?: RevalidateRange<TEntry> | false
dynamic?: 'auto' | 'force-dynamic' | 'error' | 'force-static'
dynamicParams?: boolean
Expand Down

0 comments on commit 6bbb52e

Please sign in to comment.