{"record":{"id":"228fb228eb4496f3","repo":"denoland/deno","slug":"request-cache-mode-only-if-cached-can-only-be-us","errorCode":null,"errorMessage":"Request cache mode \"only-if-cached\" can only be used with same-origin mode","messagePattern":"Request cache mode \"only-if-cached\" can only be used with same-origin mode","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/fetch/23_request.js","lineNumber":470,"sourceCode":"      request.mode = init.mode;\n    }\n\n    // 20. credentials\n    if (init.credentials !== undefined) {\n      request.credentialsMode = init.credentials;\n    }\n\n    // 21. cache\n    if (init.cache !== undefined) {\n      request.cacheMode = init.cache;\n    }\n\n    // If request's cache mode is \"only-if-cached\" and request's mode is not\n    // \"same-origin\", then throw a TypeError.\n    if (\n      request.cacheMode === \"only-if-cached\" && request.mode !== \"same-origin\"\n    ) {\n      throw new TypeError(\n        'Request cache mode \"only-if-cached\" can only be used with same-origin mode',\n      );\n    }\n\n    // 22.\n    if (init.redirect !== undefined) {\n      request.redirectMode = init.redirect;\n    }\n\n    // 23. integrity\n    if (init.integrity !== undefined) {\n      request.integrity = init.integrity;\n    }\n\n    // 24. keepalive\n    if (init.keepalive !== undefined) {\n      request.keepalive = init.keepalive;\n    }","sourceCodeStart":452,"sourceCodeEnd":488,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/fetch/23_request.js#L452-L488","documentation":"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.","triggerScenarios":"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\" }.","commonSituations":"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.","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"],"exampleFix":"// before\nnew Request(\"https://example.com/api\", { cache: \"only-if-cached\" }); // throws\n\n// after\nnew Request(\"https://example.com/api\", {\n  cache: \"only-if-cached\",\n  mode: \"same-origin\",\n});","handlingStrategy":"validation","validationCode":"if (init.cache === \"only-if-cached\" && init.mode !== \"same-origin\") {\n  init = { ...init, cache: \"default\" }; // or set mode: \"same-origin\"\n}\nconst req = new Request(url, init);","typeGuard":"function cacheModeComboIsValid(cache, mode) {\n  return cache !== \"only-if-cached\" || mode === \"same-origin\";\n}","tryCatchPattern":"try {\n  new Request(url, { cache, mode });\n} catch (err) {\n  if (err instanceof TypeError && err.message.includes(\"only-if-cached\")) {\n    new Request(url, { cache, mode: \"same-origin\" }); // or drop only-if-cached\n  } else throw err;\n}","preventionTips":["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"],"tags":["fetch","request","cache","cors"],"backgroundTag":"invalid-cache-mode-combo","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}