Skip to content

Commit b9bee24

Browse files
arunodarauchg
authored andcommitted
Add an example with SSR caching. (#497)
* Add an example with SSR caching. * Update server.js * Update README.md * Use app.renderError to display errors in SSR example.
1 parent 01da6f4 commit b9bee24

File tree

5 files changed

+125
-0
lines changed

5 files changed

+125
-0
lines changed

examples/ssr-caching/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
# Example app where it caches SSR'ed pages in the memory
3+
4+
## How to use
5+
6+
Download the example (or clone the repo)[https://github.com/zeit/next.js.git]:
7+
8+
```bash
9+
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/ssr-caching
10+
cd ssr-caching
11+
```
12+
13+
Install it and run:
14+
15+
```bash
16+
npm install
17+
npm run dev
18+
```
19+
20+
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
21+
22+
```bash
23+
now
24+
```
25+
26+
## The idea behind the example
27+
28+
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.
29+
That's what this example demonstrate.
30+
31+
This app uses Next's [custom server and routing](https://github.com/zeit/next.js#custom-server-and-routing) mode. It also uses [express](https://expressjs.com/) to handle routing and page serving.

examples/ssr-caching/package.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"scripts": {
3+
"start": "node server.js"
4+
},
5+
"dependencies": {
6+
"express": "^4.14.0",
7+
"lru-cache": "^4.0.2",
8+
"next": "^2.0.0-beta"
9+
}
10+
}

examples/ssr-caching/pages/blog.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react'
2+
3+
export default class extends React.Component {
4+
static getInitialProps ({ query: { id } }) {
5+
return { id }
6+
}
7+
8+
render () {
9+
return <div>
10+
<h1>My {this.props.id} blog post</h1>
11+
<p>
12+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
13+
tempor incididunt ut labore et dolore magna aliqua.
14+
</p>
15+
</div>
16+
}
17+
}

examples/ssr-caching/pages/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Link from 'next/link'
2+
3+
export default () => (
4+
<ul>
5+
<li><Link href='/blog?id=first' as='/blog/first'><a>My first blog post</a></Link></li>
6+
<li><Link href='/blog?id=second' as='/blog/second'><a>My second blog post</a></Link></li>
7+
<li><Link href='/blog?id=last' as='/blog/last'><a>My last blog post</a></Link></li>
8+
</ul>
9+
)

examples/ssr-caching/server.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const express = require('express')
2+
const next = require('next')
3+
const LRUCache = require('lru-cache')
4+
5+
const app = next({ dir: '.', dev: true })
6+
const handle = app.getRequestHandler()
7+
8+
// This is where we cache our rendered HTML pages
9+
const ssrCache = new LRUCache({
10+
max: 100,
11+
maxAge: 1000 * 60 * 60 // 1hour
12+
})
13+
14+
app.prepare()
15+
.then(() => {
16+
const server = express()
17+
18+
// Use the `renderAndCache` utility defined below to serve pages
19+
server.get('/', (req, res) => {
20+
renderAndCache(req, res, '/')
21+
})
22+
23+
server.get('/blog/:id', (req, res) => {
24+
const queryParams = { id: req.paradms.id }
25+
renderAndCache(req, res, '/blog', queryParams)
26+
})
27+
28+
server.get('*', (req, res) => {
29+
return handle(req, res)
30+
})
31+
32+
server.listen(3000, (err) => {
33+
if (err) throw err
34+
console.log('> Ready on http://localhost:3000')
35+
})
36+
})
37+
38+
function renderAndCache (req, res, pagePath, queryParams) {
39+
// If we have a page in the cache, let's serve it
40+
if (ssrCache.has(req.url)) {
41+
console.log(`CACHE HIT: ${req.url}`)
42+
res.send(ssrCache.get(req.url))
43+
return
44+
}
45+
46+
// If not let's render the page into HTML
47+
app.renderToHTML(req, res, pagePath, queryParams)
48+
.then((html) => {
49+
// Let's cache this page
50+
console.log(`CACHE MISS: ${req.url}`)
51+
ssrCache.set(req.url, html)
52+
53+
res.send(html)
54+
})
55+
.catch((err) => {
56+
app.renderError(err, req, res, pagePath, queryParams)
57+
})
58+
}

0 commit comments

Comments
 (0)