Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Next.js and React

URIError: URI malformed thrown while a React page loads or a link is opened

Your error

URIError: URI malformed2 more lines it can show

Fix it fast

Most common cause, 1 of 4

The text has a percent sign that is not a valid escape

Only decode text that was encoded.

Fix
function safeDecode(text) {
  try {
    return decodeURIComponent(text);
  } catch {
    return text; // not valid percent-encoding, keep it as typed
  }
}
Did this fix it?
Full steps for this cause: check, fix, verify

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.

  • Applies to Node.js 24, Node.js 26, React 19.3, Chrome, Firefox, Safari
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
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:

  1. 1The text has a percent sign that is not a valid escapeThe 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
  2. 2A value that is already decoded gets decoded a second timeThe error appears only when the user's text contains a percent sign, and the failing call wraps a value read from URLSearchParams
  3. 3Half of a surrogate pair reaches encodeURIComponentThe stack trace shows encodeURI or encodeURIComponent, and the logged text contains a broken emoji or another character outside the basic range
  4. 4User text is joined into a URL without being encodedThe 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 malformed
  • URIError: malformed URI sequence
  • URIError: 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.

Check
grep -rn "decodeURI" src

Fix

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.

Fix
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.

Verify
safeDecode('100%');
safeDecode(encodeURIComponent('100%'));

Confirmed by developer.mozilla.org

Did this fix it?

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.

Check
grep -rn -E "decodeURIComponent\([^)]*\.get\(" src

Fix

Delete the extra decode and use the value as it comes out of URLSearchParams.

Fix
const params = new URLSearchParams(window.location.search);
const query = params.get('q'); // already decoded, do not decode again

Verify

Open a page with ?q=100%25 and check that query is the text 100%.

Verify
new URLSearchParams('?q=100%25').get('q')

Confirmed by developer.mozilla.org

Did this fix it?

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.

Check
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.

Fix
const safe = encodeURIComponent(text.toWellFormed());

Verify

Encode the same text again. The call returns a string and does not throw.

Verify
encodeURIComponent('\uD800'.toWellFormed())

Confirmed by developer.mozilla.org

Did this fix it?

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.

Check
grep -rn -F 'fetch(`' src

Fix

Encode each piece of user text at the moment it goes into the URL, or let URLSearchParams build the query for you.

Fix
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.

Verify
encodeURIComponent('100%')

Confirmed by developer.mozilla.org

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.