Skip to content

Commit 5a7aeb1

Browse files
committed
feat: cache 404s indefinitely for bot probes
1 parent 0ab5748 commit 5a7aeb1

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

src/run/handlers/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export default async (request: Request, context: FutureContext) => {
112112

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

115-
setCacheControlHeaders(response.headers, request, requestContext)
115+
setCacheControlHeaders(response.headers, response.status, request, requestContext)
116116
setCacheTagsHeaders(response.headers, requestContext)
117117
setVaryHeaders(response.headers, request, nextConfig)
118118
setCacheStatusHeader(response.headers)

src/run/headers.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ export const adjustDateHeader = async ({
211211
*/
212212
export const setCacheControlHeaders = (
213213
headers: Headers,
214+
status: number,
214215
request: Request,
215216
requestContext: RequestContext,
216217
) => {
@@ -231,6 +232,12 @@ export const setCacheControlHeaders = (
231232
return
232233
}
233234

235+
if (status === 404 && request.url.endsWith('.php')) {
236+
// temporary CDN Cache Control handling for bot probes
237+
headers.set('cache-control', 'public, max-age=0, must-revalidate')
238+
headers.set('netlify-cdn-cache-control', `max-age=31536000, durable`)
239+
}
240+
234241
const cacheControl = headers.get('cache-control')
235242
if (
236243
cacheControl !== null &&

tests/integration/simple-app.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ test<FixtureTestContext>('stale-while-revalidate headers should be normalized to
158158
)
159159
})
160160

161+
test<FixtureTestContext>('404 responses for PHP pages should be cached indefinitely', async (ctx) => {
162+
await createFixture('simple', ctx)
163+
await runPlugin(ctx)
164+
const index = await invokeFunction(ctx, { url: '/admin.php' })
165+
expect(index.headers?.['netlify-cdn-cache-control']).toContain('max-age=31536000, durable')
166+
})
167+
161168
test<FixtureTestContext>('handlers receive correct site domain', async (ctx) => {
162169
await createFixture('simple', ctx)
163170
await runPlugin(ctx)

0 commit comments

Comments
 (0)