Skip to content

Commit

Permalink
Fix ssr-caching example (vercel#12180)
Browse files Browse the repository at this point in the history
cacheable-response.get expects a get(req, res) method signature:
https://www.npmjs.com/package/cacheable-response#get

Before the change:
```
curl -s  -v http://localhost:3000/blog/first 2>&1 | grep HTTP/1.1
> GET /blog/first HTTP/1.1
< HTTP/1.1 404 Not Found
```
After the change:
```
curl -s  -v http://localhost:3000/blog/first 2>&1 | grep HTTP/1.1
> GET /blog/first HTTP/1.1
< HTTP/1.1 200 OK
```

This is a partial fix of vercel#12019 to make the example work. However it doesn't fix the error 
```
(node:62360) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
``` , which will need a change at server/next-server.ts

See related issue:
vercel#11525 
vercel#11665
  • Loading branch information
xliusstk authored and rokinsky committed Jul 11, 2020
1 parent 89a8623 commit 5cc0b1a
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions examples/ssr-caching/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ const handle = app.getRequestHandler()

const ssrCache = cacheableResponse({
ttl: 1000 * 60 * 60, // 1hour
get: async ({ req, res, pagePath, queryParams }) => {
const data = await app.renderToHTML(req, res, pagePath, queryParams)
get: async ({ req, res }) => {
const data = await app.renderToHTML(req, res, req.path, {
...req.query,
...req.params,
})

// Add here custom logic for when you do not want to cache the page, for
// example when the page returns a 404 status code:
Expand All @@ -28,12 +31,10 @@ const ssrCache = cacheableResponse({
app.prepare().then(() => {
const server = express()

server.get('/', (req, res) => ssrCache({ req, res, pagePath: '/' }))
server.get('/', (req, res) => ssrCache({ req, res }))

server.get('/blog/:id', (req, res) => {
const queryParams = { id: req.params.id }
const pagePath = '/blog'
return ssrCache({ req, res, pagePath, queryParams })
return ssrCache({ req, res })
})

server.get('*', (req, res) => handle(req, res))
Expand Down

0 comments on commit 5cc0b1a

Please sign in to comment.