Skip to content

Commit

Permalink
Merge pull request #14434 from artsy/propagate-zero-ttl
Browse files Browse the repository at this point in the history
feat: propagate client-side-cached-but-not-server custom 0 TTL to CDN
  • Loading branch information
joeyAghion authored Sep 4, 2024
2 parents fdb35eb + 1381368 commit 9f67f5c
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/System/Relay/middleware/cacheHeaderMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { isServer } from "Server/isServer"
import { findRoutesByPath } from "System/Router/Utils/routeUtils"

export const RELAY_CACHE_CONFIG_HEADER_KEY = "x-relay-cache-config"
export const RELAY_CACHE_PATH_HEADER_KEY = "x-relay-cache-path"
Expand All @@ -8,7 +9,7 @@ interface CacheHeaderMiddlewareProps {
user: User
}

export const shouldSkipCDNCache = (req, user) => {
export const shouldSkipCDNCache = (req, user, path) => {
const isLoggedIn = !!user

if (isLoggedIn) {
Expand All @@ -19,6 +20,13 @@ export const shouldSkipCDNCache = (req, user) => {
return true
}

if (path) {
const route = findRoutesByPath({ path })[0]
if (route?.serverCacheTTL === 0) {
return true
}
}

return false
}

Expand All @@ -41,7 +49,7 @@ export const cacheHeaderMiddleware = (props?: CacheHeaderMiddlewareProps) => {
cacheHeaders[RELAY_CACHE_PATH_HEADER_KEY] = url
}

const cacheControlHeader = shouldSkipCDNCache(req, props?.user)
const cacheControlHeader = shouldSkipCDNCache(req, props?.user, url)
? { "Cache-Control": "no-cache" }
: {}

Expand Down

0 comments on commit 9f67f5c

Please sign in to comment.