denoland/deno · error · TypeError

Request cache mode "only-if-cached" can only be used with sa

Error message

Request cache mode "only-if-cached" can only be used with same-origin mode

What it means

Request constructor enforces the spec rule: cache mode 'only-if-cached' combined with any mode other than 'same-origin' throws TypeError. only-if-cached permits serving from the HTTP cache without network only for same-origin requests, so cross-origin (or default 'cors') usage is rejected.

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 89f33cbef2)

Solutions

  1. Add mode: 'same-origin' alongside cache: 'only-if-cached' when the URL is same-origin
  2. For cross-origin URLs use cache: 'force-cache' (serves from cache when present, network otherwise) instead
  3. Check URL origin against location.origin before selecting only-if-cached

Example fix

// before
const res = await fetch('https://same.example/api', {
  cache: 'only-if-cached', // mode defaults to 'cors' -> throws
});

// after
const res = await fetch('https://same.example/api', {
  cache: 'only-if-cached',
  mode: 'same-origin',
});
Defensive patterns

Strategy: type-guard

Validate before calling

function cacheInit(url, cache) {
  if (cache !== 'only-if-cached') return { cache };
  const origin = typeof location !== 'undefined' ? location.origin : new URL(url, 'https://example.invalid').origin;
  const target = new URL(url, typeof location !== 'undefined' ? location.href : undefined).origin;
  if (origin !== target) return { cache: 'force-cache' }; // cross-origin fallback
  return { cache, mode: 'same-origin' };
}

Type guard

const canUseOnlyIfCached = (url) => {
  try {
    return typeof location === 'undefined' || new URL(url, location.href).origin === location.origin;
  } catch {
    return false;
  }
};

Prevention

When it happens

Trigger: new Request(url, { cache: 'only-if-cached' }) with default mode 'cors'; fetch('https://cdn.example/x', { cache: 'only-if-cached' }) without mode: 'same-origin'.

Common situations: Offline-first code that adds cache: 'only-if-cached' to CDN or third-party URLs; copy-pasting a same-origin pattern onto cross-origin fetches.

Related errors


AI-assisted analysis of denoland/deno@89f33cbef2 (2026-08-16). Data as JSON: /api/errors/228fb228eb4496f3. Report an issue: GitHub.