denoland/deno · error · TypeError
Referrer " " is not a valid URL.
Error message
Referrer "${referrer}" is not a valid URL. What it means
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.
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
Example fix
// before
new Request("https://api.example.com/data", { referrer: "some page" }); // throws
// after
new Request("https://api.example.com/data", {
referrer: "https://example.com/some-page",
}); Defensive patterns
Strategy: validation
Validate before calling
let referrer;
if (init.referrer !== undefined && init.referrer !== "") {
try {
referrer = new URL(init.referrer).href; // must be absolute already
} catch {
referrer = undefined; // omit instead of throwing
}
}
const req = new Request(url, { ...init, referrer }); Type guard
function isAbsoluteUrl(s: string): boolean {
try {
new URL(s);
return true;
} catch {
return false;
}
} Try / catch
try {
new Request(url, { referrer });
} catch (err) {
if (err instanceof TypeError && err.message.includes("not a valid URL")) {
new Request(url, { referrer: "" }); // no-referrer fallback
} else throw err;
} Prevention
- 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
When it happens
Trigger: 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".
Common situations: 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.
Related errors
- cannot read url: request closed
- Cannot read url: request closed
- Body is unusable
- Cannot read headers: request closed
- Input request's body is unusable
AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20).
Data as JSON: /api/errors/21090e324149ff3e.
Report an issue: GitHub.
Appendix: 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 9ad36f7a2c)