Skip to content

fix: ISR 404 pages should be cached ephemerally #2233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions packages/runtime/src/templates/getHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,24 @@ const makeHandler = ({ conf, app, pageRoot, NextServer, staticManifest = [], mod
// ODBs currently have a minimum TTL of 60 seconds
result.ttl = Math.max(ttl, 60)
}
const ephemeralCodes = [301, 302, 307, 308, 404]
const ephemeralCodes = [301, 302, 307, 308]
if (ttl === ONE_YEAR_IN_SECONDS && ephemeralCodes.includes(result.statusCode)) {
// Only cache for 60s if default TTL provided
result.ttl = 60
}
if (result.ttl > 0) {
requestMode = `odb ttl=${result.ttl}`
}
}
multiValueHeaders['cache-control'] = ['public, max-age=0, must-revalidate']
}

// ISR 404s are not served with SWR headers so we need to set the TTL here
if (requestMode === 'odb' && result.statusCode === 404) {
result.ttl = 60
}

if (result.ttl > 0) {
requestMode = `odb ttl=${result.ttl}`
}

multiValueHeaders['x-nf-render-mode'] = [requestMode]

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