Skip to content

Commit eafdf3d

Browse files
committed
fix: work in progress fix for caching issues on catch all routes
1 parent f3aa702 commit eafdf3d

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

src/run/handlers/cache.cts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,19 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
137137
}
138138

139139
private captureCacheTags(cacheValue: NetlifyIncrementalCacheValue | null, key: string) {
140+
const requestContext = getRequestContext()
141+
140142
if (!cacheValue) {
143+
if (requestContext?.responseCacheTags) {
144+
const cacheTags = [`_N_T_${key === '/index' ? '/' : encodeURI(key)}`]
145+
requestContext.responseCacheTags = cacheTags
146+
147+
console.log('cache tags set:', cacheTags)
148+
}
149+
141150
return
142151
}
143152

144-
const requestContext = getRequestContext()
145153
// Bail if we can't get request context
146154
if (!requestContext) {
147155
return
@@ -420,7 +428,7 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
420428
value,
421429
})
422430

423-
if (data?.kind === 'PAGE' || data?.kind === 'PAGES') {
431+
// if (data?.kind === 'PAGE' || data?.kind === 'PAGES') {
424432
const requestContext = getRequestContext()
425433
if (requestContext?.didPagesRouterOnDemandRevalidate) {
426434
// encode here to deal with non ASCII characters in the key
@@ -441,7 +449,7 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
441449
}),
442450
)
443451
}
444-
}
452+
// }
445453
})
446454
}
447455

src/run/handlers/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export default async (
133133
}
134134

135135
setCacheControlHeaders(response, request, requestContext, nextConfig)
136-
setCacheTagsHeaders(response.headers, requestContext)
136+
setCacheTagsHeaders(response.headers, request, requestContext)
137137
setVaryHeaders(response.headers, request, nextConfig)
138138
setCacheStatusHeader(response.headers, nextCache)
139139

src/run/headers.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ export const setCacheControlHeaders = (
239239
.log('NetlifyHeadersHandler.trailingSlashRedirect')
240240
}
241241

242-
const cacheControl = headers.get('cache-control')
243242
if (status === 404) {
244243
if (request.url.endsWith('.php')) {
245244
// temporary CDN Cache Control handling for bot probes on PHP files
@@ -260,6 +259,8 @@ export const setCacheControlHeaders = (
260259
}
261260
}
262261

262+
const cacheControl = headers.get('cache-control')
263+
263264
if (
264265
cacheControl !== null &&
265266
['GET', 'HEAD'].includes(request.method) &&
@@ -300,13 +301,20 @@ export const setCacheControlHeaders = (
300301
}
301302
}
302303

303-
export const setCacheTagsHeaders = (headers: Headers, requestContext: RequestContext) => {
304-
if (
305-
requestContext.responseCacheTags &&
306-
(headers.has('cache-control') || headers.has('netlify-cdn-cache-control'))
307-
) {
304+
export const setCacheTagsHeaders = (headers: Headers, request: Request, requestContext: RequestContext) => {
305+
if (!headers.has('cache-control') && !headers.has('netlify-cdn-cache-control')) {
306+
return
307+
}
308+
309+
if (requestContext.responseCacheTags) {
308310
headers.set('netlify-cache-tag', requestContext.responseCacheTags.join(','))
311+
return
309312
}
313+
314+
const key = new URL(request.url).pathname
315+
const cacheTag = `_N_T_${key === '/index' ? '/' : encodeURI(key)}`
316+
console.log('setCacheTagsHeaders', 'netlify-cache-tag', key)
317+
headers.set('netlify-cache-tag', cacheTag)
310318
}
311319

312320
/**

src/run/next.cts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ export async function getMockedRequestHandler(...args: Parameters<typeof getRequ
106106
if (!file.isFallback) {
107107
const requestContext = getRequestContext()
108108
if (requestContext) {
109-
requestContext.usedFsReadForNonFallback = true
109+
// Causing caching for too many requests (e.g. api routes)
110+
// requestContext.usedFsReadForNonFallback = true
110111
}
111112
}
112113

0 commit comments

Comments
 (0)