Skip to content

Commit edc6e18

Browse files
devplaijjk
andauthored
docs(generate-static-params): add typescript example and missing jsx switcher (vercel#73962)
Hi Team. This PR enhances the Generate Static Pararms documentation by adding TypeScript examples, and adding missing jsx switcher code blocks, which previously caused some content to disappear when switching to JavaScript due to the absence of corresponding jsx examples. Co-authored-by: JJ Kasper <jj@jjsweb.site>
1 parent 4e79cef commit edc6e18

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

docs/01-app/04-api-reference/04-functions/generate-static-params.mdx

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,29 @@ description: API reference for the generateStaticParams function.
55

66
The `generateStaticParams` function can be used in combination with [dynamic route segments](/docs/app/building-your-application/routing/dynamic-routes) to [**statically generate**](/docs/app/building-your-application/rendering/server-components#static-rendering-default) routes at build time instead of on-demand at request time.
77

8-
```jsx filename="app/blog/[slug]/page.js"
8+
```tsx filename="app/blog/[slug]/page.tsx" switcher
9+
// Return a list of `params` to populate the [slug] dynamic segment
10+
export async function generateStaticParams() {
11+
const posts = await fetch('https://.../posts').then((res) => res.json())
12+
13+
return posts.map((post) => ({
14+
slug: post.slug,
15+
}))
16+
}
17+
18+
// Multiple versions of this page will be statically generated
19+
// using the `params` returned by `generateStaticParams`
20+
export default async function Page({
21+
params,
22+
}: {
23+
params: Promise<{ slug: string }>
24+
}) {
25+
const { slug } = await params
26+
// ...
27+
}
28+
```
29+
30+
```jsx filename="app/blog/[slug]/page.js" switcher
931
// Return a list of `params` to populate the [slug] dynamic segment
1032
export async function generateStaticParams() {
1133
const posts = await fetch('https://.../posts').then((res) => res.json())
@@ -193,6 +215,16 @@ export async function generateStaticParams() {
193215
}
194216
```
195217

218+
```jsx filename="app/blog/[slug]/page.js" switcher
219+
export async function generateStaticParams() {
220+
const posts = await fetch('https://.../posts').then((res) => res.json())
221+
222+
return posts.map((post) => ({
223+
slug: post.slug,
224+
}))
225+
}
226+
```
227+
196228
#### Subset of paths at build time
197229

198230
To statically render a subset of paths at build time, and the rest the first time they're visited at runtime, return a partial list of paths:
@@ -208,9 +240,34 @@ export async function generateStaticParams() {
208240
}
209241
```
210242

243+
```jsx filename="app/blog/[slug]/page.js" switcher
244+
export async function generateStaticParams() {
245+
const posts = await fetch('https://.../posts').then((res) => res.json())
246+
247+
// Render the first 10 posts at build time
248+
return posts.slice(0, 10).map((post) => ({
249+
slug: post.slug,
250+
}))
251+
}
252+
```
253+
211254
Then, by using the [`dynamicParams`](/docs/app/api-reference/file-conventions/route-segment-config#dynamicparams) segment config option, you can control what happens when a dynamic segment is visited that was not generated with `generateStaticParams`.
212255

213-
```jsx filename="app/blog/[slug]/page.js"
256+
```tsx filename="app/blog/[slug]/page.tsx" switcher
257+
// All posts besides the top 10 will be a 404
258+
export const dynamicParams = false
259+
260+
export async function generateStaticParams() {
261+
const posts = await fetch('https://.../posts').then((res) => res.json())
262+
const topPosts = posts.slice(0, 10)
263+
264+
return topPosts.map((post) => ({
265+
slug: post.slug,
266+
}))
267+
}
268+
```
269+
270+
```jsx filename="app/blog/[slug]/page.js" switcher
214271
// All posts besides the top 10 will be a 404
215272
export const dynamicParams = false
216273

0 commit comments

Comments
 (0)