0

How do I implement Caching at SSR for Next.js They have mentioned SSR Caching https://nextjs.org/docs/advanced-features/custom-server which is not working Apparently Next.js have made app.renderToHTML() as obsolete and cant be used with getServerSideProps function and using app.render() in the caching function which always gives undefined and breaks with the following error

(node:20872) UnhandledPromiseRejectionWarning: TypeError: argument entity is required
at etag (C:\xampp\htdocs\next-app\node_modules\etag\index.js:72:11)
at C:\xampp\htdocs\next-app\node_modules\cacheable-response\index.js:93:32
at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use node --trace-warnings ... to show where the warning was created)
(node:20872) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:20872) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

because of which the above example breaking.

Here is the github link where I have raised an issue https://github.com/vercel/next.js/issues/25579

Can any one help me in this?

4
  • Have you had a look at their official SSR caching example? Commented May 30, 2021 at 11:34
  • Yes. That is the one which I am talking about. Which is not working Commented May 30, 2021 at 11:45
  • They do not use renderToHTML in that example, as you mentioned in your question. Commented May 30, 2021 at 11:47
  • But prviously it was working well with renderToHTML. now its render function which give undefined in caching function here is the github url where i have raised the issue github.com/vercel/next.js/issues/25579 Commented May 30, 2021 at 11:50

1 Answer 1

2

This solution suggested by @awareness481 on github seems to fix the issue for me and I can confirm that the cache is also working. I tested it by putting a trace in get method of cachableResponse just before app.render which only gets called if there is no cached response.

Also testing the blog pages via Postman, I am getting header X-Cache-Status as HIT after first call and response time has reduced from >300 to <20

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.