From c518560745cbc43b75710bd1f3ae14fedaafc124 Mon Sep 17 00:00:00 2001 From: Nabeel Sulieman Date: Thu, 6 Apr 2023 09:32:53 -0700 Subject: [PATCH 1/5] Only cache successful fetches --- packages/next/src/server/lib/patch-fetch.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/next/src/server/lib/patch-fetch.ts b/packages/next/src/server/lib/patch-fetch.ts index 459603a98697..a33ce63c7564 100644 --- a/packages/next/src/server/lib/patch-fetch.ts +++ b/packages/next/src/server/lib/patch-fetch.ts @@ -230,6 +230,10 @@ export function patchFetch({ typeof revalidate === 'number' && revalidate > 0 ) { + if (!res.ok) { + return res + } + let base64Body = '' const resBlob = await res.blob() const arrayBuffer = await resBlob.arrayBuffer() From 3fc1f0dc7eed47759f633b40f96ef319edc5f859 Mon Sep 17 00:00:00 2001 From: Nabeel Sulieman Date: Thu, 6 Apr 2023 09:34:26 -0700 Subject: [PATCH 2/5] simpler --- packages/next/src/server/lib/patch-fetch.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/next/src/server/lib/patch-fetch.ts b/packages/next/src/server/lib/patch-fetch.ts index a33ce63c7564..b06862cfbf6d 100644 --- a/packages/next/src/server/lib/patch-fetch.ts +++ b/packages/next/src/server/lib/patch-fetch.ts @@ -225,15 +225,12 @@ export function patchFetch({ const doOriginalFetch = async () => { return originFetch(input, init).then(async (res) => { if ( + res.ok && staticGenerationStore.incrementalCache && cacheKey && typeof revalidate === 'number' && revalidate > 0 ) { - if (!res.ok) { - return res - } - let base64Body = '' const resBlob = await res.blob() const arrayBuffer = await resBlob.arrayBuffer() From 5e07b95fe4769fbb90f9e4e9ea4fbdd3b1a3b952 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 6 Apr 2023 09:52:51 -0700 Subject: [PATCH 3/5] update test and bump cache key --- .../next/src/server/lib/incremental-cache/index.ts | 2 +- test/e2e/app-dir/app-static/app-static.test.ts | 13 +++++-------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/packages/next/src/server/lib/incremental-cache/index.ts b/packages/next/src/server/lib/incremental-cache/index.ts index ee0569331fa0..315e857c4c43 100644 --- a/packages/next/src/server/lib/incremental-cache/index.ts +++ b/packages/next/src/server/lib/incremental-cache/index.ts @@ -155,7 +155,7 @@ export class IncrementalCache { ): Promise { // 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[] = [] diff --git a/test/e2e/app-dir/app-static/app-static.test.ts b/test/e2e/app-dir/app-static/app-static.test.ts index 34f7966239e1..05cabe2c84a4 100644 --- a/test/e2e/app-dir/app-static/app-static.test.ts +++ b/test/e2e/app-dir/app-static/app-static.test.ts @@ -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) { From 45a8e44d38060aad93082431a0d7d7ec4b3ebbd4 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 6 Apr 2023 10:55:47 -0700 Subject: [PATCH 4/5] update test --- .../app-static/app/variable-revalidate/status-code/page.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/e2e/app-dir/app-static/app/variable-revalidate/status-code/page.js b/test/e2e/app-dir/app-static/app/variable-revalidate/status-code/page.js index 51d80b5e729b..be261d7a2790 100644 --- a/test/e2e/app-dir/app-static/app/variable-revalidate/status-code/page.js +++ b/test/e2e/app-dir/app-static/app/variable-revalidate/status-code/page.js @@ -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', From e343fcdc5e0b72b8434acf8e6f1ffbec0ea40db4 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 6 Apr 2023 10:58:40 -0700 Subject: [PATCH 5/5] update more --- test/e2e/app-dir/app-static/app-static.test.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/test/e2e/app-dir/app-static/app-static.test.ts b/test/e2e/app-dir/app-static/app-static.test.ts index 05cabe2c84a4..02911bf0879a 100644 --- a/test/e2e/app-dir/app-static/app-static.test.ts +++ b/test/e2e/app-dir/app-static/app-static.test.ts @@ -310,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', ]) }) @@ -527,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]': {