最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Set cache-control header in getInitialProps() in NextJs - Stack Overflow

programmeradmin4浏览0评论

I need to set Cache-Control header with a dynamic value in getInitialProps method. I tried the below.

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

But it looks looks like NextJs is overriding the header value before sending the response. Below is the cache-control header value in response header in browser.

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

Let me know if anything is missing.

I need to set Cache-Control header with a dynamic value in getInitialProps method. I tried the below.

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

But it looks looks like NextJs is overriding the header value before sending the response. Below is the cache-control header value in response header in browser.

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

Let me know if anything is missing.

Share Improve this question asked Jan 10, 2018 at 10:10 Cyril Sadasivan PanickerCyril Sadasivan Panicker 691 silver badge4 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Cache-Control headers are overridden in development, so that pages do not get cached by the browser.

It will work in production (next build && next start).

as mentioned in this github bug

use res.setHeader()

res is a Node.js http.ServerResponse

发布评论

评论列表(0)

  1. 暂无评论