Cache-Control headers not applied in getServerSideProps

If I set the Cache-Control headers inside the getServerSideProps function, Netlify seems to ignore them.

Example:

export const getServerSideProps: GetServerSideProps = async ({
  res,
}) => {
  res.setHeader('Cache-Control', 's-maxage=60, stale-while-revalidate');
  return {
    props: {
      data: await getData(),  // Can be whatever
    },
  };
};

When I run the app locally in production mode, the Cache-Control headers are set as expected. However, in Netlify, the returned Cache-Control headers are always set to public, max-age=0, must-revalidate. Why is this? And is there any way around this?

Is the comment here and the following code responsible for this?

Hi @alex.hedlund,

I do think so. You can open an issue on the repo to confirm with the engineers, or we can do it for you.

Well if you can do it for me, all the better :slight_smile:

Hi, @alex.hedlund. Would you please share a URL with us where the headers should be present but are not?

I want to confirm if the URL is being handled by a static file, SSR or ISR (the ODB function).

You can post that information publicly or you can private message (PM) that to one of our support staff. I’ve confirmed that PMs are enabled for your forum login. Please keep in mind that only one person can see the PM and this will likely mean a slower reply than posting the information publicly. Please feel free to reply to however you prefer though.

Hi, @luke! I sent you a PM.

Hey @alex.hedlund,

We confirmed with the devs and yes, it’s not possible to set a custom cache control headers because it will cause weird issues especially when you re-deploy.

Also, it appears that you’re trying to use SWR, so could you probably convert the page to ISR and get the feature you need.

Thanks for the confirmation. I feel like this could be better documented in the Netlify docs since this restriction isn’t in place at other hosting platforms.

ISR can be an option at sometimes, but sometimes you need server-side rendering, and with SSR in production you often need cache-control headers.

thank you, alex. we’ll make a note of your suggestion and see if we can make this a bit more obvious.

Hey there, @alex.hedlund :wave:

Hillary here from the Support team! I just wanted to follow up and let you know that after reviewing this thread, our Docs team has implemented your feedback to include that we don’t support cache-control in the docs, as well as provide some more guidance.

Thanks again for taking the time to share your feedback with us. This helps us improve our product!