Skip to content

fix(router): apply trailingSlash config before basepath redirect comparison on SSR#7295

Open
EduardF1 wants to merge 4 commits into
TanStack:mainfrom
EduardF1:fix/ssr-basepath-trailing-slash-redirect
Open

fix(router): apply trailingSlash config before basepath redirect comparison on SSR#7295
EduardF1 wants to merge 4 commits into
TanStack:mainfrom
EduardF1:fix/ssr-basepath-trailing-slash-redirect

Conversation

@EduardF1

@EduardF1 EduardF1 commented Apr 29, 2026

Copy link
Copy Markdown

Summary

Fixes #7291

When a router is configured with a custom �asepath (e.g. �asepath: "/preview"), visiting the bare basepath URL (/preview) on SSR triggers a spurious 308 redirect to /preview/ — even with railingSlash: "never" (the default).

Root cause

The SSR redirect check in �eforeLoad compares the incoming latestLocation.publicHref against
extLocation.publicHref rebuilt via �uildLocation:

if (this.latestLocation.publicHref !== nextLocation.publicHref) {
  throw redirect(...)
}

When a basepath rewrite is active, �uildLocation calls ^[xecuteRewriteOutput which runs
ewriteBasepath.output:

url.pathname = joinPaths(['/', trimmedBasepath, url.pathname])
// joinPaths(['/', 'preview', '/']) → '/preview/'

The rewrite output always produces a trailing slash (basepath + internal /), but the router's railingSlash option was never applied to the result. The fast path (no rewrite) correctly goes through
esolvePath which handles railingSlash, but the rewrite path didn't.

Result:

  • incoming publicHref = /preview
  • rebuilt publicHref = /preview/
  • mismatch → 308 redirect (wrong)

Fix

After ^[xecuteRewriteOutput produces the rewritten URL inside �uildLocation, normalize the pathname component of publicHref according to his.options.trailingSlash:

  • "never" (default): rimPathRight("/preview/") → /preview — matches incoming, no redirect ✅
  • "always": ensure trailing slash present — correctly redirects /preview → /preview/ ✅
  • "preserve": leave rewrite output unchanged (existing behaviour) ✅

The change is surgical: it only affects the publicHref used for the SSR canonicality check; href (internal routing) is unchanged.

Summary by CodeRabbit

Bug Fixes

  • Fixed incorrect 308 redirects when using SSR with basepath rewriting and trailingSlash: 'never' configuration.

Review Change Stack

@github-actions

github-actions Bot commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

Bundle Size Benchmarks

  • Commit: b1c061aff918
  • Measured at: 2026-05-09T23:47:22.218Z
  • Baseline source: history:b1c061aff918
  • Dashboard: bundle-size history
Scenario Current (gzip) Delta vs baseline Initial gzip Raw Brotli Trend
react-router.minimal 87.34 KiB +47 B (+0.05%) 87.20 KiB 274.20 KiB 75.95 KiB ▁▁▁▁▁▁▁▁▁▆▆█
react-router.full 90.86 KiB +40 B (+0.04%) 90.72 KiB 285.70 KiB 78.94 KiB ▁▁▁▁▁▁▁▁▁▆▆█
solid-router.minimal 35.56 KiB +46 B (+0.13%) 35.43 KiB 106.48 KiB 31.97 KiB ▁▁▁▁▁▁▁▁▁▆▆█
solid-router.full 40.26 KiB +35 B (+0.08%) 40.14 KiB 120.69 KiB 36.17 KiB ▁▁▁▁▁▁▁▁▁▆▇█
vue-router.minimal 53.33 KiB +49 B (+0.09%) 53.20 KiB 151.63 KiB 47.97 KiB ▁▁▁▁▁▁▁▁▁▆▆█
vue-router.full 58.46 KiB +46 B (+0.08%) 58.33 KiB 167.81 KiB 52.38 KiB ▁▁▁▁▁▁▁▁▁▆▆█
react-start.minimal 102.02 KiB +47 B (+0.05%) 101.88 KiB 322.64 KiB 88.12 KiB ▁▁▁▁▁▁▁▁▃▇▇█
react-start.full 105.46 KiB +57 B (+0.05%) 105.33 KiB 332.97 KiB 91.14 KiB ▁▁▁▁▁▁▁▁▃▇▇█
react-start.rsbuild.minimal 99.63 KiB +39 B (+0.04%) 99.46 KiB 317.08 KiB 85.72 KiB ▁▁▁▁▁▁▁▁▃▇▇█
react-start.rsbuild.full 102.94 KiB +46 B (+0.04%) 102.77 KiB 327.51 KiB 88.52 KiB ▁▁▁▁▁▁▁▁▃▇▇█
solid-start.minimal 49.65 KiB +41 B (+0.08%) 49.52 KiB 152.60 KiB 43.78 KiB ▁▁▁▁▁▁▁▁▃▇▇█
solid-start.full 55.44 KiB +42 B (+0.07%) 55.31 KiB 169.51 KiB 48.84 KiB ▁▁▁▁▁▁▁▁▃▇▇█

Current gzip tracks all emitted client JS chunks. Initial gzip tracks only the entry/import graph. Trend sparkline is historical current gzip ending with this PR measurement; lower is better.

@EduardF1 EduardF1 force-pushed the fix/ssr-basepath-trailing-slash-redirect branch from ef52522 to 463d40c Compare April 30, 2026 17:58
…arison on SSR

When a basepath is configured (e.g. �asepath: "/preview"), the SSR
redirect check in �eforeLoad compares latestLocation.publicHref
(the raw incoming URL, e.g. /preview) against
extLocation.publicHref
(rebuilt via �uildLocation).

The problem: �uildLocation runs the rewrite output (
ewriteBasepath)
which joins basepath + "/" = /preview/, but never applies the router's
	railingSlash option to the result.  With 	railingSlash: "never" (the
default) the rebuilt publicHref is /preview/ while the incoming one is
/preview, so they always differ and a spurious 308 redirect fires.

Fix: after �xecuteRewriteOutput produces the rewritten URL inside
�uildLocation, normalize the pathname component of publicHref according
to 	his.options.trailingSlash before returning, the same way the fast
(no-rewrite) path already does via
esolvePath.

- 	railingSlash: "never" (default) – strip trailing slash → /preview
  matches incoming /preview, no redirect.
- 	railingSlash: "always" – ensure trailing slash → /preview/ → will
  correctly redirect if the user lands on /preview.
- 	railingSlash: "preserve" ΓÇô leave the rewrite output unchanged
  (existing behaviour).

Fixes TanStack#7291
@EduardF1 EduardF1 force-pushed the fix/ssr-basepath-trailing-slash-redirect branch from 463d40c to c9c673a Compare April 30, 2026 18:37
@coderabbitai

coderabbitai Bot commented May 9, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 35d2a84d-fc5e-4122-9cf7-ec05bace980c

📥 Commits

Reviewing files that changed from the base of the PR and between b1c061a and 00fc9a4.

📒 Files selected for processing (2)
  • .changeset/fix-ssr-bare-basepath-redirect.md
  • packages/router-core/src/router.ts

📝 Walkthrough

Walkthrough

This PR fixes SSR redirect behavior for bare basepath URLs. When a basepath is configured and SSR rewrites the URL to the same origin, the rewritten pathname is now normalized according to the router's trailingSlash option before constructing publicHref, preventing spurious redirects to slash-appended paths.

Changes

SSR Basepath Redirect Fix

Layer / File(s) Summary
Release Documentation
.changeset/fix-ssr-bare-basepath-redirect.md
Changeset documenting patch release for @tanstack/router-core that applies trailingSlash normalization to rewritten basepath URLs before redirect comparison.
Router Pathname Normalization
packages/router-core/src/router.ts
When this.rewrite is active and rewritten origin matches router origin, the publicHref now normalizes the rewritten pathname based on this.options.trailingSlash (trims for 'never', adds for 'always') before concatenating search and hash.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A basepath bare was trailing slashed,
When SSR redirects it brashly flashed,
Now trailingSlash rules the rewritten way,
No phantom / to ruin the day! ✨

🚥 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 accurately describes the main change: applying trailingSlash config before basepath redirect comparison on SSR.
Linked Issues check ✅ Passed The PR correctly implements the fix described in issue #7291 by normalizing the rewritten pathname according to trailingSlash before the SSR canonicality check.
Out of Scope Changes check ✅ Passed All changes directly address the core issue: applying trailingSlash normalization to publicHref in buildLocation and documenting the fix in a changeset.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


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 and usage tips.

@schiller-manuel

Copy link
Copy Markdown
Collaborator

we will look into this, just a lot of things to do

@EduardF1

Copy link
Copy Markdown
Author

Thanks @schiller-manuel, appreciated — no rush. Happy to adjust anything once you get a chance to review.

@EduardF1

Copy link
Copy Markdown
Author

Summary

Applies trailingSlash normalization before the SSR basepath redirect comparison, preventing an infinite redirect loop when the configured trailing slash policy differs from the incoming URL.

Status

  • CI: ✅ All checks passing (Benchmark PR, labeler, Bundle Size, CodeRabbit)
  • Changeset included
  • No unresolved review threads
  • Ready for review

@schiller-manuel @SeanCassiere @tannerlinsley — happy to adjust anything if needed. Thanks!

@EduardF1

EduardF1 commented Jun 4, 2026

Copy link
Copy Markdown
Author

Hi team, CI is green. Would love a review when you get a chance!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Start] Bare basepath URL (e.g. /preview) redirects to /preview/ on SSR even with trailingSlash: 'never'

2 participants