URIError: URI malformed thrown while a React page loads or a link is opened
Fix it fast
Most common cause, 1 of 4The text has a percent sign that is not a valid escape
Only decode text that was encoded.
function safeDecode(text) {
try {
return decodeURIComponent(text);
} catch {
return text; // not valid percent-encoding, keep it as typed
}
}Short answer
URIError: URI malformed is thrown by decodeURIComponent, decodeURI or their encode twins. Decoding fails on a % without two hex digits after it, as in text that was never encoded or was already decoded by URLSearchParams. Encoding fails on a lone surrogate. Encode user text once, decode once inside try/catch, and call toWellFormed() first. Node.js 24, 26 and current browsers.
Which cause do you have?
Open the stack trace and find which function threw, then log the exact text you gave it. The function name and the value point to one cause below.
try {
decodeURIComponent(raw);
} catch (error) {
console.error(error.name, JSON.stringify(raw));
}Then find the line you see in this list and open its fix:
- 1The text has a percent sign that is not a valid escape
The logged value contains a % that is not followed by two hex digits, for example 100% or 50% off, or an escape cut short such as %E0%A4%A - 2A value that is already decoded gets decoded a second time
The error appears only when the user's text contains a percent sign, and the failing call wraps a value read from URLSearchParams - 3Half of a surrogate pair reaches encodeURIComponent
The stack trace shows encodeURI or encodeURIComponent, and the logged text contains a broken emoji or another character outside the basic range - 4User text is joined into a URL without being encoded
The failing value is a URL your own code put together, and the part that came from a form or a search box still holds a raw % or &
What does the error look like?
URIError: URI malformedURIError: malformed URI sequenceURIError: String contained an illegal UTF-16 sequence.
Cause 1 of 4
The text has a percent sign that is not a valid escape
You seeThe logged value contains a % that is not followed by two hex digits, for example 100% or 50% off, or an escape cut short such as %E0%A4%A
Check
URIError: URI malformed from decodeURIComponent means the input holds a percent sign that is not a valid escape. The reference for decodeURIComponent says it throws when the input has a % not followed by two hexadecimal digits, or when the escape sequence does not encode a valid UTF-8 character. Plain user text that was never encoded fails the same way.
grep -rn "decodeURI" srcFix
Only decode text that was encoded. Where the text can be anything, catch the error and keep the original, so one bad link cannot take the page down.
function safeDecode(text) {
try {
return decodeURIComponent(text);
} catch {
return text; // not valid percent-encoding, keep it as typed
}
}Verify
Run it on a value with a lone percent sign and on a properly encoded one.
safeDecode('100%');
safeDecode(encodeURIComponent('100%'));Confirmed by developer.mozilla.org
Cause 2 of 4
A value that is already decoded gets decoded a second time
You seeThe error appears only when the user's text contains a percent sign, and the failing call wraps a value read from URLSearchParams
Check
A URI malformed error on a value taken from URLSearchParams comes from decoding it a second time. The URLSearchParams reference shows that parsing a query string turns the escapes back into plain characters, and that get() hands you the unencoded value. A decodeURIComponent around get() therefore decodes text that is no longer encoded.
grep -rn -E "decodeURIComponent\([^)]*\.get\(" srcFix
Delete the extra decode and use the value as it comes out of URLSearchParams.
const params = new URLSearchParams(window.location.search);
const query = params.get('q'); // already decoded, do not decode againVerify
Open a page with ?q=100%25 and check that query is the text 100%.
new URLSearchParams('?q=100%25').get('q')Confirmed by developer.mozilla.org
Cause 3 of 4
Half of a surrogate pair reaches encodeURIComponent
You seeThe stack trace shows encodeURI or encodeURIComponent, and the logged text contains a broken emoji or another character outside the basic range
Check
When encodeURIComponent throws URI malformed, the string holds a lone surrogate, half of a UTF-16 pair; its reference names exactly that case. JavaScript string indexes count UTF-16 code units and split('') separates pairs, so text cut by index can end between the two halves of an emoji. isWellFormed() reports whether a string has that problem.
console.log(text.isWellFormed());Fix
Replace lone surrogates before encoding, or cut text in a way that keeps pairs together. toWellFormed() swaps each lone surrogate for the Unicode replacement character.
const safe = encodeURIComponent(text.toWellFormed());Verify
Encode the same text again. The call returns a string and does not throw.
encodeURIComponent('\uD800'.toWellFormed())Confirmed by developer.mozilla.org
Cause 4 of 4
User text is joined into a URL without being encoded
You seeThe failing value is a URL your own code put together, and the part that came from a form or a search box still holds a raw % or &
Check
URI malformed in code that reads a URL you assembled yourself points to user text that went in without encoding. MDN tells you to run encodeURIComponent on user-entered fields before they go to the server, because characters such as & would otherwise be read as part of the URL. A template string that drops the raw text into a path or a query skips that step, so the code that later reads the URL meets an invalid escape.
grep -rn -F 'fetch(`' srcFix
Encode each piece of user text at the moment it goes into the URL, or let URLSearchParams build the query for you.
const url = `/api/search?q=${encodeURIComponent(term)}`;
const res = await fetch(url);Verify
Encode the text 100% and check that it becomes 100%25, the form a decoder accepts.
encodeURIComponent('100%')Confirmed by developer.mozilla.org
How do I stop it coming back?
- Encode with encodeURIComponent when user text goes into a URL, and decode once, at the place where you read it back.
- Never wrap a value from URLSearchParams in decodeURIComponent.
- Wrap decoding of anything a visitor can type or paste in try/catch.
- Call toWellFormed() before encoding text that your code may have cut.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: TaxiDex, QistLock, Workforce.