Skip to content

fix(router-core): stringify parent params for missing route templates - #7897

Open
xianjianlf2 wants to merge 1 commit into
TanStack:mainfrom
xianjianlf2:fix/server-route-link-stringify-7877
Open

fix(router-core): stringify parent params for missing route templates#7897
xianjianlf2 wants to merge 1 commit into
TanStack:mainfrom
xianjianlf2:fix/server-route-link-stringify-7877

Conversation

@xianjianlf2

@xianjianlf2 xianjianlf2 commented Jul 25, 2026

Copy link
Copy Markdown

Fixes #7877.

When Start prunes a server-only leaf route from the client route tree, a client-side buildLocation() call for that route template cannot find an exact routesByPath entry. The current fallback leaves destRoutes empty for route templates, so parent params.stringify hooks are skipped and the generated href can differ from SSR.

This keeps exact route-template matches unchanged, but when a template is missing it falls back to the longest known parent route branch. That gives parent routes a chance to canonicalize path params while still leaving the server-only leaf absent from the client route tree.

The regression test models the client route tree after a server-only child was pruned: only /$postType exists locally, while a link is built for /$postType/$postId/download. The parent stringify maps article back to the canonical articles segment.

Tests run:

  • PATH=/opt/homebrew/bin:$PATH pnpm --dir packages/history build
  • PATH=/opt/homebrew/bin:$PATH pnpm --dir packages/router-core test:unit --run tests/build-location.test.ts -t "matched parent of a missing route template"
  • PATH=/opt/homebrew/bin:$PATH pnpm --dir packages/router-core test:unit --run tests/build-location.test.ts
  • PATH=/opt/homebrew/bin:$PATH pnpm --dir packages/router-core test:unit
  • PATH=/opt/homebrew/bin:$PATH pnpm --dir packages/router-core test:types:ts70
  • PATH=/opt/homebrew/bin:$PATH pnpm --dir packages/router-core test:eslint
  • PATH=/opt/homebrew/bin:$PATH pnpm exec prettier --check packages/router-core/src/router.ts packages/router-core/tests/build-location.test.ts

Note: test:eslint exits 0 with existing warnings in unrelated router-core files.

Summary by CodeRabbit

  • Bug Fixes
    • Improved navigation to template-based destinations when a deeply nested route is not available in the client route tree.
    • Preserved parameter formatting and canonical URL generation using the nearest known parent route.
    • Ensured nested paths resolve correctly, including transformed URLs such as /articles/1/download.

@coderabbitai

coderabbitai Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: c919be4e-abbe-415c-a776-fd767b3fbe53

📥 Commits

Reviewing files that changed from the base of the PR and between 88f366a and ae9f8d1.

📒 Files selected for processing (2)
  • packages/router-core/src/router.ts
  • packages/router-core/tests/build-location.test.ts

📝 Walkthrough

Walkthrough

RouterCore.buildLocation now uses the nearest known parent route branch when a typed destination’s leaf template is missing, preserving parent parameter stringification. A regression test verifies canonical URL generation for a server-only child route.

Changes

Router parent canonicalization

Layer / File(s) Summary
Parent branch fallback and regression coverage
packages/router-core/src/router.ts, packages/router-core/tests/build-location.test.ts
buildLocation finds the nearest known parent route branch for template-based destinations without a client leaf route, and tests confirm the parent params.stringify transformation is applied.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested labels: package: router-core

Suggested reviewers: schiller-manuel

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main router-core change: preserving parent param stringification when route templates are missing.
Linked Issues check ✅ Passed The code and regression test address #7877 by falling back to the nearest parent route branch so client hrefs match SSR canonicalization.
Out of Scope Changes check ✅ Passed The changes stay within the reported bug fix and test coverage; no unrelated functionality appears to be introduced.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Link to a server-only route skips parent params.stringify on the client

1 participant