简体   繁体   中英

Set cache-control header in getInitialProps() in NextJs

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.

as mentioned in this github bug

use res.setHeader()

res is a Node.js http.ServerResponse

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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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