Skip to content

Commit d864971

Browse files
deepjyoti30-stporcellus
authored andcommitted
fix: issue of cache field not supported in CF workers
1 parent 175574b commit d864971

File tree

2 files changed

+47
-32
lines changed

2 files changed

+47
-32
lines changed

lib/build/utils.js

Lines changed: 22 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/ts/utils.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,46 @@ export const doFetch: typeof fetch = async (input: RequestInfo | URL, init?: Req
1717
if (init === undefined) {
1818
ProcessState.getInstance().addState(PROCESS_STATE.ADDING_NO_CACHE_HEADER_IN_FETCH);
1919
init = {
20-
cache: "no-cache",
20+
cache: "no-store",
2121
redirect: "manual",
2222
};
2323
} else {
2424
if (init.cache === undefined) {
2525
ProcessState.getInstance().addState(PROCESS_STATE.ADDING_NO_CACHE_HEADER_IN_FETCH);
26-
init.cache = "no-cache";
26+
init.cache = "no-store";
2727
init.redirect = "manual";
2828
}
2929
}
30+
31+
// Remove the cache field if the runtime is Cloudflare Workers
32+
//
33+
// CF Workers did not support the cache field at all until Nov, 2024
34+
// when they added support for the `cache` field but it only supports
35+
// `no-store`.
36+
//
37+
// The following check is to ensure that this doesn't error out in
38+
// Cloudflare Workers where compatibility flag is set to an older version.
39+
//
40+
// Since there is no way for us to determine which compatibility flags are
41+
// enabled, we are disabling the cache functionality for CF Workers altogether.
42+
// Ref issue: https://github.com/cloudflare/workerd/issues/698
43+
if (isRunningInCloudflareWorker()) {
44+
delete init.cache;
45+
}
46+
3047
const fetchFunction = typeof fetch !== "undefined" ? fetch : crossFetch;
3148
try {
3249
return await fetchFunction(input, init);
3350
} catch (e) {
34-
// Cloudflare Workers don't support the 'cache' field in RequestInit.
35-
// To work around this, we delete the 'cache' field and retry the fetch if the error is due to the missing 'cache' field.
36-
// Remove this workaround once the 'cache' field is supported.
37-
// More info: https://github.com/cloudflare/workerd/issues/698
38-
const unimplementedCacheError =
39-
e &&
40-
typeof e === "object" &&
41-
"message" in e &&
42-
e.message === "The 'cache' field on 'RequestInitializerDict' is not implemented.";
43-
if (!unimplementedCacheError) throw e;
44-
45-
const newOpts = { ...init };
46-
delete newOpts.cache;
47-
48-
return await fetchFunction(input, newOpts);
51+
logDebugMessage("Error fetching: " + e);
52+
throw e;
4953
}
5054
};
5155

56+
function isRunningInCloudflareWorker() {
57+
return navigator.userAgent === "Cloudflare-Workers";
58+
}
59+
5260
export function getLargestVersionFromIntersection(v1: string[], v2: string[]): string | undefined {
5361
let intersection = v1.filter((value) => v2.indexOf(value) !== -1);
5462
if (intersection.length === 0) {

0 commit comments

Comments
 (0)