繁体   English   中英

在NextJs的getInitialProps()中设置缓存控制标头

[英]Set cache-control header in getInitialProps() in NextJs

我需要在getInitialProps方法中使用动态值设置Cache-Control标头。 我尝试了以下。

if(context.res){
    context.res.setHeader('Cache-Control','My-Cache-Control');
    context.res.setHeader('My-Header','My-Value');
}

但是看起来NextJs在发送响应之前覆盖了头值。 以下是浏览器中响应标头中的cache-control标头值。

Cache-Control: no-store, must-revalidate
My-Header: My-Value

让我知道是否有任何遗漏。

如在这个github bug中提到的

使用res.setHeader()

res是一个Node.js http.ServerResponse

Cache-Control标头在开发中被覆盖 ,因此浏览器不会缓存页面。

它将在生产中工作( next build && next start )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM