Skip to content

Commit b7f5107

Browse files
authored
Fix: respect init.cache if fetch input is request instance (#60821)
When there’s a request input instance and init object present the same time, we should respect init as preferred Closes NEXT-2149
1 parent 752c15e commit b7f5107

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

packages/next/src/server/lib/patch-fetch.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,9 @@ export function patchFetch({
236236
typeof (input as Request).method === 'string'
237237

238238
const getRequestMeta = (field: string) => {
239-
let value = isRequestInput ? (input as any)[field] : null
240-
return value || (init as any)?.[field]
239+
// If request input is present but init is not, retrieve from input first.
240+
const value = (init as any)?.[field]
241+
return value || (isRequestInput ? (input as any)[field] : null)
241242
}
242243

243244
// If the staticGenerationStore is not available, we can't do any
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default async function Page() {
2+
await fetch(
3+
new Request(
4+
'https://next-data-api-endpoint.vercel.app/api/random?request-input'
5+
),
6+
{
7+
cache: 'no-store',
8+
}
9+
)
10+
11+
return <div>Hello World!</div>
12+
}

test/e2e/app-dir/logging/fetch-logging.test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,16 @@ createNextDescribe(
141141
expect(output).toContain('Cache missed reason: (noStore call)')
142142
})
143143
})
144+
145+
it('should respect request.init.cache when use with fetch input is instance', async () => {
146+
const logLength = next.cliOutput.length
147+
await next.fetch('/fetch-no-store')
148+
149+
await retry(() => {
150+
const output = stripAnsi(next.cliOutput.slice(logLength))
151+
expect(output).toContain('Cache missed reason: (cache: no-store)')
152+
})
153+
})
144154
}
145155
} else {
146156
// No fetches logging enabled

0 commit comments

Comments
 (0)