fix(router-core): canonicalize the search string on publicHref in parseLocation #8450
main fromrefs/pull/8450/head·opened 🎯 Changes#
Fixes #8448.
parseLocation has two branches. The fast one builds publicHref from the re-stringified search:
const searchStr = this.options.stringifySearch(parsedSearch)
return {
href: pathname + searchStr + hash,
publicHref: pathname + searchStr + hash, // canonical
...
}
The other one — taken whenever a rewrite is configured, and also when the pathname needs encoding — returns the raw history href instead:
url.search = searchStr
return {
href: url.href.replace(url.origin, ''), // canonical
publicHref: href, // raw, straight from history
...
}
So with any rewrite set, even a no-op one, landing on a url whose search is not in the router's canonical form (?q=a%2Ab, ?q=two%20words, ?a=1&&b=2) leaves publicHref non-canonical while href is already canonical. The transitioner compares that against what buildLocation produces for the same location, sees a difference, commits a location change, and every matched loader runs a second time right after hydration.
Nothing is visible — the address bar does not change and no navigation happens — so the only symptom is a duplicate server-function call on every landing. As the reporter puts it, ad networks encode * and spaces differently from stringifySearch, so in their app this fired on every paid-ad landing.
The fix#
Only the query component is re-stringified. The path and hash are left exactly as history gave them.
I first tried the other option the issue suggests — sending the canonical url back through executeRewriteOutput, mirroring how buildLocation derives publicHref. That is wrong here, and the existing suite catches it:
- An output rewrite is not the inverse of the input one. With
basepath: '/other'and a path that does not carry that basepath, the output rewrite prepends it again andpublicHrefbecomes/other/app/publicinstead of/app/public. - It makes a user's
outputrewrite run during parsing. Three existing tests assert the rewrite call log, and it went from['input:/public']to['input:/public', 'output:/internal']— an observable side effect in user code.
Re-stringifying only the query avoids both: no assumption that the rewrite round-trips, and no user callback invoked during parse.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested code changes locally with the relevant test commands, or tests do not apply to this pull request.
- I fully understand the code in this pull request, including any code generated with AI assistance.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Tests#
A regression test in packages/router-core/tests/rewrite.test.ts asserts a parsed landing location matches the built one. It fails on main:
Expected: "/posts?q=a*b"
Received: "/posts?q=a%2Ab"
and passes with this change.
@tanstack/router-core: 126 files, 3246 passed, 4 expected-fail.@tanstack/react-router: 73 files, 1011 passed, 1 skipped (after buildingrouter-core; without that build these fail to resolve@tanstack/router-coreon a clean checkout too).tsc --noEmitonrouter-coreis clean, and both changed files are Prettier-formatted.
Disclosure: developed with AI assistance (Claude Code); the diagnosis, the discarded first approach and the final diff were reviewed before submitting.
Summary by CodeRabbit#
- Bug Fixes
- Fixed rewritten landing URLs so their public links use the canonical search-string format.
- Prevented unnecessary location updates and repeated data loading immediately after hydration when search parameters use an equivalent, non-canonical format.
- Preserved the existing path and hash while normalizing only the query string.
- 1
Maher-Reven(GitHub profile, opens in a new tab) linked this to fix #8448
Sign in to join this conversation.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Advanced
Run ID:
72ce79e4-4144-401e-9b5f-b4f5de49a814📥 Commits
Reviewing files that changed from the base of the PR and between 1ca361ba52a627d2f76ab33323bd83d1d0aa65a3 and 4bf83a4b1d6cac46d983eec1c481442b9db36c23.
📒 Files selected for processing (3)
.changeset/public-href-canonical-search.mdpackages/router-core/src/router.tspackages/router-core/tests/rewrite.test.tsIncluded review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.
📝 Walkthrough
Walkthrough#
The router now canonicalizes the search component of
publicHrefduringparseLocationwhile preserving the path and hash. A rewrite test verifies parity withbuildLocation, and a patch changeset documents the fix.Changes#
Canonical search handling
publicHrefsearchpackages/router-core/src/router.tswithCanonicalSearchand uses it inparseLocationto re-stringify only the query component.packages/router-core/tests/rewrite.test.ts,.changeset/public-href-canonical-search.md@tanstack/router-core.Priority: ➖ Normal
Estimated code review effort: 2 (Simple) | ~10 minutes
Change: Bug fix · Severity of issue fixed: Medium
Suggested reviewers:
schiller-manuelMerge Risk: ⚪ Minimal · up to
4bf83The router now canonicalizes rewritten location searches consistently, with regression coverage for the reported hydration mismatch.
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)#
✅ Passed checks (4 passed)
publicHrefduringparseLocation.#8448.parseLocationnow canonicalizes only the search component ofpublicHrefwhen rewrites apply. The path and hash remain unchanged. The regression test covers a no-op…parseLocationfix, its focused regression test, and a patch-release changeset for@tanstack/router-core. These changes directly support issue#8448. No unre…Full details: Docstring Coverage
Explanation
Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 2 files. (1 skipped: 1 unsupported.)
✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share