Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

context.res.setHeader not setting Cache-Control #62294

Open
eloisetaylor5693 opened this issue Feb 20, 2024 · 5 comments
Open

context.res.setHeader not setting Cache-Control #62294

eloisetaylor5693 opened this issue Feb 20, 2024 · 5 comments
Labels
bug Issue was opened via the bug report template. Pages Router Related to Pages Router.

Comments

@eloisetaylor5693
Copy link

eloisetaylor5693 commented Feb 20, 2024

Link to the code that reproduces this issue

https://github.com/eloisetaylor5693/nextjs-set-cache-control

To Reproduce

export async function getServerSideProps(context: GetServerSidePropsContext) {
  context.res.setHeader(
    "Cache-Control",
    "public, s-maxage=300, stale-while-revalidate=600"
  );
  return {
    props: { title: "hello world" },
  };
}

results in the response header
Cache-Control: no-store, must-revalidate

Current vs. Expected behavior

When testing in the browser, the response header doesn't include the cache control I set

Current: Cache-Control: no-store, must-revalidate
Expected: Cache-Control: public, s-maxage=300, stale-while-revalidate=600

Provide environment information

Node: 20.10.0
npm: 10.2.3
Nextjs: canary

Which area(s) are affected? (Select all that apply)

Data fetching (gS(S)P, getInitialProps)

Which stage(s) are affected? (Select all that apply)

Other (Deployed)

Additional context

No response

@eloisetaylor5693 eloisetaylor5693 added the bug Issue was opened via the bug report template. label Feb 20, 2024
@github-actions github-actions bot added the Pages Router Related to Pages Router. label Feb 20, 2024
@victorcarvalhosp

This comment has been minimized.

@victorcarvalhosp
Copy link

Exact same issue here. This is costing us a lot of money when Google crawls our website especially... Did you find some workaround for it?

@eloisetaylor5693
Copy link
Author

eloisetaylor5693 commented Aug 27, 2024

Hi @victorcarvalhosp, no I didn't. The only other options are SSG, implement your own caching of data eg with redis, or migrate to app router.

These aren't satisfactory workarounds since they are not necessarily appropriate for a page you want to cache depending on requirements

@lhguerra
Copy link

Migrating to app router might not help much. There are a lot of little things that can make a page dynamic and it won't cache at all. For instance, in v13 with pages router I could have a static page that accepted query strings (provided that those query strings are used only in client side), now I can't and the page must be dynamic, which in pages router with v13 I solved setting a cache header because I have Clouflare in front of Vercel. But with v14 it is impossible to set that header so dynamic pages can't be cached in Vercel and anywhere else!

@victorcarvalhosp
Copy link

Migrating to app router might not help much. There are a lot of little things that can make a page dynamic and it won't cache at all. For instance, in v13 with pages router I could have a static page that accepted query strings (provided that those query strings are used only in client side), now I can't and the page must be dynamic, which in pages router with v13 I solved setting a cache header because I have Clouflare in front of Vercel. But with v14 it is impossible to set that header so dynamic pages can't be cached in Vercel and anywhere else!

I had the exact same problem when I started migrating to the app router, so we decided not to proceed with that for now, as it would be a lot of work and we couldn't even be sure if it would cost less.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Pages Router Related to Pages Router.
Projects
None yet
Development

No branches or pull requests

3 participants