Skip to content

Commit dff05b2

Browse files
committed
chore: refactor odb ttl handling to avoid patch
1 parent 3402c0f commit dff05b2

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

packages/runtime/src/helpers/files.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,6 @@ const baseServerReplacements: Array<[string, string]> = [
365365
`checkIsManualRevalidate(req, this.renderOpts.previewProps)`,
366366
`checkIsManualRevalidate(process.env._REVALIDATE_SSG ? { headers: { 'x-prerender-revalidate': this.renderOpts.previewProps.previewModeId } } : req, this.renderOpts.previewProps)`,
367367
],
368-
// ensure ISR 404 pages send the correct SWR cache headers
369-
[`private: isPreviewMode || is404Page`, `private: isPreviewMode || false`],
370368
]
371369

372370
const nextServerReplacements: Array<[string, string]> = [

packages/runtime/src/templates/getHandler.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -130,26 +130,26 @@ const makeHandler = (conf: NextConfig, app, pageRoot, staticManifest: Array<[str
130130

131131
// Sending SWR headers causes undefined behaviour with the Netlify CDN
132132
const cacheHeader = multiValueHeaders['cache-control']?.[0]
133+
const ttl = getMaxAge(cacheHeader)
133134

134135
if (cacheHeader?.includes('stale-while-revalidate')) {
135-
if (requestMode === 'odb') {
136-
const ttl = getMaxAge(cacheHeader)
137-
// Long-expiry TTL is basically no TTL, so we'll skip it
138-
if (ttl > 0 && ttl < ONE_YEAR_IN_SECONDS) {
139-
// ODBs currently have a minimum TTL of 60 seconds
140-
result.ttl = Math.max(ttl, 60)
141-
}
142-
const ephemeralCodes = [301, 302, 307, 308, 404]
143-
if (ttl === ONE_YEAR_IN_SECONDS && ephemeralCodes.includes(result.statusCode)) {
144-
// Only cache for 60s if default TTL provided
145-
result.ttl = 60
146-
}
147-
if (result.ttl > 0) {
148-
requestMode = `odb ttl=${result.ttl}`
149-
}
136+
// Long-expiry TTL is basically no TTL, so we'll skip it
137+
if (requestMode === 'odb' && ttl > 0 && ttl < ONE_YEAR_IN_SECONDS) {
138+
// ODBs currently have a minimum TTL of 60 seconds
139+
result.ttl = Math.max(ttl, 60)
150140
}
151141
multiValueHeaders['cache-control'] = ['public, max-age=0, must-revalidate']
152142
}
143+
144+
// We don't want to cache not found or redirects for ever
145+
const ephemeralCodes = [301, 302, 307, 308, 404]
146+
if (ephemeralCodes.includes(result.statusCode) && requestMode === 'odb' && result.ttl === undefined) {
147+
result.ttl = 60
148+
}
149+
150+
if (result.ttl > 0) {
151+
requestMode = `odb ttl=${result.ttl}`
152+
}
153153
multiValueHeaders['x-nf-render-mode'] = [requestMode]
154154

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

0 commit comments

Comments
 (0)