From 925384c1cf150201aa4b7a47dd7edce77d411ef3 Mon Sep 17 00:00:00 2001 From: Luis Alvarez D Date: Wed, 29 Jul 2020 21:35:25 -0500 Subject: [PATCH] Added docs for Incremental Static Regeneration (#15663) --- docs/basic-features/data-fetching.md | 75 +++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/docs/basic-features/data-fetching.md b/docs/basic-features/data-fetching.md index b09a724798..736d449c1d 100644 --- a/docs/basic-features/data-fetching.md +++ b/docs/basic-features/data-fetching.md @@ -54,6 +54,11 @@ The `context` parameter is an object containing the following keys: - `preview` is `true` if the page is in the preview mode and `false` otherwise. See the [Preview Mode documentation](/docs/advanced-features/preview-mode.md). - `previewData` contains the preview data set by `setPreviewData`. See the [Preview Mode documentation](/docs/advanced-features/preview-mode.md). +`getStaticProps` should return an object with: + +- `props` - A **required** object with the props that will be received by the page component. It should be a [serializable object](https://en.wikipedia.org/wiki/Serialization) +- `revalidate` - An **optional** amount in seconds after which a page re-generation can occur. More on [Incremental Static Regeneration](#incremental-static-regeneration) + > **Note**: You can import modules in top-level scope for use in `getStaticProps`. > Imports used in `getStaticProps` will not be bundled for the client-side, as [explained below](#write-server-side-code-directly). @@ -143,6 +148,67 @@ function Blog({ posts }: InferGetStaticPropsType) { export default Blog ``` +### Incremental Static Regeneration + +> This feature was introduced in [Next.js 9.5](https://nextjs.org/blog/next-9-5#stable-incremental-static-regeneration) and up. If you’re using older versions of Next.js, please upgrade before trying Incremental Static Regeneration. + +
+ Examples + +
+ +With [`getStaticProps`](#getstaticprops-static-generation) you don't have to stop relying in dynamic content, as **static content can also be dynamic**. Incremental Static Regeneration allows you to update _existing_ pages by re-rendering them in the background as traffic comes in. + +Inspired by [stale-while-revalidate](https://tools.ietf.org/html/rfc5861), background regeneration ensures traffic is served uninterruptedly, always from static storage, and the newly built page is pushed only after it's done generating. + +Consider our previous [`getStaticProps` example](#simple-example), but now with regeneration enabled: + +```jsx +function Blog({ posts }) { + return ( + + ) +} + +// This function gets called at build time on server-side. +// It may be called again, on a serverless function, if +// revalidation is enabled and a new request comes in +export async function getStaticProps() { + const res = await fetch('https://.../posts') + const posts = await res.json() + + return { + props: { + posts, + }, + // Next.js will attempt to re-generate the page: + // - When a request comes in + // - At most once every second + revalidate: 1, // In seconds + } +} + +export default Blog +``` + +Now the list of blog posts will be revalidated once per second; if you add a new blog post it will be available almost immediately, without having to re-build your app or make a new deployment. + +This works perfectly with [`fallback: true`](#fallback-true). Because now you can have a list of posts that's always up to date with the latest posts, and have a [blog post page](#fallback-pages) that generates blog posts on-demand, no matter how many posts you add or update. + +#### Static content at scale + +Unlike traditional SSR, [Incremental Static Regeneration](#incremental-static-regeneration) ensures you retain the benefits of static: + +- No spikes in latency. Pages are served consistently fast +- Pages never go offline. If the background page re-generation fails, the old page remains unaltered +- Low database and backend load. Pages are re-computed at most once concurrently + ### Reading files: Use `process.cwd()` Files can be read directly from the filesystem in `getStaticProps`. @@ -387,7 +453,12 @@ export async function getStaticProps({ params }) { const post = await res.json() // Pass post data to the page via props - return { props: { post } } + return { + props: { post }, + // Re-generate the post at most once per second + // if a request comes in + revalidate: 1, + } } export default Post @@ -401,6 +472,8 @@ Instead, you may statically generate a small subset of pages and use `fallback: This ensures that users always have a fast experience while preserving fast builds and the benefits of Static Generation. +`fallback: true` will not _update_ generated pages, for that take a look at [Incremental Static Regeneration](#incremental-static-regeneration). + ### When should I use `getStaticPaths`? You should use `getStaticPaths` if you’re statically pre-rendering pages that use dynamic routes. -- GitLab