Skip to content
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

Only cache successful fetches #48033

Merged
merged 8 commits into from Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next/src/server/lib/incremental-cache/index.ts
Expand Up @@ -155,7 +155,7 @@ export class IncrementalCache {
): Promise<string> {
// this should be bumped anytime a fix is made to cache entries
// that should bust the cache
const MAIN_KEY_PREFIX = 'v1'
const MAIN_KEY_PREFIX = 'v2'

let cacheKey: string
const bodyChunks: string[] = []
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/server/lib/patch-fetch.ts
Expand Up @@ -225,6 +225,7 @@ export function patchFetch({
const doOriginalFetch = async () => {
return originFetch(input, init).then(async (res) => {
if (
res.ok &&
staticGenerationStore.incrementalCache &&
cacheKey &&
typeof revalidate === 'number' &&
Expand Down
20 changes: 5 additions & 15 deletions test/e2e/app-dir/app-static/app-static.test.ts
Expand Up @@ -168,19 +168,16 @@ createNextDescribe(
})
}

it('should include statusCode in cache', async () => {
it('should not cache non-ok statusCode', async () => {
const $ = await next.render$('/variable-revalidate/status-code')
const origData = JSON.parse($('#page-data').text())

expect(origData.status).toBe(404)

await check(async () => {
const new$ = await next.render$('/variable-revalidate/status-code')
const newData = JSON.parse(new$('#page-data').text())
expect(newData.status).toBe(origData.status)
expect(newData.text).not.toBe(origData.text)
return 'success'
}, 'success')
const new$ = await next.render$('/variable-revalidate/status-code')
const newData = JSON.parse(new$('#page-data').text())
expect(newData.status).toBe(origData.status)
expect(newData.text).not.toBe(origData.text)
})

if (isNextStart) {
Expand Down Expand Up @@ -313,8 +310,6 @@ createNextDescribe(
'variable-revalidate/revalidate-360.html',
'variable-revalidate/revalidate-360.rsc',
'variable-revalidate/revalidate-360/page.js',
'variable-revalidate/status-code.html',
'variable-revalidate/status-code.rsc',
'variable-revalidate/status-code/page.js',
])
})
Expand Down Expand Up @@ -530,11 +525,6 @@ createNextDescribe(
initialRevalidateSeconds: 10,
srcRoute: '/variable-revalidate/revalidate-360',
},
'/variable-revalidate/status-code': {
dataRoute: '/variable-revalidate/status-code.rsc',
initialRevalidateSeconds: 3,
srcRoute: '/variable-revalidate/status-code',
},
})
expect(curManifest.dynamicRoutes).toEqual({
'/blog/[author]/[slug]': {
Expand Down
@@ -1,3 +1,5 @@
export const revalidate = 0

export default async function Page() {
const data = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random?status=404',
Expand Down