Skip to content

Commit b274c74

Browse files
pkuzcoBahaa Naamneh
andauthored
do not set cache in authenticated fetch (#5020)
* do not set cache in authenticated fetch * issue 5019 - updated comment * do not set cache in authenticated fetch Signed-off-by: Bahaa Naamneh <[email protected]> * issue 5019 - updated comment Signed-off-by: Bahaa Naamneh <[email protected]> --------- Signed-off-by: Bahaa Naamneh <[email protected]> Co-authored-by: Bahaa Naamneh <[email protected]>
1 parent b489bb1 commit b274c74

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/http-api/fetch.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,13 @@ export class FetchHttpApi<O extends IHttpOpts> {
279279

280280
const { signal, cleanup } = anySignal(signals);
281281

282+
// Set cache mode based on presence of Authorization header.
283+
// Browsers/proxies do not cache responses to requests with Authorization headers.
284+
// So specifying "no-cache" is redundant, and actually prevents caching
285+
// of preflight requests in CORS scenarios. As such, we only set "no-cache"
286+
// when there is no Authorization header.
287+
const cacheMode = "Authorization" in headers ? undefined : "no-cache";
288+
282289
let res: Response;
283290
const start = Date.now();
284291
try {
@@ -291,7 +298,7 @@ export class FetchHttpApi<O extends IHttpOpts> {
291298
redirect: "follow",
292299
referrer: "",
293300
referrerPolicy: "no-referrer",
294-
cache: "no-cache",
301+
cache: cacheMode,
295302
credentials: "omit", // we send credentials via headers
296303
keepalive: keepAlive,
297304
priority: opts.priority,

0 commit comments

Comments
 (0)