Skip to content

Commit

Permalink
feat: add central cache directive
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoboucas committed Jun 20, 2024
1 parent 6899682 commit c8621b3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/run/handlers/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ const disableFaultyTransferEncodingHandling = (res: ComputeJsOutgoingMessage) =>
// TODO: remove once https://github.com/netlify/serverless-functions-api/pull/219
// is released and public types are updated
interface FutureContext extends Context {

Check failure on line 50 in src/run/handlers/server.ts

View workflow job for this annotation

GitHub Actions / Lint

Interface 'FutureContext' incorrectly extends interface '{ account: { id: string; }; cookies: Cookies; deploy: { id: string; }; flags: Flags; geo: Geo; ip: string; json: (input: unknown) => Response; log: { (...data: any[]): void; (message?: any, ...optionalParams: any[]): void; }; ... 5 more ...; site: Site; }'.
flags: {
get: (name: string) => boolean
},
waitUntil?: (promise: Promise<unknown>) => void
}

Expand Down Expand Up @@ -123,7 +126,9 @@ export default async (request: Request, context: FutureContext) => {

await adjustDateHeader({ headers: response.headers, request, span, tracer, requestContext })

setCacheControlHeaders(response.headers, request, requestContext)
const useCentralCache = context.flags.get("serverless_functions_nextjs_central_cache")

setCacheControlHeaders(response.headers, request, requestContext, useCentralCache)
setCacheTagsHeaders(response.headers, request, tagsManifest, requestContext)
setVaryHeaders(response.headers, request, nextConfig)
setCacheStatusHeader(response.headers)
Expand Down
7 changes: 5 additions & 2 deletions src/run/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,10 @@ export const setCacheControlHeaders = (
headers: Headers,
request: Request,
requestContext: RequestContext,
useCentralCache: boolean
) => {
const centralCacheDirective = useCentralCache ? ', persist' : ''

if (
typeof requestContext.routeHandlerRevalidate !== 'undefined' &&
['GET', 'HEAD'].includes(request.method) &&
Expand All @@ -232,7 +235,7 @@ export const setCacheControlHeaders = (
// if we are serving already stale response, instruct edge to not attempt to cache that response
headers.get('x-nextjs-cache') === 'STALE'
? 'public, max-age=0, must-revalidate'
: `s-maxage=${requestContext.routeHandlerRevalidate === false ? 31536000 : requestContext.routeHandlerRevalidate}, stale-while-revalidate=31536000`
: `s-maxage=${requestContext.routeHandlerRevalidate === false ? 31536000 : requestContext.routeHandlerRevalidate}, stale-while-revalidate=31536000${centralCacheDirective}`

headers.set('netlify-cdn-cache-control', cdnCacheControl)
return
Expand All @@ -259,7 +262,7 @@ export const setCacheControlHeaders = (
)

headers.set('cache-control', browserCacheControl || 'public, max-age=0, must-revalidate')
headers.set('netlify-cdn-cache-control', cdnCacheControl)
headers.set('netlify-cdn-cache-control', cdnCacheControl + centralCacheDirective)
return
}

Expand Down

0 comments on commit c8621b3

Please sign in to comment.