Skip to content

Commit

Permalink
Add docs about SSR caching (yes, you can do this with Next.js) (#36588)
Browse files Browse the repository at this point in the history
I've heard a lot of folks not realize this is possible πŸ˜„ Hopefully this helps.
  • Loading branch information
leerob committed Apr 30, 2022
1 parent 0de109b commit d6cb99b
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions docs/basic-features/data-fetching/get-server-side-props.md
Expand Up @@ -74,6 +74,32 @@ export async function getServerSideProps() {
export default Page
```

## Caching with Server-Side Rendering (SSR)

You can use caching headers (`Cache-Control`) inside `getServerSideProps` to cache dynamic responses. For example, using [`stale-while-revalidate`](https://web.dev/stale-while-revalidate/).

```jsx
// This value is considered fresh for ten seconds (s-maxage=10).
// If a request is repeated within the next 10 seconds, the previously
// cached value will still be fresh. If the request is repeated before 59 seconds,
// the cached value will be stale but still render (stale-while-revalidate=59).
//
// In the background, a revalidation request will be made to populate the cache
// with a fresh value. If you refresh the page, you will see the new value.
export async function getServerSideProps({ req, res }) {
res.setHeader(
'Cache-Control',
'public, s-maxage=10, stale-while-revalidate=59'
)

return {
props: {},
}
}
```

Learn more about [caching](/docs/going-to-production.md).

## Does getServerSideProps render an error page

If an error is thrown inside `getServerSideProps`, it will show the `pages/500.js` file. Check out the documentation for [500 page](/docs/advanced-features/custom-error-page#500-page) to learn more on how to create it. During development this file will not be used and the dev overlay will be shown instead.
Expand Down

0 comments on commit d6cb99b

Please sign in to comment.