{"record":{"id":"21090e324149ff3e","repo":"denoland/deno","slug":"referrer-referrer-is-not-a-valid-url","errorCode":null,"errorMessage":"Referrer \"${referrer}\" is not a valid URL.","messagePattern":"Referrer \"(.+?)\" is not a valid URL\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/fetch/23_request.js","lineNumber":427,"sourceCode":"      // fold in of step 12 from below\n      request = cloneInnerRequest(originalReq, true);\n      request.redirectCount = 0; // reset to 0 - cloneInnerRequest copies the value\n      signal = input[_signal];\n    }\n\n    // 12. is folded into the else statement of step 6 above.\n\n    // 17. referrer\n    if (init.referrer !== undefined) {\n      const referrer = init.referrer;\n      if (referrer === \"\") {\n        request.referrer = \"no-referrer\";\n      } else {\n        let parsedReferrer;\n        try {\n          parsedReferrer = new URL(referrer, baseURL);\n        } catch (err) {\n          throw new TypeError(`Referrer \"${referrer}\" is not a valid URL.`, {\n            cause: err,\n          });\n        }\n        if (\n          (parsedReferrer.protocol === \"about:\" &&\n            parsedReferrer.pathname === \"client\")\n        ) {\n          request.referrer = \"client\";\n        } else {\n          request.referrer = parsedReferrer.href;\n        }\n      }\n    }\n\n    // 18. referrerPolicy\n    if (init.referrerPolicy !== undefined) {\n      request.referrerPolicy = init.referrerPolicy;\n    }","sourceCodeStart":409,"sourceCodeEnd":445,"githubUrl":"https://github.com/denoland/deno/blob/89f33cbef296a2b287f323d42de54c871fa69c77/ext/fetch/23_request.js#L409-L445","documentation":"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'.","triggerScenarios":"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.","commonSituations":"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).","solutions":["Pass '' for no referrer instead of the string 'no-referrer'","Use an absolute URL ('https://origin/page') for the referrer value","Pre-validate with new URL(referrer, typeof location !== 'undefined' ? location.href : undefined) in a try/catch and drop the option when it fails","Inspect err.cause to see the underlying URL parse failure"],"exampleFix":"// before\nconst req = new Request('https://a.example', {\n  referrer: 'no-referrer', // invalid URL, keyword misuse\n});\n\n// after\nconst req = new Request('https://a.example', {\n  referrer: '', // correct way to say no referrer\n});","handlingStrategy":"validation","validationCode":"function normalizeReferrer(referrer) {\n  if (referrer === '' || referrer == null) return '';\n  if (referrer === 'no-referrer') return ''; // common keyword misuse\n  const base = typeof location !== 'undefined' ? location.href : undefined;\n  try {\n    return new URL(referrer, base).href;\n  } catch {\n    throw new Error(`invalid referrer: ${JSON.stringify(referrer)}`);\n  }\n}","typeGuard":"const isValidReferrer = (r) =>\n  r == null || r === '' ||\n  (() => { try { new URL(r, typeof location !== 'undefined' ? location.href : undefined); return true; } catch { return false; } })();","tryCatchPattern":"try {\n  req = new Request(url, { referrer });\n} catch (err) {\n  if (err instanceof TypeError && err.message.includes('is not a valid URL')) {\n    req = new Request(url); // drop the referrer and retry\n  } else throw err;\n}","preventionTips":["Pass '' instead of 'no-referrer' to suppress a referrer","Prefer absolute URLs for referrer values","Check err.cause for the underlying URL parse failure when debugging"],"tags":["fetch","request","referrer","url-parsing"],"backgroundTag":null,"analyzedSha":"89f33cbef296a2b287f323d42de54c871fa69c77","analyzedAt":"2026-08-16T07:54:21.310Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}