Skip to content

Commit 7c3aca6

Browse files
authored
fix: edge fetch no cache invalid (#1012)
## Summary of change This PR fixes an issue with `fetch` calls throwing in Cloudflare workers due to the newly added `cache` support from CF where only `no-store` is supported. ## Related issues ## Test Plan All tests should pass including deployment on Cloudflare Workers ## Documentation changes (If relevant, please create a PR in our [docs repo](https://github.com/supertokens/docs), or create a checklist here highlighting the necessary changes) ## Checklist for important updates - [x] Changelog has been updated - [ ] `coreDriverInterfaceSupported.json` file has been updated (if needed) - Along with the associated array in `lib/ts/version.ts` - [ ] `frontendDriverInterfaceSupported.json` file has been updated (if needed) - [ ] Changes to the version if needed - In `package.json` - In `package-lock.json` - In `lib/ts/version.ts` - [x] Had run `npm run build-pretty` - [x] Had installed and ran the pre-commit hook - [ ] If new thirdparty provider is added, - [ ] update switch statement in `recipe/thirdparty/providers/configUtils.ts` file, `createProvider` function. - [ ] add an icon on the user management dashboard. - [x] Issue this PR against the latest non released version branch. - To know which one it is, run find the latest released tag (`git tag`) in the format `vX.Y.Z`, and then find the latest branch (`git branch --all`) whose `X.Y` is greater than the latest released tag. - If no such branch exists, then create one from the latest released branch. - [ ] If have added a new web framework, update the `add-ts-no-check.js` file to include that - [ ] If added a new recipe / api interface, then make sure that the implementation of it uses NON arrow functions only (like `someFunc: function () {..}`). - [ ] If added a new recipe, then make sure to expose it inside the recipe folder present in the root of this repo. We also need to expose its types. - [ ] If added a new entry point, then make sure that it is importable by adding it to the `exports` in `package.json` ## Remaining TODOs for this PR
2 parents 175574b + e0680da commit 7c3aca6

File tree

3 files changed

+48
-32
lines changed

3 files changed

+48
-32
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2525
- The `User` class now has a `fromApi` function to normalize the user object returned from the API.
2626
- Refactors querier to use dynamic request body and response body types inference.
2727
- Refactor internal network calls made with querier to use the new dynamic types.
28+
- Fixes an issue with fetch not supporting `cache: no-cache` in Cloudflare Workers.
2829

2930
## [22.1.0] - 2025-04-04
3031

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 typeof navigator !== "undefined" && 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)