React Forum
Blog & Guide Responses
May 20, 2023 · 04:00 AM

MDN URLSearchParams - discussion (2023-05-20)

Started by Jules Kwon
externalurlquery paramsurlsearchparamsroutingnormalizationcanonicalizationevidence

URLSearchParams feels like a utility until you try to ship shareable, stable links with filters, arrays, and normalization rules. The moment you care about caching and request identity, query canonicalization stops being optional.

Read the MDN docs

Do you treat query params as a strict schema (validate + normalize), or do you pass them through loosely? How do you canonicalize arrays/ordering so identity keys stay stable? Do you log query normalization decisions (tips lines) and/or render evidence when canonicalization happened?

Comments (16)

Back to latest
Hana Saito
May 23, 2023 · 04:16 PM

Strict schema.

If the URL is user-facing, it's product contract.

Rafael Soto
May 27, 2023 · 04:40 AM

We log parse/normalize/serialize as a single story:

txt
[tips] queryParse raw="?tag=react&tag=hooks" tags=[react,hooks]
[tips] queryNormalize tags sort=alpha result=[hooks,react]
[tips] querySerialize canonical="?tag=hooks&tag=react"

It turns "why did the URL change" into an answerable question.

Mei Tan
Jun 03, 2023 · 04:04 AM

We compute request identity from the canonical query only.

Otherwise you get duplicate caches for the same meaning, which looks like random stale data bugs.

Jun Park
Jun 12, 2023 · 12:12 PM

Concrete alternative: put less in the URL, keep the rest local.

We only put share-worthy posture in query params. Everything else stays local draft state. Otherwise you generate links nobody wants to share.

Jules Kwon
Jun 14, 2023 · 02:14 PM

Yes. The hardest part is deciding what is share-worthy.

Once you decide that, schema + canonicalization becomes straightforward.

Nina Kapoor
Jun 18, 2023 · 06:18 PM

Long-form: query normalization is a UX decision disguised as a parsing decision.

Do you drop empty values? Do you clamp numbers? Do you preserve unknown keys? Each choice affects link durability and migration strategy.

We wrote the rules down and we render an evidence token when a fallback occurred (so support can see it).

Grace McCoy
Jun 22, 2023 · 10:22 PM

Docs tie-in: query state is route state with a serialization layer; the routing docs help frame it as a contract.

Related: Routing and Navigation and State Management.

Caleb Price
Jul 07, 2023 · 07:07 AM

Short take: if parse/serialize doesn't converge, you built a URL oscillator.

Lina Ortega
Aug 21, 2023 · 09:21 PM

We keep unknown keys but we log them (migration signals).

It's useful when old links are circulating and you want to know which legacy params still matter.

Evan Brooks
Sep 12, 2023 · 12:12 PM

We render a data-qnorm=1 evidence token if canonicalization changed the URL.

It prevents the bug report where users think the app is "rewriting links" for no reason.

Ibrahim Saleh
Oct 18, 2023 · 06:18 PM

We also canonicalize spacing/encoding. It's surprising how many caching bugs are just encoding differences.

Normalize before identity, always.

Anika Sharma
Nov 10, 2023 · 10:10 AM

If you treat query params as schema, unit test the schema. It's cheap and it prevents drift.

Most query bugs are "someone changed normalization" without realizing it.

Salma Qureshi
Dec 05, 2023 · 05:05 AM

Long-form counterpoint: strict schemas can make links less tolerant of real-world copy/paste weirdness.

We kept strict normalization but we also support a small set of legacy keys and we log when we used them.

Dmitri Kline
Dec 12, 2023 · 12:12 PM

One pattern: represent arrays canonically as repeated keys, sorted, and never as comma-separated strings.

Comma-separated looks nice until you have escaping edge cases.

Priya Liao
Dec 18, 2023 · 06:18 PM

Canonicalization is the difference between debuggable and haunted links.

Keira Santos
Dec 22, 2023 · 10:22 PM

We keep canonicalization stable across web and native clients by sharing the normalization rules in one module.

If clients disagree, you get duplicate caches and inconsistent behavior.

Rowan Pierce
Dec 29, 2023 · 11:23 PM

Takeaway: URLSearchParams is easy; the policy is hard.

Write the policy down, log normalization, and your router stops feeling mysterious.