Skip to content

Commit

Permalink
Fix: respect init.cache if fetch input is request instance (#60821)
Browse files Browse the repository at this point in the history
When there’s a request input instance and init object present the same
time, we should respect init as preferred

Closes NEXT-2149
  • Loading branch information
huozhi committed Jan 18, 2024
1 parent 752c15e commit b7f5107
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/next/src/server/lib/patch-fetch.ts
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions 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 <div>Hello World!</div>
}
10 changes: 10 additions & 0 deletions test/e2e/app-dir/logging/fetch-logging.test.ts
Expand Up @@ -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
Expand Down

0 comments on commit b7f5107

Please sign in to comment.