Skip to content

Conversation

@eduardoboucas
Copy link
Member

@eduardoboucas eduardoboucas commented Sep 5, 2025

Exports the cacheHeaders method that returns a set of cache headers based on the settings supplied.

Example

import { cacheHeaders, DAY } from "@netlify/cache"

export default async () => new Response("I shall be cached", {
  headers: {
    ...cacheHeaders({
      ttl: 2 * DAY, // Two days
      tags: ["product", "sale"],
      vary: {
        cookie: ["ab_test_name", "ab_test_bucket"],
        query: ["item_id", "page"]
      }
    })
  }
})

@eduardoboucas eduardoboucas requested a review from a team as a code owner September 5, 2025 09:42
@@ -1,5 +1,5 @@
export { caches } from './polyfill.js'
export { setCacheHeaders } from './cache-headers/cache-headers.js'
export { cacheHeaders, setCacheHeaders } from './cache-headers/cache-headers.js'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps we should rename the method to netlifyCacheHeaders as it is not a generic cache headers function, it generates the netlify-cdn-cache-control, netlify-vary, netlify-cache-tag, and netlify-cache-id headers - what do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great question. I'm a bit torn, to be honest. I don't think the goal of this package is to be a set of utility methods for HTTP caching. There are better alternatives like https://github.com/ascorbic/cache-primitives for that. I think the main use case is to provide a set of utility methods for working with the Netlify cache, and in that sense I think netlifyCacheHeaders is a bit redundant, as we'd also need to prefix setCacheHeaders (which we're currently not documenting) and potentially others, making the names more verbose and clunkier.

@@ -1,5 +1,5 @@
export { caches } from './polyfill.js'
export { setCacheHeaders } from './cache-headers/cache-headers.js'
export { cacheHeaders, setCacheHeaders } from './cache-headers/cache-headers.js'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export { cacheHeaders, setCacheHeaders } from './cache-headers/cache-headers.js'
export { cacheHeaders as netlifyCacheHeaders, setCacheHeaders } from './cache-headers/cache-headers.js'

@eduardoboucas eduardoboucas merged commit 54eda15 into main Sep 5, 2025
17 checks passed
@eduardoboucas eduardoboucas deleted the feat/expose-cacheheaders branch September 5, 2025 10:24
@token-generator-app token-generator-app bot mentioned this pull request Sep 5, 2025
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.

3 participants