Skip to content

Commit beec5f5

Browse files
committed
fix: wip - fix for caching issues on catch all routes
1 parent d278f66 commit beec5f5

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

src/run/handlers/cache.cts

+3-2
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,12 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
121121
}
122122

123123
private captureCacheTags(cacheValue: NetlifyIncrementalCacheValue | null, key: string) {
124+
const requestContext = getRequestContext()
125+
124126
if (!cacheValue) {
125127
return
126128
}
127129

128-
const requestContext = getRequestContext()
129130
// Bail if we can't get request context
130131
if (!requestContext) {
131132
return
@@ -393,7 +394,7 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
393394

394395
await this.cacheStore.set(key, { lastModified, value }, 'blobStore.set')
395396

396-
if (data?.kind === 'PAGE' || data?.kind === 'PAGES') {
397+
if (!data || data?.kind === 'PAGE' || data?.kind === 'PAGES') {
397398
const requestContext = getRequestContext()
398399
if (requestContext?.didPagesRouterOnDemandRevalidate) {
399400
// encode here to deal with non ASCII characters in the key

src/run/handlers/server.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export default async (
132132
}
133133

134134
setCacheControlHeaders(response, request, requestContext, nextConfig)
135-
setCacheTagsHeaders(response.headers, requestContext)
135+
setCacheTagsHeaders(response.headers, request, requestContext)
136136
setVaryHeaders(response.headers, request, nextConfig)
137137
setCacheStatusHeader(response.headers, nextCache)
138138

src/run/headers.ts

+19-6
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ export const setCacheControlHeaders = (
220220
.log('NetlifyHeadersHandler.trailingSlashRedirect')
221221
}
222222

223-
const cacheControl = headers.get('cache-control')
224223
if (status === 404) {
225224
if (request.url.endsWith('.php')) {
226225
// temporary CDN Cache Control handling for bot probes on PHP files
@@ -241,6 +240,8 @@ export const setCacheControlHeaders = (
241240
}
242241
}
243242

243+
const cacheControl = headers.get('cache-control')
244+
244245
if (
245246
cacheControl !== null &&
246247
['GET', 'HEAD'].includes(request.method) &&
@@ -273,6 +274,7 @@ export const setCacheControlHeaders = (
273274
['GET', 'HEAD'].includes(request.method) &&
274275
!headers.has('cdn-cache-control') &&
275276
!headers.has('netlify-cdn-cache-control') &&
277+
!new URL(request.url).pathname.startsWith('/api/') &&
276278
requestContext.usedFsReadForNonFallback
277279
) {
278280
// handle CDN Cache Control on static files
@@ -281,13 +283,24 @@ export const setCacheControlHeaders = (
281283
}
282284
}
283285

284-
export const setCacheTagsHeaders = (headers: Headers, requestContext: RequestContext) => {
285-
if (
286-
requestContext.responseCacheTags &&
287-
(headers.has('cache-control') || headers.has('netlify-cdn-cache-control'))
288-
) {
286+
export const setCacheTagsHeaders = (
287+
headers: Headers,
288+
request: Request,
289+
requestContext: RequestContext,
290+
) => {
291+
if (!headers.has('cache-control') && !headers.has('netlify-cdn-cache-control')) {
292+
return
293+
}
294+
295+
if (requestContext.responseCacheTags) {
289296
headers.set('netlify-cache-tag', requestContext.responseCacheTags.join(','))
297+
return
290298
}
299+
300+
const key = new URL(request.url).pathname
301+
const cacheTag = `_N_T_${key === '/index' ? '/' : encodeURI(key)}`
302+
console.log('setCacheTagsHeaders', 'netlify-cache-tag', key)
303+
headers.set('netlify-cache-tag', cacheTag)
291304
}
292305

293306
/**

0 commit comments

Comments
 (0)