5

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.

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

2

as mentioned in this github bug

use res.setHeader()

res is a Node.js http.ServerResponse

2 Comments

res is same as context.res.
if so you should reopen the bug in github i guess

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.