Skip to content

Commit d7aad8a

Browse files
fix: ISR 404 pages should be cached ephemerally (#2233)
* fix: ensure isr 404 pages are only cached for 60s * chore: refactor isr 404 code under main SWR logic * chore: remove unnecessary 404 code from list of ephemeral codes --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 5eb3dc7 commit d7aad8a

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

packages/runtime/src/templates/getHandler.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,24 @@ const makeHandler = ({ conf, app, pageRoot, NextServer, staticManifest = [], mod
171171
// ODBs currently have a minimum TTL of 60 seconds
172172
result.ttl = Math.max(ttl, 60)
173173
}
174-
const ephemeralCodes = [301, 302, 307, 308, 404]
174+
const ephemeralCodes = [301, 302, 307, 308]
175175
if (ttl === ONE_YEAR_IN_SECONDS && ephemeralCodes.includes(result.statusCode)) {
176176
// Only cache for 60s if default TTL provided
177177
result.ttl = 60
178178
}
179-
if (result.ttl > 0) {
180-
requestMode = `odb ttl=${result.ttl}`
181-
}
182179
}
183180
multiValueHeaders['cache-control'] = ['public, max-age=0, must-revalidate']
184181
}
182+
183+
// ISR 404s are not served with SWR headers so we need to set the TTL here
184+
if (requestMode === 'odb' && result.statusCode === 404) {
185+
result.ttl = 60
186+
}
187+
188+
if (result.ttl > 0) {
189+
requestMode = `odb ttl=${result.ttl}`
190+
}
191+
185192
multiValueHeaders['x-nf-render-mode'] = [requestMode]
186193

187194
console.log(`[${event.httpMethod}] ${event.path} (${requestMode?.toUpperCase()})`)

0 commit comments

Comments
 (0)