我们很少有页面和组件作为服务器端渲染。
我们尝试对少数API响应使用缓存。
export async function getServerSideProps(context) {
const res = await getRequest(API.home)
return {
props: {
"home": res?.data?.result
},
}
}
Next.js的版本是11.1。
这里可以有人建议我们如何实现缓存吗?
您可以使用res.setHeader
在getServerSideProps
内设置Cache-Control
标头。
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
}
}
}
设置Cache-Control
值仅在生产模式下有效,因为在开发模式中将覆盖标头。
有关详细信息,请参见使用服务器端渲染进行缓存文档。