diff --git a/packages/next/src/server/lib/patch-fetch.ts b/packages/next/src/server/lib/patch-fetch.ts index 3ee5873f75e8..843f40353a7a 100644 --- a/packages/next/src/server/lib/patch-fetch.ts +++ b/packages/next/src/server/lib/patch-fetch.ts @@ -236,8 +236,9 @@ export function patchFetch({ typeof (input as Request).method === 'string' const getRequestMeta = (field: string) => { - let value = isRequestInput ? (input as any)[field] : null - return value || (init as any)?.[field] + // If request input is present but init is not, retrieve from input first. + const value = (init as any)?.[field] + return value || (isRequestInput ? (input as any)[field] : null) } // If the staticGenerationStore is not available, we can't do any diff --git a/test/e2e/app-dir/logging/app/fetch-no-store/page.js b/test/e2e/app-dir/logging/app/fetch-no-store/page.js new file mode 100644 index 000000000000..df09454b4c97 --- /dev/null +++ b/test/e2e/app-dir/logging/app/fetch-no-store/page.js @@ -0,0 +1,12 @@ +export default async function Page() { + await fetch( + new Request( + 'https://next-data-api-endpoint.vercel.app/api/random?request-input' + ), + { + cache: 'no-store', + } + ) + + return
Hello World!
+} diff --git a/test/e2e/app-dir/logging/fetch-logging.test.ts b/test/e2e/app-dir/logging/fetch-logging.test.ts index d5c0ef341359..da012ef5d3cb 100644 --- a/test/e2e/app-dir/logging/fetch-logging.test.ts +++ b/test/e2e/app-dir/logging/fetch-logging.test.ts @@ -141,6 +141,16 @@ createNextDescribe( expect(output).toContain('Cache missed reason: (noStore call)') }) }) + + it('should respect request.init.cache when use with fetch input is instance', async () => { + const logLength = next.cliOutput.length + await next.fetch('/fetch-no-store') + + await retry(() => { + const output = stripAnsi(next.cliOutput.slice(logLength)) + expect(output).toContain('Cache missed reason: (cache: no-store)') + }) + }) } } else { // No fetches logging enabled