Skip to content

Configurable CDN cache TTLs + purge-on-publish#746

Open
jamiefolsom wants to merge 2 commits into
developfrom
feat/cache-ttl-purge
Open

Configurable CDN cache TTLs + purge-on-publish#746
jamiefolsom wants to merge 2 commits into
developfrom
feat/cache-ttl-purge

Conversation

@jamiefolsom

Copy link
Copy Markdown
Member

Closes #744.

Two changes, both inert until a site opts in:

  1. Configurable TTLs. CACHE_CDN_MAX_AGE and CACHE_STALE_WHILE_REVALIDATE env vars feed the Netlify-CDN-Cache-Control header, defaulting to the current hardcoded values (300 / 604800) — with no env vars set, the emitted headers are byte-identical to today's. Browser-facing Cache-Control is unchanged.

  2. Purge-on-publish. Cached responses now carry a Netlify-Cache-Tag, and TinaCMS saves/deletes call purgeCache for that tag after the GitHub write (tina/git-provider.ts), so a publish is visible in seconds regardless of TTL. A failed purge logs and never fails the save; local dev never purges (GitHubProvider only exists when not local). This is what makes a long TTL safe: the plan is to trial CACHE_CDN_MAX_AGE=86400 on one production site and measure against the baselines taken today.

Record-detail islands set no cache headers, so live FairData content is unaffected by any TTL.

Tests: new test/cacheControl.test.ts (defaults byte-identical, env overrides, tag present, DISABLE_CACHE short-circuit); full vitest suite green (69 passed).

One thing to validate on staging before any site opts in: whether purgeCache picks up an ambient token in our v1-style function; if it 401s, the site needs NETLIFY_PURGE_API_TOKEN set (will document either way).

- CACHE_CDN_MAX_AGE / CACHE_STALE_WHILE_REVALIDATE env vars, defaulting to
  the current hardcoded values (300 / 604800) so no site changes behavior
  without opting in
- Netlify-Cache-Tag on cached responses; TinaCMS saves and deletes purge
  the tag via purgeCache, so publishes are visible immediately regardless
  of TTL; a failed purge never fails the save
- tests for header defaults, env overrides, tag, and DISABLE_CACHE
@netlify

netlify Bot commented Jul 13, 2026

Copy link
Copy Markdown

Deploy Preview for padp-staging ready!

Name Link
🔨 Latest commit 459c3b6
🔍 Latest deploy log https://app.netlify.com/projects/padp-staging/deploys/6a554803230b8000086bbba8
😎 Deploy Preview https://deploy-preview-746--padp-staging.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Jul 13, 2026

Copy link
Copy Markdown

Deploy Preview for juel-ancestry ready!

Name Link
🔨 Latest commit 459c3b6
🔍 Latest deploy log https://app.netlify.com/projects/juel-ancestry/deploys/6a554803d6d75f000885079c
😎 Deploy Preview https://deploy-preview-746--juel-ancestry.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Jul 13, 2026

Copy link
Copy Markdown

Deploy Preview for marronage-staging ready!

Name Link
🔨 Latest commit 459c3b6
🔍 Latest deploy log https://app.netlify.com/projects/marronage-staging/deploys/6a5548036059d00008aaa84a
😎 Deploy Preview https://deploy-preview-746--marronage-staging.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Jul 13, 2026

Copy link
Copy Markdown

Deploy Preview for pss-scavenger-hunt ready!

Name Link
🔨 Latest commit 459c3b6
🔍 Latest deploy log https://app.netlify.com/projects/pss-scavenger-hunt/deploys/6a554803b326b20008552431
😎 Deploy Preview https://deploy-preview-746--pss-scavenger-hunt.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Jul 13, 2026

Copy link
Copy Markdown

Deploy Preview for juel-life ready!

Name Link
🔨 Latest commit 459c3b6
🔍 Latest deploy log https://app.netlify.com/projects/juel-life/deploys/6a5548030cbd8a0008dd159b
😎 Deploy Preview https://deploy-preview-746--juel-life.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Jul 13, 2026

Copy link
Copy Markdown

Deploy Preview for juel-staging ready!

Name Link
🔨 Latest commit 459c3b6
🔍 Latest deploy log https://app.netlify.com/projects/juel-staging/deploys/6a5548030919060008747e02
😎 Deploy Preview https://deploy-preview-746--juel-staging.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Jul 13, 2026

Copy link
Copy Markdown

Deploy Preview for relnet-staging ready!

Name Link
🔨 Latest commit 459c3b6
🔍 Latest deploy log https://app.netlify.com/projects/relnet-staging/deploys/6a5548030cbd8a0008dd159d
😎 Deploy Preview https://deploy-preview-746--relnet-staging.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@jamiefolsom

jamiefolsom commented Jul 13, 2026

Copy link
Copy Markdown
Member Author

We tested this on the padp-staging deploy preview, against the published develop deploy as a baseline. Staging previously had DISABLE_CACHE=true, so caching was off entirely; we removed that first so the baseline matches production's normal five-minute caching.

While the cache is fresh, the two behave the same: repeat requests come back from the CDN in under 100 ms. The difference shows up after five minutes. The baseline's cached copy expired on schedule and requests went back to the server. The preview, built with CACHE_CDN_MAX_AGE=86400, was still served from the CDN a few minutes later. The longer cache lifetime works.

One part can only be tested after this merges: clearing the cache when an editor publishes. The cache-clearing API only accepts a token that Netlify provides to deployed functions, not personal tokens, so I couldn't call it from outside. Once staging's published deploy includes this change, saving a page in TinaCMS will show whether the cache clears right away, and whether our function receives that token automatically or the site needs it set by hand.

@jamiefolsom
jamiefolsom requested a review from ajolipa July 14, 2026 17:17
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.

Pages are CDN-cached for only 5 minutes, and publishing content doesn't invalidate the cache

1 participant