denoland/deno · error · TypeError
Request cache mode "only-if-cached" can only be used with…
Error message
Request cache mode "only-if-cached" can only be used with same-origin mode
What it means
Per fetch spec step 22, a Request whose cache mode is "only-if-cached" must also have mode "same-origin" — a cross-origin only-if-cached request could otherwise serve one origin's cached response to another. The Request constructor enforces this after applying init.cache and init.mode and throws TypeError when the combination is invalid. Note "only-if-cached" is meaningful mainly in browsers with an HTTP cache.
Solutions
- Add mode: "same-origin" alongside cache: "only-if-cached"
- If you are not in a browser, drop "only-if-cached" — Deno's fetch has no matching spec HTTP cache behavior for it
- Use "no-store" or "force-cache" if you only need cache hints
Example fix
// before
new Request("https://example.com/api", { cache: "only-if-cached" }); // throws
// after
new Request("https://example.com/api", {
cache: "only-if-cached",
mode: "same-origin",
}); Defensive patterns
Strategy: validation
Validate before calling
if (init.cache === "only-if-cached" && init.mode !== "same-origin") {
init = { ...init, cache: "default" }; // or set mode: "same-origin"
}
const req = new Request(url, init); Type guard
function cacheModeComboIsValid(cache, mode) {
return cache !== "only-if-cached" || mode === "same-origin";
} Try / catch
try {
new Request(url, { cache, mode });
} catch (err) {
if (err instanceof TypeError && err.message.includes("only-if-cached")) {
new Request(url, { cache, mode: "same-origin" }); // or drop only-if-cached
} else throw err;
} Prevention
- Pair cache: 'only-if-cached' with mode: 'same-origin' in every example and helper
- Remember this cache mode is a browser HTTP-cache concept; rarely useful in Deno
- Validate (cache, mode) as a unit, not independently
When it happens
Trigger: new Request(url, { cache: "only-if-cached" }) without any mode (default mode is "cors" or "no-cors", never "same-origin" unless set); explicitly { cache: "only-if-cached", mode: "cors" }.
Common situations: Porting Service Worker or browser cache-strategy code (workbox-style patterns) to Deno, where the HTTP cache semantics differ; setting cache modes from a config that doesn't pair them with mode.
Related errors
- Body is unusable
- Cannot read headers: request closed
- cannot read url: request closed
- Cannot read url: request closed
- Input request's body is unusable
AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20).
Data as JSON: /api/errors/228fb228eb4496f3.
Report an issue: GitHub.
Appendix: source
Thrown at ext/fetch/23_request.js:470
request.mode = init.mode;
}
// 20. credentials
if (init.credentials !== undefined) {
request.credentialsMode = init.credentials;
}
// 21. cache
if (init.cache !== undefined) {
request.cacheMode = init.cache;
}
// If request's cache mode is "only-if-cached" and request's mode is not
// "same-origin", then throw a TypeError.
if (
request.cacheMode === "only-if-cached" && request.mode !== "same-origin"
) {
throw new TypeError(
'Request cache mode "only-if-cached" can only be used with same-origin mode',
);
}
// 22.
if (init.redirect !== undefined) {
request.redirectMode = init.redirect;
}
// 23. integrity
if (init.integrity !== undefined) {
request.integrity = init.integrity;
}
// 24. keepalive
if (init.keepalive !== undefined) {
request.keepalive = init.keepalive;
}View on GitHub (pinned to 9ad36f7a2c)