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

Automatically set cache-control header for cached pages #64

Closed
wants to merge 2 commits into from

Conversation

queicherius
Copy link

@queicherius queicherius commented Jan 25, 2022

This PR automatically sets the Cache-Control header to public, max-age=<TTL>, must-revalidate for pages that are served from cache with a TTL > 0.

I'm up for adding more customizability to this which could live on the routes level if this is something you would like. I'm also thinking about if the cache header should be public, max-age=0, s-maxage=<TTL>, stale-while-revalidate instead.

Closes #23

@rjyo
Copy link
Collaborator

rjyo commented Jan 26, 2022

@queicherius Thanks for the PR. Seems there's an error in the test case. I'll check this later.

@queicherius
Copy link
Author

queicherius commented Jan 26, 2022

I was testing this change a bit more since I opened the PR and I feel like since the headers depend on the application it would be good to have a configuration option for this.

I could implement an extension to rules like this (tho it would not work for rules as a function without a breaking change):

  rules: [
    { url: '^/_next/static/.*', ttl: 60 * 60, cacheControl: 'public, max-age=31536000, immutable' },
    { url: '.*', ttl: 5 * 60, cacheControl: 'public, max-age=0, s-maxage=300, stale-while-revalidate' },
  ],

or a function:

  cacheControl: (req, ttl) => {
    if (req.url.startsWith('/_next/static')) {
      return 'public, max-age=31536000, immutable'
    }

    return `public, max-age=0, s-maxage=${ttl}, stale-while-revalidate`
  },

Which would you prefer?

@queicherius
Copy link
Author

I've updated the implementation with the second option (the cacheControl configuration option). Let me know if you're happy with that, and then I'll update tests & docs 👍

@benkingcode
Copy link

What happens if getServerSideProps sets its own cache control header? Can't we just pass that along? That's where we're implementing our own stale-while-revalidate settings

@queicherius queicherius closed this Jan 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Set cache-control header
3 participants