Skip to content

Commit 76ad58b

Browse files
nabsulijjk
andauthored
Only cache successful fetches (#48033)
### What? Change the caching logic for fetch-cache to only cache successful responses. ### Why? Currently fetch-cache will cache any response, without checking the http status code. But situations like 500 and 304 and others should not be cached, because we want to re-fetch from the origin. ### How? Add an extra check before deciding to call `incrementalCache.set()` --------- Co-authored-by: JJ Kasper <[email protected]>
1 parent 95e46f7 commit 76ad58b

File tree

4 files changed

+9
-16
lines changed

4 files changed

+9
-16
lines changed

packages/next/src/server/lib/incremental-cache/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export class IncrementalCache {
155155
): Promise<string> {
156156
// this should be bumped anytime a fix is made to cache entries
157157
// that should bust the cache
158-
const MAIN_KEY_PREFIX = 'v1'
158+
const MAIN_KEY_PREFIX = 'v2'
159159

160160
let cacheKey: string
161161
const bodyChunks: string[] = []

packages/next/src/server/lib/patch-fetch.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ export function patchFetch({
225225
const doOriginalFetch = async () => {
226226
return originFetch(input, init).then(async (res) => {
227227
if (
228+
res.ok &&
228229
staticGenerationStore.incrementalCache &&
229230
cacheKey &&
230231
typeof revalidate === 'number' &&

test/e2e/app-dir/app-static/app-static.test.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -168,19 +168,16 @@ createNextDescribe(
168168
})
169169
}
170170

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

175175
expect(origData.status).toBe(404)
176176

177-
await check(async () => {
178-
const new$ = await next.render$('/variable-revalidate/status-code')
179-
const newData = JSON.parse(new$('#page-data').text())
180-
expect(newData.status).toBe(origData.status)
181-
expect(newData.text).not.toBe(origData.text)
182-
return 'success'
183-
}, 'success')
177+
const new$ = await next.render$('/variable-revalidate/status-code')
178+
const newData = JSON.parse(new$('#page-data').text())
179+
expect(newData.status).toBe(origData.status)
180+
expect(newData.text).not.toBe(origData.text)
184181
})
185182

186183
if (isNextStart) {
@@ -313,8 +310,6 @@ createNextDescribe(
313310
'variable-revalidate/revalidate-360.html',
314311
'variable-revalidate/revalidate-360.rsc',
315312
'variable-revalidate/revalidate-360/page.js',
316-
'variable-revalidate/status-code.html',
317-
'variable-revalidate/status-code.rsc',
318313
'variable-revalidate/status-code/page.js',
319314
])
320315
})
@@ -530,11 +525,6 @@ createNextDescribe(
530525
initialRevalidateSeconds: 10,
531526
srcRoute: '/variable-revalidate/revalidate-360',
532527
},
533-
'/variable-revalidate/status-code': {
534-
dataRoute: '/variable-revalidate/status-code.rsc',
535-
initialRevalidateSeconds: 3,
536-
srcRoute: '/variable-revalidate/status-code',
537-
},
538528
})
539529
expect(curManifest.dynamicRoutes).toEqual({
540530
'/blog/[author]/[slug]': {

test/e2e/app-dir/app-static/app/variable-revalidate/status-code/page.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export const revalidate = 0
2+
13
export default async function Page() {
24
const data = await fetch(
35
'https://next-data-api-endpoint.vercel.app/api/random?status=404',

0 commit comments

Comments
 (0)