Skip to content
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

refactor: remove unnecessary handling of Durable Cache feature flag after it was fully rolled out #2571

Merged
merged 2 commits into from
Aug 21, 2024
Merged
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
4 changes: 1 addition & 3 deletions src/run/handlers/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ export default async (request: Request, context: FutureContext) => {

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

const useDurableCache =
context.flags.get('serverless_functions_nextjs_durable_cache_disable') !== true
setCacheControlHeaders(response.headers, request, requestContext, useDurableCache)
setCacheControlHeaders(response.headers, request, requestContext)
setCacheTagsHeaders(response.headers, requestContext)
setVaryHeaders(response.headers, request, nextConfig)
setCacheStatusHeader(response.headers)
Expand Down
124 changes: 17 additions & 107 deletions src/run/headers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,96 +194,6 @@ describe('headers', () => {
describe('setCacheControlHeaders', () => {
const defaultUrl = 'https://example.com'

describe('Durable Cache feature flag disabled', () => {
test('should set permanent, non-durable "netlify-cdn-cache-control" if "cache-control" is not set and "requestContext.usedFsRead" is truthy', () => {
const headers = new Headers()
const request = new Request(defaultUrl)
vi.spyOn(headers, 'set')

const requestContext = createRequestContext()
requestContext.usedFsRead = true

setCacheControlHeaders(headers, request, requestContext, false)

expect(headers.set).toHaveBeenNthCalledWith(
1,
'cache-control',
'public, max-age=0, must-revalidate',
)
expect(headers.set).toHaveBeenNthCalledWith(
2,
'netlify-cdn-cache-control',
'max-age=31536000',
)
})

describe('route handler responses with a specified `revalidate` value', () => {
test('should set non-durable SWC=1yr with 1yr TTL if "{netlify-,}cdn-cache-control" is not present and `revalidate` is `false` (GET)', () => {
const headers = new Headers()
const request = new Request(defaultUrl)
vi.spyOn(headers, 'set')

const ctx: RequestContext = { ...createRequestContext(), routeHandlerRevalidate: false }
setCacheControlHeaders(headers, request, ctx, false)

expect(headers.set).toHaveBeenCalledTimes(1)
expect(headers.set).toHaveBeenNthCalledWith(
1,
'netlify-cdn-cache-control',
's-maxage=31536000, stale-while-revalidate=31536000',
)
})

test('should set non-durable SWC=1yr with 1yr TTL if "{netlify-,}cdn-cache-control" is not present and `revalidate` is `false` (HEAD)', () => {
const headers = new Headers()
const request = new Request(defaultUrl, { method: 'HEAD' })
vi.spyOn(headers, 'set')

const ctx: RequestContext = { ...createRequestContext(), routeHandlerRevalidate: false }
setCacheControlHeaders(headers, request, ctx, false)

expect(headers.set).toHaveBeenCalledTimes(1)
expect(headers.set).toHaveBeenNthCalledWith(
1,
'netlify-cdn-cache-control',
's-maxage=31536000, stale-while-revalidate=31536000',
)
})

test('should set non-durable SWC=1yr with given TTL if "{netlify-,}cdn-cache-control" is not present and `revalidate` is a number (GET)', () => {
const headers = new Headers()
const request = new Request(defaultUrl)
vi.spyOn(headers, 'set')

const ctx: RequestContext = { ...createRequestContext(), routeHandlerRevalidate: 7200 }
setCacheControlHeaders(headers, request, ctx, false)

expect(headers.set).toHaveBeenCalledTimes(1)
expect(headers.set).toHaveBeenNthCalledWith(
1,
'netlify-cdn-cache-control',
's-maxage=7200, stale-while-revalidate=31536000',
)
})

test('should set non-durable SWC=1yr with 1yr TTL if "{netlify-,}cdn-cache-control" is not present and `revalidate` is a number (HEAD)', () => {
const headers = new Headers()
const request = new Request(defaultUrl, { method: 'HEAD' })
vi.spyOn(headers, 'set')

const ctx: RequestContext = { ...createRequestContext(), routeHandlerRevalidate: 7200 }
setCacheControlHeaders(headers, request, ctx, false)

expect(headers.set).toHaveBeenCalledTimes(1)
expect(headers.set).toHaveBeenNthCalledWith(
1,
'netlify-cdn-cache-control',
's-maxage=7200, stale-while-revalidate=31536000',
)
})
})
})

describe('route handler responses with a specified `revalidate` value', () => {
test('should not set any headers if "cdn-cache-control" is present', () => {
const givenHeaders = {
Expand All @@ -294,7 +204,7 @@ describe('headers', () => {
vi.spyOn(headers, 'set')

const ctx: RequestContext = { ...createRequestContext(), routeHandlerRevalidate: false }
setCacheControlHeaders(headers, request, ctx, true)
setCacheControlHeaders(headers, request, ctx)

expect(headers.set).toHaveBeenCalledTimes(0)
})
Expand All @@ -308,7 +218,7 @@ describe('headers', () => {
vi.spyOn(headers, 'set')

const ctx: RequestContext = { ...createRequestContext(), routeHandlerRevalidate: false }
setCacheControlHeaders(headers, request, ctx, true)
setCacheControlHeaders(headers, request, ctx)

expect(headers.set).toHaveBeenCalledTimes(0)
})
Expand All @@ -322,7 +232,7 @@ describe('headers', () => {
vi.spyOn(headers, 'set')

const ctx: RequestContext = { ...createRequestContext(), routeHandlerRevalidate: false }
setCacheControlHeaders(headers, request, ctx, true)
setCacheControlHeaders(headers, request, ctx)

expect(headers.set).toHaveBeenCalledTimes(1)
expect(headers.set).toHaveBeenNthCalledWith(
Expand All @@ -341,7 +251,7 @@ describe('headers', () => {
vi.spyOn(headers, 'set')

const ctx: RequestContext = { ...createRequestContext(), routeHandlerRevalidate: false }
setCacheControlHeaders(headers, request, ctx, true)
setCacheControlHeaders(headers, request, ctx)

expect(headers.set).toHaveBeenCalledTimes(1)
expect(headers.set).toHaveBeenNthCalledWith(
Expand All @@ -357,7 +267,7 @@ describe('headers', () => {
vi.spyOn(headers, 'set')

const ctx: RequestContext = { ...createRequestContext(), routeHandlerRevalidate: false }
setCacheControlHeaders(headers, request, ctx, true)
setCacheControlHeaders(headers, request, ctx)

expect(headers.set).toHaveBeenCalledTimes(1)
expect(headers.set).toHaveBeenNthCalledWith(
Expand All @@ -373,7 +283,7 @@ describe('headers', () => {
vi.spyOn(headers, 'set')

const ctx: RequestContext = { ...createRequestContext(), routeHandlerRevalidate: 7200 }
setCacheControlHeaders(headers, request, ctx, true)
setCacheControlHeaders(headers, request, ctx)

expect(headers.set).toHaveBeenCalledTimes(1)
expect(headers.set).toHaveBeenNthCalledWith(
Expand All @@ -389,7 +299,7 @@ describe('headers', () => {
vi.spyOn(headers, 'set')

const ctx: RequestContext = { ...createRequestContext(), routeHandlerRevalidate: 7200 }
setCacheControlHeaders(headers, request, ctx, true)
setCacheControlHeaders(headers, request, ctx)

expect(headers.set).toHaveBeenCalledTimes(1)
expect(headers.set).toHaveBeenNthCalledWith(
Expand All @@ -405,7 +315,7 @@ describe('headers', () => {
vi.spyOn(headers, 'set')

const ctx: RequestContext = { ...createRequestContext(), routeHandlerRevalidate: false }
setCacheControlHeaders(headers, request, ctx, true)
setCacheControlHeaders(headers, request, ctx)

expect(headers.set).toHaveBeenCalledTimes(0)
})
Expand All @@ -416,7 +326,7 @@ describe('headers', () => {
const request = new Request(defaultUrl)
vi.spyOn(headers, 'set')

setCacheControlHeaders(headers, request, createRequestContext(), true)
setCacheControlHeaders(headers, request, createRequestContext())

expect(headers.set).toHaveBeenCalledTimes(0)
})
Expand Down Expand Up @@ -452,7 +362,7 @@ describe('headers', () => {
const request = new Request(defaultUrl)
vi.spyOn(headers, 'set')

setCacheControlHeaders(headers, request, createRequestContext(), true)
setCacheControlHeaders(headers, request, createRequestContext())

expect(headers.set).toHaveBeenCalledTimes(0)
})
Expand All @@ -466,7 +376,7 @@ describe('headers', () => {
const request = new Request(defaultUrl)
vi.spyOn(headers, 'set')

setCacheControlHeaders(headers, request, createRequestContext(), true)
setCacheControlHeaders(headers, request, createRequestContext())

expect(headers.set).toHaveBeenCalledTimes(0)
})
Expand All @@ -479,7 +389,7 @@ describe('headers', () => {
const request = new Request(defaultUrl)
vi.spyOn(headers, 'set')

setCacheControlHeaders(headers, request, createRequestContext(), true)
setCacheControlHeaders(headers, request, createRequestContext())

expect(headers.set).toHaveBeenNthCalledWith(
1,
Expand All @@ -501,7 +411,7 @@ describe('headers', () => {
const request = new Request(defaultUrl, { method: 'HEAD' })
vi.spyOn(headers, 'set')

setCacheControlHeaders(headers, request, createRequestContext(), true)
setCacheControlHeaders(headers, request, createRequestContext())

expect(headers.set).toHaveBeenNthCalledWith(
1,
Expand All @@ -523,7 +433,7 @@ describe('headers', () => {
const request = new Request(defaultUrl, { method: 'POST' })
vi.spyOn(headers, 'set')

setCacheControlHeaders(headers, request, createRequestContext(), true)
setCacheControlHeaders(headers, request, createRequestContext())

expect(headers.set).toHaveBeenCalledTimes(0)
})
Expand All @@ -536,7 +446,7 @@ describe('headers', () => {
const request = new Request(defaultUrl)
vi.spyOn(headers, 'set')

setCacheControlHeaders(headers, request, createRequestContext(), true)
setCacheControlHeaders(headers, request, createRequestContext())

expect(headers.set).toHaveBeenNthCalledWith(1, 'cache-control', 'public')
expect(headers.set).toHaveBeenNthCalledWith(
Expand All @@ -554,7 +464,7 @@ describe('headers', () => {
const request = new Request(defaultUrl)
vi.spyOn(headers, 'set')

setCacheControlHeaders(headers, request, createRequestContext(), true)
setCacheControlHeaders(headers, request, createRequestContext())

expect(headers.set).toHaveBeenNthCalledWith(1, 'cache-control', 'max-age=604800')
expect(headers.set).toHaveBeenNthCalledWith(
Expand All @@ -572,7 +482,7 @@ describe('headers', () => {
const request = new Request(defaultUrl)
vi.spyOn(headers, 'set')

setCacheControlHeaders(headers, request, createRequestContext(), true)
setCacheControlHeaders(headers, request, createRequestContext())

expect(headers.set).toHaveBeenNthCalledWith(
1,
Expand Down
8 changes: 3 additions & 5 deletions src/run/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,7 @@ export const setCacheControlHeaders = (
headers: Headers,
request: Request,
requestContext: RequestContext,
useDurableCache: boolean,
) => {
const durableCacheDirective = useDurableCache ? ', durable' : ''
if (
typeof requestContext.routeHandlerRevalidate !== 'undefined' &&
['GET', 'HEAD'].includes(request.method) &&
Expand All @@ -227,7 +225,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${durableCacheDirective}`
: `s-maxage=${requestContext.routeHandlerRevalidate === false ? 31536000 : requestContext.routeHandlerRevalidate}, stale-while-revalidate=31536000, durable`

headers.set('netlify-cdn-cache-control', cdnCacheControl)
return
Expand All @@ -253,7 +251,7 @@ export const setCacheControlHeaders = (
...getHeaderValueArray(cacheControl).map((value) =>
value === 'stale-while-revalidate' ? 'stale-while-revalidate=31536000' : value,
),
...(useDurableCache ? ['durable'] : []),
'durable',
].join(', ')

headers.set('cache-control', browserCacheControl || 'public, max-age=0, must-revalidate')
Expand All @@ -269,7 +267,7 @@ export const setCacheControlHeaders = (
) {
// handle CDN Cache Control on static files
headers.set('cache-control', 'public, max-age=0, must-revalidate')
headers.set('netlify-cdn-cache-control', `max-age=31536000${durableCacheDirective}`)
headers.set('netlify-cdn-cache-control', `max-age=31536000, durable`)
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/cli-before-regional-blobs-support.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test('should serve 404 page when requesting non existing page (no matching route
expect(await page.textContent('h1')).toBe('404')

expect(headers['netlify-cdn-cache-control']).toBe(
'no-cache, no-store, max-age=0, must-revalidate',
'no-cache, no-store, max-age=0, must-revalidate, durable',
)
expect(headers['cache-control']).toBe('no-cache,no-store,max-age=0,must-revalidate')
})
19 changes: 0 additions & 19 deletions tests/e2e/durable-cache.test.ts

This file was deleted.

8 changes: 4 additions & 4 deletions tests/e2e/on-demand-app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ test.describe('app router on-demand revalidation', () => {
expect(response1?.status()).toBe(200)
expect(headers1['x-nextjs-cache']).toBeUndefined()
expect(headers1['netlify-cdn-cache-control']).toBe(
's-maxage=31536000, stale-while-revalidate=31536000',
's-maxage=31536000, stale-while-revalidate=31536000, durable',
)

const date1 = await page.textContent('[data-testid="date-now"]')
Expand Down Expand Up @@ -104,7 +104,7 @@ test.describe('app router on-demand revalidation', () => {
expect(headers2['cache-status']).toMatch(/"Next.js"; hit/m)
}
expect(headers2['netlify-cdn-cache-control']).toBe(
's-maxage=31536000, stale-while-revalidate=31536000',
's-maxage=31536000, stale-while-revalidate=31536000, durable',
)

// the page is cached
Expand Down Expand Up @@ -134,7 +134,7 @@ test.describe('app router on-demand revalidation', () => {
expect(response3?.status()).toBe(200)
expect(headers3?.['x-nextjs-cache']).toBeUndefined()
expect(headers3['netlify-cdn-cache-control']).toBe(
's-maxage=31536000, stale-while-revalidate=31536000',
's-maxage=31536000, stale-while-revalidate=31536000, durable',
)

// the page has now an updated date
Expand All @@ -161,7 +161,7 @@ test.describe('app router on-demand revalidation', () => {
expect(headers4['cache-status']).toMatch(/"Next.js"; hit/m)
}
expect(headers4['netlify-cdn-cache-control']).toBe(
's-maxage=31536000, stale-while-revalidate=31536000',
's-maxage=31536000, stale-while-revalidate=31536000, durable',
)

// the page is cached
Expand Down
Loading
Loading