From 67df6101982bbe4485a5e1cee71066d8f7c7b4c0 Mon Sep 17 00:00:00 2001 From: Zack Tanner Date: Wed, 15 Nov 2023 12:50:07 -0800 Subject: [PATCH] tweak cache + revalidate fetch warning --- packages/next/src/server/lib/patch-fetch.ts | 10 ++-- .../logging/app/cache-revalidate/page.js | 40 +++++++++++++++ .../{index.test.ts => fetch-logging.test.ts} | 0 .../e2e/app-dir/logging/fetch-warning.test.ts | 50 +++++++++++++++++++ 4 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 test/e2e/app-dir/logging/app/cache-revalidate/page.js rename test/e2e/app-dir/logging/{index.test.ts => fetch-logging.test.ts} (100%) create mode 100644 test/e2e/app-dir/logging/fetch-warning.test.ts diff --git a/packages/next/src/server/lib/patch-fetch.ts b/packages/next/src/server/lib/patch-fetch.ts index 9f4d499a111da..4a831e9788151 100644 --- a/packages/next/src/server/lib/patch-fetch.ts +++ b/packages/next/src/server/lib/patch-fetch.ts @@ -266,9 +266,13 @@ export function patchFetch({ typeof _cache === 'string' && typeof curRevalidate !== 'undefined' ) { - Log.warn( - `fetch for ${fetchUrl} on ${staticGenerationStore.urlPathname} specified "cache: ${_cache}" and "revalidate: ${curRevalidate}", only one should be specified.` - ) + // when providing fetch with a Request input, it'll automatically set a cache value of 'default' + // we only want to warn if the user is explicitly setting a cache value + if (!(isRequestInput && _cache === 'default')) { + Log.warn( + `fetch for ${fetchUrl} on ${staticGenerationStore.urlPathname} specified "cache: ${_cache}" and "revalidate: ${curRevalidate}", only one should be specified.` + ) + } _cache = undefined } diff --git a/test/e2e/app-dir/logging/app/cache-revalidate/page.js b/test/e2e/app-dir/logging/app/cache-revalidate/page.js new file mode 100644 index 0000000000000..296b4c6eb50ce --- /dev/null +++ b/test/e2e/app-dir/logging/app/cache-revalidate/page.js @@ -0,0 +1,40 @@ +export const fetchCache = 'default-cache' + +export default async function Page() { + await fetch( + new Request( + 'https://next-data-api-endpoint.vercel.app/api/random?request-input' + ), + { + next: { + revalidate: 3, + }, + } + ) + + await fetch( + new Request( + 'https://next-data-api-endpoint.vercel.app/api/random?request-input-cache-override', + { + cache: 'force-cache', + } + ), + { + next: { + revalidate: 3, + }, + } + ) + + await fetch( + 'https://next-data-api-endpoint.vercel.app/api/random?request-string', + { + next: { + revalidate: 3, + }, + cache: 'force-cache', + } + ) + + return
Hello World!
+} diff --git a/test/e2e/app-dir/logging/index.test.ts b/test/e2e/app-dir/logging/fetch-logging.test.ts similarity index 100% rename from test/e2e/app-dir/logging/index.test.ts rename to test/e2e/app-dir/logging/fetch-logging.test.ts diff --git a/test/e2e/app-dir/logging/fetch-warning.test.ts b/test/e2e/app-dir/logging/fetch-warning.test.ts new file mode 100644 index 0000000000000..9773564778770 --- /dev/null +++ b/test/e2e/app-dir/logging/fetch-warning.test.ts @@ -0,0 +1,50 @@ +import { check } from 'next-test-utils' +import { createNextDescribe } from 'e2e-utils' + +createNextDescribe( + 'app-dir - fetch warnings', + { + skipDeployment: true, + files: __dirname, + }, + ({ next }) => { + beforeAll(async () => { + // we don't need verbose logging (enabled by default in this Next app) for these tests to work + // we avoid enabling it since it's not currently compatible with Turbopack. + await next.stop() + await next.deleteFile('next.config.js') + await next.start() + await next.fetch('/cache-revalidate') + }) + + it('should log when request input is a string', async () => { + await check(() => { + return next.cliOutput.includes( + 'fetch for https://next-data-api-endpoint.vercel.app/api/random?request-string on /cache-revalidate specified "cache: force-cache" and "revalidate: 3", only one should be specified' + ) + ? 'success' + : 'fail' + }, 'success') + }) + + it('should log when request input is a Request instance', async () => { + await check(() => { + return next.cliOutput.includes( + 'fetch for https://next-data-api-endpoint.vercel.app/api/random?request-input-cache-override on /cache-revalidate specified "cache: force-cache" and "revalidate: 3", only one should be specified.' + ) + ? 'success' + : 'fail' + }, 'success') + }) + + it('should not log when overriding cache within the Request object', async () => { + await check(() => { + return next.cliOutput.includes( + `fetch for https://next-data-api-endpoint.vercel.app/api/random?request-input on /cache-revalidate specified "cache: default" and "revalidate: 3", only one should be specified.` + ) + ? 'fail' + : 'success' + }, 'success') + }) + } +)