Unverified Commit d6cb99b3 authored by Lee Robinson's avatar Lee Robinson Committed by GitHub

Add docs about SSR caching (yes, you can do this with Next.js) (#36588)

I've heard a lot of folks not realize this is possible 😄 Hopefully this helps.
parent 0de109ba
...@@ -74,6 +74,32 @@ export async function getServerSideProps() { ...@@ -74,6 +74,32 @@ export async function getServerSideProps() {
export default Page 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 ## 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. 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.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment