denoland/deno · error · TypeError

Referrer "${referrer}" is not a valid URL.

Error message

Referrer "${referrer}" is not a valid URL.

What it means

Request constructor step 17 parses init.referrer with new URL(referrer, baseURL). If parsing fails, the original error is wrapped in TypeError with message 'Referrer "<value>" is not a valid URL.' and the parse error attached as cause. Empty string is allowed ('no-referrer'); 'about:client' is mapped to 'client'.

Source

Thrown at ext/fetch/23_request.js:427

      // fold in of step 12 from below
      request = cloneInnerRequest(originalReq, true);
      request.redirectCount = 0; // reset to 0 - cloneInnerRequest copies the value
      signal = input[_signal];
    }

    // 12. is folded into the else statement of step 6 above.

    // 17. referrer
    if (init.referrer !== undefined) {
      const referrer = init.referrer;
      if (referrer === "") {
        request.referrer = "no-referrer";
      } else {
        let parsedReferrer;
        try {
          parsedReferrer = new URL(referrer, baseURL);
        } catch (err) {
          throw new TypeError(`Referrer "${referrer}" is not a valid URL.`, {
            cause: err,
          });
        }
        if (
          (parsedReferrer.protocol === "about:" &&
            parsedReferrer.pathname === "client")
        ) {
          request.referrer = "client";
        } else {
          request.referrer = parsedReferrer.href;
        }
      }
    }

    // 18. referrerPolicy
    if (init.referrerPolicy !== undefined) {
      request.referrerPolicy = init.referrerPolicy;
    }

View on GitHub (pinned to 89f33cbef2)

Solutions

  1. Pass '' for no referrer instead of the string 'no-referrer'
  2. Use an absolute URL ('https://origin/page') for the referrer value
  3. Pre-validate with new URL(referrer, typeof location !== 'undefined' ? location.href : undefined) in a try/catch and drop the option when it fails
  4. Inspect err.cause to see the underlying URL parse failure

Example fix

// before
const req = new Request('https://a.example', {
  referrer: 'no-referrer', // invalid URL, keyword misuse
});

// after
const req = new Request('https://a.example', {
  referrer: '', // correct way to say no referrer
});
Defensive patterns

Strategy: validation

Validate before calling

function normalizeReferrer(referrer) {
  if (referrer === '' || referrer == null) return '';
  if (referrer === 'no-referrer') return ''; // common keyword misuse
  const base = typeof location !== 'undefined' ? location.href : undefined;
  try {
    return new URL(referrer, base).href;
  } catch {
    throw new Error(`invalid referrer: ${JSON.stringify(referrer)}`);
  }
}

Type guard

const isValidReferrer = (r) =>
  r == null || r === '' ||
  (() => { try { new URL(r, typeof location !== 'undefined' ? location.href : undefined); return true; } catch { return false; } })();

Try / catch

try {
  req = new Request(url, { referrer });
} catch (err) {
  if (err instanceof TypeError && err.message.includes('is not a valid URL')) {
    req = new Request(url); // drop the referrer and retry
  } else throw err;
}

Prevention

When it happens

Trigger: new Request('https://a.example', { referrer: 'not a url' }) with no usable base URL (workers/scripts without a location), or a referrer string containing invalid URL characters that even the base cannot rescue.

Common situations: Passing document.referrer-style strings that are empty after trimming, relative referrers in a Web Worker or CLI script (no baseURL), or copy-pasting header values like 'no-referrer' into referrer (the keyword must be '' instead).

Related errors


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