如何在Next.js的SSR上实现缓存他们提到了SSR缓存https://nextjs.org/docs/advanced-features/custom-server不起作用显然,Next.js已经使app.renderToHTML((过时,不能与getServerSideProps函数一起使用,也不能在缓存函数中使用app.render((,缓存函数总是给出未定义并中断,并出现以下错误
(node:20872) UnhandledPromiseRejectionWarning: TypeError: argument entity is required
at etag (C:xampphtdocsnext-appnode_modulesetagindex.js:72:11)
at C:xampphtdocsnext-appnode_modulescacheable-responseindex.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.
正因为如此,上面的例子被打破了。
这是我提出问题的github链接https://github.com/vercel/next.js/issues/25579
有人能帮我吗?
@awareness481在github上提出的这个解决方案似乎为我解决了这个问题,我可以确认缓存也在工作。我通过在app.render
之前的cachableResponse
的get
方法中放置一个跟踪来测试它,只有在没有缓存响应的情况下才会调用该方法。
同样通过Postman测试博客页面,我在第一次调用后得到标题X-Cache-Status
作为HIT
,响应时间从>300
减少到<20