From be895926efd575b65bb626b2d2b8d015b8c10e28 Mon Sep 17 00:00:00 2001 From: Rijk van Zanten Date: Wed, 17 Apr 2024 17:01:09 -0400 Subject: [PATCH] Revert "Don't influence Cache-Control through `CACHE_AUTO_PURGE`" (#22235) * Revert "Don't influence Cache-Control through `CACHE_AUTO_PURGE` (#22203)" This reverts commit 12fb7719fe2ee7c21f591c13962610bf0a1e39e6. * Add changeset --- .changeset/breezy-crabs-yell.md | 5 +++ api/src/utils/get-cache-headers.test.ts | 44 +++++++++++++++++++++++++ api/src/utils/get-cache-headers.ts | 3 ++ 3 files changed, 52 insertions(+) create mode 100644 .changeset/breezy-crabs-yell.md diff --git a/.changeset/breezy-crabs-yell.md b/.changeset/breezy-crabs-yell.md new file mode 100644 index 0000000000..d06235e625 --- /dev/null +++ b/.changeset/breezy-crabs-yell.md @@ -0,0 +1,5 @@ +--- +'@directus/api': patch +--- + +Reverted cache-control header change to prevent cache inconsistencies in-app diff --git a/api/src/utils/get-cache-headers.test.ts b/api/src/utils/get-cache-headers.test.ts index 92642f6f2c..9eeadee1f4 100644 --- a/api/src/utils/get-cache-headers.test.ts +++ b/api/src/utils/get-cache-headers.test.ts @@ -69,6 +69,50 @@ const scenarios = [ output: 'max-age=0', }, + // Test CACHE_AUTO_PURGE env for no-cache + { + name: 'when CACHE_AUTO_PURGE is true and globalCacheSettings is true', + input: { + env: { + CACHE_AUTO_PURGE: true, + }, + headers: {}, + accountability: null, + ttl: 5678910, + globalCacheSettings: true, + personalized: false, + }, + output: 'no-cache', + }, + { + name: 'when CACHE_AUTO_PURGE is true and globalCacheSettings is false', + input: { + env: { + CACHE_AUTO_PURGE: true, + }, + headers: {}, + accountability: null, + ttl: 5678910, + globalCacheSettings: false, + personalized: false, + }, + output: 'max-age=5679', + }, + { + name: 'when CACHE_AUTO_PURGE is false and globalCacheSettings is true', + input: { + env: { + CACHE_AUTO_PURGE: false, + }, + headers: {}, + accountability: null, + ttl: 5678910, + globalCacheSettings: true, + personalized: false, + }, + output: 'max-age=5679', + }, + // Test personalized { name: 'when personalized is true and accountability is null', diff --git a/api/src/utils/get-cache-headers.ts b/api/src/utils/get-cache-headers.ts index ae254fdf41..98838382df 100644 --- a/api/src/utils/get-cache-headers.ts +++ b/api/src/utils/get-cache-headers.ts @@ -24,6 +24,9 @@ export function getCacheControlHeader( // When the resource / current request shouldn't be cached if (ttl === undefined || ttl < 0) return 'no-cache'; + // When the API cache can invalidate at any moment + if (globalCacheSettings && env['CACHE_AUTO_PURGE'] === true) return 'no-cache'; + const headerValues = []; // When caching depends on the authentication status of the users