{"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/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/fetch/23_request.js#L409-L445","documentation":"When RequestInit.referrer is a non-empty string, the Request constructor parses it with new URL(referrer, baseURL). If parsing fails (the string is not an absolute URL and cannot be resolved against the current base — workers/scripts often have no base URL), the underlying SyntaxError is wrapped and re-thrown as TypeError 'Referrer \"...\" is not a valid URL.' with the original error as its cause.","triggerScenarios":"new Request(url, { referrer: \"not a url\" }); referrer: \"page.html\" in a context without a location/base URL (worker, CLI script); referrer values containing spaces like \"my page\".","commonSituations":"Forwarding document.referrer or Referer headers verbatim into referrer; relative referrer strings that worked in a browser window (which has a base URL) but not in a Deno worker; typos in hardcoded referrers.","solutions":["Pass an absolute URL: referrer: \"https://example.com/source-page\"","Pass \"\" for no referrer, or omit the option","Pre-validate with new URL(referrer) in a try/catch before constructing the Request"],"exampleFix":"// before\nnew Request(\"https://api.example.com/data\", { referrer: \"some page\" }); // throws\n\n// after\nnew Request(\"https://api.example.com/data\", {\n  referrer: \"https://example.com/some-page\",\n});","handlingStrategy":"validation","validationCode":"let referrer;\nif (init.referrer !== undefined && init.referrer !== \"\") {\n  try {\n    referrer = new URL(init.referrer).href; // must be absolute already\n  } catch {\n    referrer = undefined; // omit instead of throwing\n  }\n}\nconst req = new Request(url, { ...init, referrer });","typeGuard":"function isAbsoluteUrl(s: string): boolean {\n  try {\n    new URL(s);\n    return true;\n  } catch {\n    return false;\n  }\n}","tryCatchPattern":"try {\n  new Request(url, { referrer });\n} catch (err) {\n  if (err instanceof TypeError && err.message.includes(\"not a valid URL\")) {\n    new Request(url, { referrer: \"\" }); // no-referrer fallback\n  } else throw err;\n}","preventionTips":["Only pass absolute URLs (or \"\") as referrer — workers have no base URL","Don't pipe raw Referer headers into the referrer option verbatim","Pre-resolve relative referrers against your app's origin before constructing the Request"],"tags":["fetch","request","referrer","url"],"backgroundTag":"invalid-referrer-url","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"}