Skip to content

feat: add central cache directive #2502

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
flags: {
Copy link
Member Author

Choose a reason for hiding this comment

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

This is not meant to be a customer-facing property, so we never added it to the Context types. I've added it here for now, but we should consider having a InternalContext or similar that extends the main context with any internal properties.

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense. Might be worth adding the reason for putting this here to the comment above, until InternalContext is implemented

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