You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api-routes/response-helpers.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,6 +12,7 @@ The included helpers are:
12
12
-`res.json(body)` - Sends a JSON response. `body` must be a [serializable object](https://developer.mozilla.org/en-US/docs/Glossary/Serialization)
13
13
-`res.send(body)` - Sends the HTTP response. `body` can be a `string`, an `object` or a `Buffer`
14
14
-`res.redirect([status,] path)` - Redirects to a specified path or URL. `status` must be a valid [HTTP status code](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes). If not specified, `status` defaults to "307" "Temporary redirect".
15
+
-`res.unstable_revalidate(urlPath)` - [Revalidate a page on demand](/docs/basic-features/data-fetching/incremental-static-regeneration.md#on-demand-revalidation-beta) using `getStaticProps`. `urlPath` must be a `string`.
Copy file name to clipboardExpand all lines: docs/basic-features/data-fetching/get-static-paths.md
+10-2Lines changed: 10 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,7 @@ export async function getStaticPaths() {
19
19
}
20
20
```
21
21
22
-
Note that`getStaticProps`**must** be used with `getStaticPaths`, and that you**cannot** use it with [`getServerSideProps`](/docs/basic-features/data-fetching/get-server-side-props.md).
22
+
`getStaticPaths`**must** be used with `getStaticProps`. You**cannot** use it with [`getServerSideProps`](/docs/basic-features/data-fetching/get-server-side-props.md).
23
23
24
24
The [`getStaticPaths` API reference](/docs/api-reference/data-fetching/get-static-paths.md) covers all parameters and props that can be used with `getStaticPaths`.
25
25
@@ -35,7 +35,11 @@ You should use `getStaticPaths` if you’re statically pre-rendering pages that
35
35
36
36
## When does getStaticPaths run
37
37
38
-
`getStaticPaths` only runs at build time on server-side. If you're using [Incremental Static Regeneration](/docs/basic-features/data-fetching/incremental-static-regeneration.md), `getStaticPaths` can also be run on-demand _in the background_, but still only on the server-side.
38
+
`getStaticPaths` always runs on the server and never on the client. You can validate code written inside `getStaticPaths` is removed from the client-side bundle [with this tool](https://next-code-elimination.vercel.app/).
39
+
40
+
-`getStaticPaths` runs during `next build` for Pages included in `paths`
41
+
-`getStaticPaths` runs on-demand in the background when using `fallback: true`
42
+
-`getStaticPaths` runs on-demand blocking rendering when using `fallback: blocking`
39
43
40
44
## Where can I use getStaticPaths
41
45
@@ -47,6 +51,10 @@ Note that you must use export `getStaticPaths` as a standalone function — it w
47
51
48
52
In development (`next dev`), `getStaticPaths` will be called on every request.
49
53
54
+
## Related
55
+
56
+
For more information on what to do next, we recommend the following sections:
Copy file name to clipboardExpand all lines: docs/basic-features/data-fetching/get-static-props.md
+13-3Lines changed: 13 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,9 +23,17 @@ You should use `getStaticProps` if:
23
23
- The data can be publicly cached (not user-specific)
24
24
- The page must be pre-rendered (for SEO) and be very fast — `getStaticProps` generates `HTML` and `JSON` files, both of which can be cached by a CDN for performance
25
25
26
+
## When does getStaticProps run
27
+
28
+
`getStaticProps` always runs on the server and never on the client. You can validate code written inside `getStaticProps` is removed from the client-side bundle [with this tool](https://next-code-elimination.vercel.app/).
29
+
30
+
-`getStaticProps` always runs during `next build`
31
+
-`getStaticProps` runs in the background when using `revalidate`
32
+
-`getStaticProps` runs on-demand in the background when using [`unstable_revalidate`](/docs/basic-features/data-fetching/incremental-static-regeneration.md#on-demand-revalidation-beta)
33
+
26
34
When combined with [Incremental Static Regeneration](/docs/basic-features/data-fetching/incremental-static-regeneration.md), `getStaticProps` will run in the background while the stale page is being revalidated, and the fresh page served to the browser.
27
35
28
-
Because `getStaticProps`runs at build time, it does **not** have access to the incoming request (such as query parameters or `HTTP` headers) as it generates static `HTML`. If you need access to the request for your page, consider using [Middleware](/docs/middleware.md) in addition to `getStaticProps`.
36
+
`getStaticProps` does not have access to the incoming request (such as query parameters or HTTP headers) as it generates static HTML. If you need access to the request for your page, consider using [Middleware](/docs/middleware.md) in addition to `getStaticProps`.
29
37
30
38
## Using getStaticProps to fetch data from a CMS
31
39
@@ -128,9 +136,11 @@ In development (`next dev`), `getStaticProps` will be called on every request.
128
136
129
137
## Preview Mode
130
138
131
-
In some cases, you might want to temporarily bypass Static Generation and render the page at **request time** instead of build time. For example, you might be using a headless CMS and want to preview drafts before they're published.
139
+
You can temporarily bypass static generation and render the page at **request time** instead of build time using [**Preview Mode**](/docs/advanced-features/preview-mode.md). For example, you might be using a headless CMS and want to preview drafts before they're published.
140
+
141
+
## Related
132
142
133
-
This use case is supported in Next.js by the [**Preview Mode**](/docs/advanced-features/preview-mode.md) feature.
143
+
For more information on what to do next, we recommend the following sections:
@@ -85,7 +87,61 @@ When a request is made to a page that was pre-rendered at build time, it will in
85
87
86
88
When a request is made to a path that hasn’t been generated, Next.js will server-render the page on the first request. Future requests will serve the static file from the cache. ISR on Vercel [persists the cache globally and handles rollbacks](https://vercel.com/docs/concepts/next.js/incremental-static-regeneration).
87
89
88
-
## Error Handling and Revalidation
90
+
## On-demand Revalidation (Beta)
91
+
92
+
If you set a `revalidate` time of `60`, all visitors will see the same generated version of your site for one minute. The only way to invalidate the cache is from someone visiting that page after the minute has passed.
93
+
94
+
Starting with `v12.1.0`, Next.js supports on-demand Incremental Static Regeneration to manually purge the Next.js cache for a specific page. This makes it easier to update your site when:
95
+
96
+
- Content from your headless CMS is created or updated
Inside `getStaticProps`, you do not need to specify `revalidate` to use on-demand revalidation. If `revalidate` is omitted, Next.js will use the default value of `false` (no revalidation) and only revalidate the page on-demand when `unstable_revalidate` is called.
100
+
101
+
### Using On-Demand Revalidation
102
+
103
+
First, create a secret token only known by your Next.js app. This secret will be used to prevent unauthorized access to the revalidation API Route. You can access the route (either manually or with a webhook) with the following URL structure:
Next, add the secret as an [Environment Variable](/docs/basic-features/environment-variables.md) to your application. Finally, create the revalidation API Route:
110
+
111
+
```jsx
112
+
// pages/api/revalidate.js
113
+
114
+
exportdefaultasyncfunctionhandler(req, res) {
115
+
// Check for secret to confirm this is a valid request
116
+
if (req.query.secret!==process.env.MY_SECRET_TOKEN) {
[View our demo](https://on-demand-isr.vercel.app) to see on-demand revalidation in action and provide feedback.
132
+
133
+
### Testing on-demand ISR during development
134
+
135
+
When running locally with `next dev`, `getStaticProps` is invoked on every request. To verify your on-demand ISR configuration is correct, you will need to create a [production build](/docs/api-reference/cli.md#build) and start the [production server](/docs/api-reference/cli.md#production):
136
+
137
+
```bash
138
+
$ next build
139
+
$ next start
140
+
```
141
+
142
+
Then, you are able to validate static pages are successfully revalidated.
143
+
144
+
## Error handling and revalidation
89
145
90
146
If there is an error inside `getStaticProps` when handling background regeneration, or you manually throw an error, the last successfully generated page will continue to show. On the next subsequent request, Next.js will retry calling `getStaticProps`.
91
147
@@ -114,3 +170,14 @@ export async function getStaticProps() {
114
170
}
115
171
}
116
172
```
173
+
174
+
## Related
175
+
176
+
For more information on what to do next, we recommend the following sections:
0 commit comments