Unverified Commit 76ad58b0 authored by Nabeel Sulieman's avatar Nabeel Sulieman Committed by GitHub

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: 's avatarJJ Kasper <jj@jjsweb.site>
parent 95e46f73
...@@ -155,7 +155,7 @@ export class IncrementalCache { ...@@ -155,7 +155,7 @@ export class IncrementalCache {
): Promise<string> { ): Promise<string> {
// this should be bumped anytime a fix is made to cache entries // this should be bumped anytime a fix is made to cache entries
// that should bust the cache // that should bust the cache
const MAIN_KEY_PREFIX = 'v1' const MAIN_KEY_PREFIX = 'v2'
let cacheKey: string let cacheKey: string
const bodyChunks: string[] = [] const bodyChunks: string[] = []
......
...@@ -225,6 +225,7 @@ export function patchFetch({ ...@@ -225,6 +225,7 @@ export function patchFetch({
const doOriginalFetch = async () => { const doOriginalFetch = async () => {
return originFetch(input, init).then(async (res) => { return originFetch(input, init).then(async (res) => {
if ( if (
res.ok &&
staticGenerationStore.incrementalCache && staticGenerationStore.incrementalCache &&
cacheKey && cacheKey &&
typeof revalidate === 'number' && typeof revalidate === 'number' &&
......
...@@ -168,19 +168,16 @@ createNextDescribe( ...@@ -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 $ = await next.render$('/variable-revalidate/status-code')
const origData = JSON.parse($('#page-data').text()) const origData = JSON.parse($('#page-data').text())
expect(origData.status).toBe(404) expect(origData.status).toBe(404)
await check(async () => { const new$ = await next.render$('/variable-revalidate/status-code')
const new$ = await next.render$('/variable-revalidate/status-code') const newData = JSON.parse(new$('#page-data').text())
const newData = JSON.parse(new$('#page-data').text()) expect(newData.status).toBe(origData.status)
expect(newData.status).toBe(origData.status) expect(newData.text).not.toBe(origData.text)
expect(newData.text).not.toBe(origData.text)
return 'success'
}, 'success')
}) })
if (isNextStart) { if (isNextStart) {
...@@ -313,8 +310,6 @@ createNextDescribe( ...@@ -313,8 +310,6 @@ createNextDescribe(
'variable-revalidate/revalidate-360.html', 'variable-revalidate/revalidate-360.html',
'variable-revalidate/revalidate-360.rsc', 'variable-revalidate/revalidate-360.rsc',
'variable-revalidate/revalidate-360/page.js', 'variable-revalidate/revalidate-360/page.js',
'variable-revalidate/status-code.html',
'variable-revalidate/status-code.rsc',
'variable-revalidate/status-code/page.js', 'variable-revalidate/status-code/page.js',
]) ])
}) })
...@@ -530,11 +525,6 @@ createNextDescribe( ...@@ -530,11 +525,6 @@ createNextDescribe(
initialRevalidateSeconds: 10, initialRevalidateSeconds: 10,
srcRoute: '/variable-revalidate/revalidate-360', 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({ expect(curManifest.dynamicRoutes).toEqual({
'/blog/[author]/[slug]': { '/blog/[author]/[slug]': {
......
export const revalidate = 0
export default async function Page() { export default async function Page() {
const data = await fetch( const data = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random?status=404', 'https://next-data-api-endpoint.vercel.app/api/random?status=404',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment