From bf69d061967667f71582286782f38fe548e30227 Mon Sep 17 00:00:00 2001 From: dvoytenko Date: Wed, 13 Mar 2024 18:26:34 -0700 Subject: [PATCH] Disable cache in testmode --- .../next/src/server/lib/incremental-cache/index.ts | 11 +++++++---- test/e2e/testmode/testmode.test.ts | 14 ++++++++++++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/packages/next/src/server/lib/incremental-cache/index.ts b/packages/next/src/server/lib/incremental-cache/index.ts index f829ae21920b8..58d47c8324197 100644 --- a/packages/next/src/server/lib/incremental-cache/index.ts +++ b/packages/next/src/server/lib/incremental-cache/index.ts @@ -66,6 +66,7 @@ export class CacheHandler { export class IncrementalCache implements IncrementalCacheType { dev?: boolean + disableForTestmode?: boolean cacheHandler?: CacheHandler hasCustomCacheHandler: boolean prerenderManifest: PrerenderManifest @@ -142,6 +143,7 @@ export class IncrementalCache implements IncrementalCacheType { maxMemoryCacheSize = parseInt(process.env.__NEXT_TEST_MAX_ISR_CACHE, 10) } this.dev = dev + this.disableForTestmode = process.env.NEXT_PRIVATE_TEST_PROXY === 'true' // this is a hack to avoid Webpack knowing this is equal to this.minimalMode // because we replace this.minimalMode to true in production bundles. const minimalModeKey = 'minimalMode' @@ -441,9 +443,10 @@ export class IncrementalCache implements IncrementalCacheType { // we don't leverage the prerender cache in dev mode // so that getStaticProps is always called for easier debugging if ( - this.dev && - (ctx.kindHint !== 'fetch' || - this.requestHeaders['cache-control'] === 'no-cache') + this.disableForTestmode || + (this.dev && + (ctx.kindHint !== 'fetch' || + this.requestHeaders['cache-control'] === 'no-cache')) ) { return null } @@ -560,7 +563,7 @@ export class IncrementalCache implements IncrementalCacheType { }) } - if (this.dev && !ctx.fetchCache) return + if (this.disableForTestmode || (this.dev && !ctx.fetchCache)) return // FetchCache has upper limit of 2MB per-entry currently const itemSize = JSON.stringify(data).length if ( diff --git a/test/e2e/testmode/testmode.test.ts b/test/e2e/testmode/testmode.test.ts index b48129e7c240c..caf0a948823a0 100644 --- a/test/e2e/testmode/testmode.test.ts +++ b/test/e2e/testmode/testmode.test.ts @@ -38,11 +38,11 @@ createNextDescribe( proxyServer.close() }) - const fetchForTest = async (url: string) => { + const fetchForTest = async (url: string, testData?: string) => { return next.fetch(url, { headers: { 'Next-Test-Proxy-Port': String(proxyServer.port), - 'Next-Test-Data': 'test1', + 'Next-Test-Data': testData ?? 'test1', }, }) } @@ -53,6 +53,16 @@ createNextDescribe( expect(html).toContain('
test1
') }) + it('should avoid fetch cache', async () => { + const html1 = await (await fetchForTest('/app/rsc-fetch')).text() + expect(html1).toContain('
test1
') + + const html2 = await ( + await fetchForTest('/app/rsc-fetch', 'test2') + ).text() + expect(html2).toContain('
test2
') + }) + it('should handle RSC with http.get in serverless function', async () => { const html = await (await fetchForTest('/app/rsc-httpget')).text() expect(html).toContain('
test1
')