Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 907ef21

Browse files
authored
[examples] Fix ssr-caching example. (vercel#26540)
Closes vercel#12019 with a better example of proper SSR caching.
1 parent c732c65 commit 907ef21

File tree

5 files changed

+39
-98
lines changed

5 files changed

+39
-98
lines changed

examples/ssr-caching/README.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
1-
# Example app where it caches SSR'ed pages in the memory
1+
# Server-Side Rendering Caching Headers
22

3-
React Server Side rendering is very costly and takes a lot of server's CPU power for that. One of the best solutions for this problem is cache already rendered pages.
4-
That's what this example demonstrate.
3+
This example uses [`stale-while-revalidate`](https://web.dev/stale-while-revalidate/) [cache-control headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control) in combination with `getServerSideProps` for server-rendering.
54

6-
This app uses Next's [custom server and routing](https://nextjs.org/docs/advanced-features/custom-server) mode. It also uses [express](https://expressjs.com/) to handle routing and page serving.
5+
`pages/index.js` uses `getServerSideProps` to forward the request header to the React component, as well as setting a response header. This `cache-control` header uses `stale-while-revalidate` to cache the server response.
6+
7+
`pages/index.js` 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`).
8+
9+
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 shown.
10+
11+
## Preview
12+
13+
Preview the example live on [StackBlitz](http://stackblitz.com/):
14+
15+
[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/vercel/next.js/tree/canary/examples/ssr-caching)
16+
17+
## Deploy your own
18+
19+
Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example):
20+
21+
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/ssr-caching&project-name=ssr-caching&repository-name=ssr-caching)
722

823
## How to use
924

examples/ssr-caching/package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22
"name": "ssr-caching",
33
"version": "1.0.0",
44
"scripts": {
5-
"dev": "node server.js",
5+
"dev": "next",
66
"build": "next build",
7-
"start": "cross-env NODE_ENV=production node server.js"
7+
"start": "next start"
88
},
99
"dependencies": {
10-
"cacheable-response": "^2.1.6",
11-
"cross-env": "^7.0.2",
12-
"express": "^4.17.1",
1310
"next": "latest",
1411
"react": "^17.0.2",
1512
"react-dom": "^17.0.2"

examples/ssr-caching/pages/blog/[id].js

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
1-
import Link from 'next/link'
2-
3-
export default function Home() {
1+
export default function Index({ time }) {
42
return (
5-
<ul>
6-
<li>
7-
<Link href="/blog?id=first" as="/blog/first">
8-
<a>My first blog post</a>
9-
</Link>
10-
</li>
11-
<li>
12-
<Link href="/blog?id=second" as="/blog/second">
13-
<a>My second blog post</a>
14-
</Link>
15-
</li>
16-
<li>
17-
<Link href="/blog?id=last" as="/blog/last">
18-
<a>My last blog post</a>
19-
</Link>
20-
</li>
21-
</ul>
3+
<main>
4+
<h1>SSR Caching with Next.js</h1>
5+
<time dateTime={time}>{time}</time>
6+
</main>
227
)
238
}
9+
10+
export async function getServerSideProps({ req, res }) {
11+
res.setHeader(
12+
'Cache-Control',
13+
'public, s-maxage=10, stale-while-revalidate=59'
14+
)
15+
16+
return {
17+
props: {
18+
time: new Date().toISOString(),
19+
},
20+
}
21+
}

examples/ssr-caching/server.js

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)