jpk*_*jpk 5 javascript server-side-rendering next.js
我们有很少的页面和组件作为服务器端渲染。
我们尝试对少数 API 响应使用缓存。
export async function getServerSideProps(context) {
const res = await getRequest(API.home)
return {
props: {
"home": res?.data?.result
},
}
}
Run Code Online (Sandbox Code Playgroud)
Next.js 版本是 11.1。
有人可以建议我们如何实现缓存吗?
jul*_*ves 15
您可以使用Cache-Control内部设置标题。getServerSidePropsres.setHeader
export async function getServerSideProps(context) {
// Add whatever `Cache-Control` value you want here
context.res.setHeader(
'Cache-Control',
'public, s-maxage=10, stale-while-revalidate=59'
)
const res = await getRequest(API.home)
return {
props: {
home: res?.data?.result
}
}
}
Run Code Online (Sandbox Code Playgroud)
设置Cache-Control值仅适用于生产模式,因为标头将在开发模式下被覆盖。
有关更多详细信息,请参阅使用服务器端渲染进行缓存文档。
| 归档时间: |
|
| 查看次数: |
12139 次 |
| 最近记录: |