【问题标题】:Set cache-control header in getInitialProps() in NextJs在 NextJs 的 getInitialProps() 中设置缓存控制标头
【发布时间】:2018-06-19 11:25:46
【问题描述】:

我需要在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

如果有任何遗漏,请告诉我。

【问题讨论】:

    标签: javascript reactjs nextjs


    【解决方案1】:

    正如github bug中提到的那样

    使用res.setHeader()

    res 是 Node.js http.ServerResponse

    【讨论】:

    • rescontext.res 相同。
    • 如果是这样,我猜你应该重新打开 github 中的错误
    【解决方案2】:

    Cache-Control headers are overridden in development,这样页面就不会被浏览器缓存。

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

    【讨论】:

      猜你喜欢
      • 2011-10-01
      • 2013-04-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 2020-09-16
      • 1970-01-01
      • 1970-01-01
      • 2012-02-06
      相关资源
      最近更新 更多