Skip to content

Commit 6c5efd7

Browse files
authored
Fix ssr-caching example (vercel#12180)
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
1 parent 0b94756 commit 6c5efd7

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

examples/ssr-caching/server.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ const handle = app.getRequestHandler()
1010

1111
const ssrCache = cacheableResponse({
1212
ttl: 1000 * 60 * 60, // 1hour
13-
get: async ({ req, res, pagePath, queryParams }) => {
14-
const data = await app.renderToHTML(req, res, pagePath, queryParams)
13+
get: async ({ req, res }) => {
14+
const data = await app.renderToHTML(req, res, req.path, {
15+
...req.query,
16+
...req.params,
17+
})
1518

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

31-
server.get('/', (req, res) => ssrCache({ req, res, pagePath: '/' }))
34+
server.get('/', (req, res) => ssrCache({ req, res }))
3235

3336
server.get('/blog/:id', (req, res) => {
34-
const queryParams = { id: req.params.id }
35-
const pagePath = '/blog'
36-
return ssrCache({ req, res, pagePath, queryParams })
37+
return ssrCache({ req, res })
3738
})
3839

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

0 commit comments

Comments
 (0)