fix(router-core): ensure rewrite basepath applies consistently #5202
main fromrefs/pull/5202/head·opened fixes #5200
Problem#
When using the new rewrite system with basepath, redirects from / to /basepath were not working in client-side navigation.
Root Cause#
The buildLocation function was returning the original fullPath for the href property instead of applying the output rewrite. This caused the Transitioner component to compare non-rewritten URLs, preventing proper redirect detection.
Summary by CodeRabbit#
Bug Fixes
- Links/href generation now uses the rewritten path (without origin) to prevent mixed-origin or malformed links while keeping the full rewritten URL for navigation and deep links.
Documentation
- Examples updated to demonstrate the new rewrite API shape and composing rewrites; public interfaces/signatures unchanged.
Tests
- Added tests validating basepath rewrite behavior and backward compatibility with basepath option.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: router-core
🤖 Nx Cloud AI Fix Eligible#
To disable these notifications, a workspace admin can disable them [in workspace settings.](https://cloud.nx.app/orgs/6412ca9d1c251d000efa21ba/workspaces/6442f7de8226a6291646a813/settings#selfHealingCISettings?utm_source=pull-request&utm_medium=comment)An automatically generated fix could have helped fix failing tasks for this run, but Self-healing CI is disabled for this workspace. Visit workspace settings to enable it and get automatic fixes in future runs.
View your CI Pipeline Execution ↗ for commit 48702a462d1e53889afa51ae5d479c0e493b0c7d
Command Status Duration Result nx affected --targets=test:eslint,test:unit,tes...❌ Failed 10m 6s View ↗ nx run-many --target=build --exclude=examples/*...✅ Succeeded 1m 39s View ↗
☁️ Nx Cloud last updated this comment at
2025-12-08 04:27:29UTCMore templates
- tanstack-router-react-example-authenticated-routes
- tanstack-router-react-example-authenticated-routes-firebase
- tanstack-router-react-example-basic
- tanstack-router-react-example-basic-default-search-params
- tanstack-router-react-example-basic-devtools-panel
- tanstack-router-react-example-basic-file-based
- tanstack-router-react-example-basic-non-nested-devtools
- tanstack-router-react-example-react-query
- tanstack-router-react-example-basic-react-query-file-based
- tanstack-router-react-example-basic-ssr-file-based
- tanstack-router-react-example-basic-ssr-streaming-file-based
- tanstack-router-react-example-basic-virtual-file-based
- tanstack-router-react-example-basic-virtual-inside-file-based
- tanstack-router-react-example-deferred-data
- tanstack-router-i18n-paraglide
- tanstack-router-react-example-kitchen-sink
- tanstack-router-react-example-kitchen-sink-file-based
- tanstack-router-react-example-kitchen-sink-react-query
- tanstack-router-react-example-kitchen-sink-react-query-file-based
- tanstack-router-react-example-large-file-based
- tanstack-router-react-example-location-masking
- tanstack-router-react-example-navigation-blocking
- tanstack-router-react-example-quickstart
- tanstack-router-react-example-quickstart-esbuild-file-based
- tanstack-router-react-example-quickstart-file-based
- tanstack-router-react-example-quickstart-rspack-file-based
- tanstack-router-react-example-quickstart-webpack-file-based
- router-monorepo-react-query
- router-mono-simple
- router-mono-simple-lazy
- tanstack-router-react-example-scroll-restoration
- tanstack-search-validator-adapters
- tanstack-start-example-bare
- tanstack-start-example-basic
- tanstack-start-example-basic-auth
- tanstack-router-react-example-basic-authjs
- tanstack-start-example-basic-cloudflare
- tanstack-start-example-basic-react-query
- tanstack-start-example-basic-rsc
- tanstack-start-example-basic-static
- tanstack-start-bun-hosting
- tanstack-start-example-clerk-basic
- tanstack-start-example-convex-trellaux
- tanstack-start-example-counter
- tanstack-start-i18n-paraglide
- tanstack-start-example-large
- tanstack-start-example-material-ui
- tanstack-start-streaming-data-from-server-functions
- tanstack-start-example-supabase-basic
- tanstack-start-tailwind-v4
- tanstack-start-example-trellaux
- tanstack-start-example-workos
- tanstack-router-react-example-view-transitions
- tanstack-router-react-example-with-framer-motion
- tanstack-router-react-example-with-trpc
- tanstack-router-react-example-with-trpc-react-query
- tanstack-router-solid-example-authenticated-routes
- tanstack-router-solid-example-authenticated-routes-firebase
- tanstack-router-solid-example-basic
- tanstack-router-solid-example-basic-default-search-params
- tanstack-router-solid-example-basic-devtools-panel
- tanstack-router-solid-example-basic-file-based
- tanstack-router-solid-example-basic-non-nested-devtools
- tanstack-router-solid-example-basic-solid-query
- tanstack-router-solid-example-basic-solid-query-file-based
- tanstack-router-solid-example-basic-ssr-file-based
- tanstack-router-solid-example-basic-ssr-streaming-file-based
- tanstack-router-solid-example-basic-virtual-file-based
- tanstack-router-solid-example-basic-virtual-inside-file-based
- tanstack-router-solid-example-deferred-data
- tanstack-router-solid-i18n-paraglide
- tanstack-router-solid-example-kitchen-sink
- tanstack-router-solid-example-kitchen-sink-file-based
- tanstack-router-solid-example-kitchen-sink-solid-query
- tanstack-router-solid-example-kitchen-sink-solid-query-file-based
- tanstack-router-solid-example-large-file-based
- tanstack-router-solid-example-location-masking
- tanstack-router-solid-example-navigation-blocking
- tanstack-router-solid-example-quickstart
- tanstack-router-solid-example-quickstart-esbuild-file-based
- tanstack-router-solid-example-quickstart-file-based
- tanstack-router-solid-example-quickstart-rspack-file-based
- tanstack-router-solid-example-quickstart-webpack-file-based
- @tanstack/router-solid-mono-simple
- router-solid-mono-simple-lazy
- router-solid-monorepo-solid-query
- tanstack-router-solid-example-scroll-restoration
- tanstack-solid-router-search-validator-adapters
- tanstack-solid-start-example-basic
- tanstack-solid-start-example-basic-auth
- tanstack-solid-start-example-basic-authjs
- tanstack-solid-start-example-basic-cloudflare
- tanstack-solid-start-example-basic-netlify
- tanstack-solid-start-example-basic-nitro
- tanstack-start-example-basic-solid-query
- tanstack-solid-start-example-basic-static
- tanstack-solid-start-bun-hosting
- tanstack-solid-start-example-convex-better-auth
- tanstack-solid-start-example-counter
- tanstack-solid-start-i18n-paraglide
- tanstack-solid-start-example-large
- tanstack-solid-start-streaming-data-from-server-functions
- tanstack-solid-start-example-supabase-basic
- tanstack-solid-start-tailwind-v4
- tanstack-router-solid-example-view-transitions
- tanstack-router-solid-example-with-framer-motion
- tanstack-router-solid-example-with-trpc
- tanstack-router-vue-example-basic-jsx
- tanstack-router-vue-example-basic-file-based-jsx
- tanstack-router-vue-example-basic-file-based-sfc
@tanstack/arktype-adapter
npm i https://pkg.pr.new/TanStack/router/@tanstack/arktype-adapter@5202@tanstack/directive-functions-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/directive-functions-plugin@5202@tanstack/eslint-plugin-router
npm i https://pkg.pr.new/TanStack/router/@tanstack/eslint-plugin-router@5202@tanstack/history
npm i https://pkg.pr.new/TanStack/router/@tanstack/history@5202@tanstack/nitro-v2-vite-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/nitro-v2-vite-plugin@5202@tanstack/react-router
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router@5202@tanstack/react-router-devtools
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-devtools@5202@tanstack/react-router-ssr-query
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-ssr-query@5202@tanstack/react-start
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start@5202@tanstack/react-start-client
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-client@5202@tanstack/react-start-server
npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-server@5202@tanstack/router-cli
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-cli@5202@tanstack/router-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-core@5202@tanstack/router-devtools
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools@5202@tanstack/router-devtools-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools-core@5202@tanstack/router-generator
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-generator@5202@tanstack/router-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-plugin@5202@tanstack/router-ssr-query-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-ssr-query-core@5202@tanstack/router-utils
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-utils@5202@tanstack/router-vite-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/router-vite-plugin@5202@tanstack/server-functions-plugin
npm i https://pkg.pr.new/TanStack/router/@tanstack/server-functions-plugin@5202@tanstack/solid-router
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router@5202@tanstack/solid-router-devtools
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-devtools@5202@tanstack/solid-router-ssr-query
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-ssr-query@5202@tanstack/solid-start
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start@5202@tanstack/solid-start-client
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-client@5202@tanstack/solid-start-server
npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-server@5202@tanstack/start-client-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-client-core@5202@tanstack/start-plugin-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-plugin-core@5202@tanstack/start-server-core
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-core@5202@tanstack/start-static-server-functions
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-static-server-functions@5202@tanstack/start-storage-context
npm i https://pkg.pr.new/TanStack/router/@tanstack/start-storage-context@5202@tanstack/valibot-adapter
npm i https://pkg.pr.new/TanStack/router/@tanstack/valibot-adapter@5202@tanstack/virtual-file-routes
npm i https://pkg.pr.new/TanStack/router/@tanstack/virtual-file-routes@5202@tanstack/vue-router
npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-router@5202@tanstack/vue-router-devtools
npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-router-devtools@5202@tanstack/vue-router-ssr-query
npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-router-ssr-query@5202@tanstack/zod-adapter
npm i https://pkg.pr.new/TanStack/router/@tanstack/zod-adapter@5202commit:
48702a4Actionable comments posted: 0
🧹 Nitpick comments (3)
packages/router-core/src/router.ts (3)
1683-1690: Minor: Buildhreffrom URL parts instead of stripping originSafer and clearer to concatenate
pathname + search + hashthan usingreplaceonhref(avoids accidental substring removal if origin appears elsewhere).- href: rewrittenUrl.href.replace(rewrittenUrl.origin, ''), + href: rewrittenUrl.pathname + rewrittenUrl.search + rewrittenUrl.hash,
1781-1783: Align equality check with newhrefsemantics (comparepublicHref)buildLocation.hrefis now output‑rewritten whilelatestLocation.href(from parse) remains input‑rewritten. Comparing them will false‑negative under basepath. ComparepublicHrefon both sides to retain the “no-op if nothing changed” optimization.- const isSameUrl = - trimPathRight(this.latestLocation.href) === trimPathRight(next.href) + const isSameUrl = + trimPathRight(this.latestLocation.publicHref) === + trimPathRight(next.publicHref)
1939-1945: SSR parity: compare and redirect usingpublicHrefSame reasoning as above: avoid comparing input‑rewritten
hrefto output‑rewrittenhref. UsepublicHreffor both the comparison and redirect target to prevent unnecessary redirects (or loops) when a basepath rewrite is active.- if ( - trimPath(normalizeUrl(this.latestLocation.href)) !== - trimPath(normalizeUrl(nextLocation.href)) - ) { - throw redirect({ href: nextLocation.href }) - } + if ( + trimPath(normalizeUrl(this.latestLocation.publicHref)) !== + trimPath(normalizeUrl(nextLocation.publicHref)) + ) { + throw redirect({ href: nextLocation.publicHref }) + }📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📥 Commits
Reviewing files that changed from the base of the PR and between 065722ab2949b1a6b8493dc288d37dfd0e19c3ff and 23be5f9d773baf950be6ddb2271f55ded775541f.
📒 Files selected for processing (1)
packages/router-core/src/router.ts(1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{ts,tsx}
📄 CodeRabbit inference engine (AGENTS.md)
Use TypeScript in strict mode with extensive type safety across the codebase
Files:
packages/router-core/src/router.ts
packages/router-core/**
📄 CodeRabbit inference engine (AGENTS.md)
Keep framework-agnostic core router logic in packages/router-core/
Files:
packages/router-core/src/router.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Preview
- GitHub Check: Test
thanks for the PR! any idea why the e2e tests fail?
also, we should add tests for this (ideally some unit tests)
thanks for the PR! any idea why the e2e tests fail?
also, we should add tests for this (ideally some unit tests)
I’m not really sure — honestly, I’m not very familiar with TanStack Start. When I run the Start example on my branch, the app gets stuck due to excessive redirects caused by the basepath. Do you have any idea what might be happening?
I’ll go ahead and add a unit test in the meantime!
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: react-router
Actionable comments posted: 0
🧹 Nitpick comments (2)
packages/react-router/tests/router.test.tsx (2)
2701-2732: Good coverage of basepath on root; add a test for “/” → basepath redirectThis validates internal “/” vs browser “/my-app/” when landing on “/my-app/”. To guard against regressions for the original bug (missing redirect from “/” to basepath), add a companion test starting at “/” and asserting history ends at “/my-app/” while router stays at “/”.
Apply this diff near this test:
+ it('should redirect from "/" to basepath and keep internal pathname "/"', async () => { + const rootRoute = createRootRoute({ component: () => <Outlet /> }) + const indexRoute = createRoute({ + getParentRoute: () => rootRoute, + path: '/', + component: () => <div data-testid="home">Home</div>, + }) + const routeTree = rootRoute.addChildren([indexRoute]) + + const history = createMemoryHistory({ initialEntries: ['/'] }) + const router = createRouter({ + routeTree, + history, + rewrite: rewriteBasepath({ basepath: '/my-app' }), + }) + + render(<RouterProvider router={router} />) + + await waitFor(() => { + expect(screen.getByTestId('home')).toBeInTheDocument() + }) + + expect(router.state.location.pathname).toBe('/') + // Allow either trailing or non-trailing slash if normalization changes: + expect(['/my-app', '/my-app/']).toContain(history.location.pathname) + })
2734-2765: Back-compat test looks good; also assert navigation applies output rewrite with basepath optionThis confirms basepath option parity on initial load. Add a navigation test mirroring the Link-based case to ensure output rewrite occurs with basepath option too.
Apply this diff near this block:
+ it('basepath option: Link navigation applies output rewrite', async () => { + const rootRoute = createRootRoute({ component: () => <Outlet /> }) + const indexRoute = createRoute({ + getParentRoute: () => rootRoute, + path: '/', + component: () => ( + <div> + <Link to="/about" data-testid="about-link">About</Link> + </div> + ), + }) + const aboutRoute = createRoute({ + getParentRoute: () => rootRoute, + path: '/about', + component: () => <div data-testid="about">About</div>, + }) + const routeTree = rootRoute.addChildren([indexRoute, aboutRoute]) + + const history = createMemoryHistory({ initialEntries: ['/my-app/'] }) + const router = createRouter({ routeTree, history, basepath: '/my-app' }) + + render(<RouterProvider router={router} />) + const aboutLink = await screen.findByTestId('about-link') + fireEvent.click(aboutLink) + + await waitFor(() => { + expect(screen.getByTestId('about')).toBeInTheDocument() + }) + expect(router.state.location.pathname).toBe('/about') + expect(history.location.pathname).toBe('/my-app/about') + })📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📥 Commits
Reviewing files that changed from the base of the PR and between 593cb78e4ba6b8dc482762fed50803693870a9b3 and a9dbeb776f0be8cdaede5c419917488804384d0a.
📒 Files selected for processing (2)
packages/react-router/tests/router.test.tsx(1 hunks)packages/router-core/src/router.ts(2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- packages/router-core/src/router.ts
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{ts,tsx}
📄 CodeRabbit inference engine (AGENTS.md)
Use TypeScript in strict mode with extensive type safety across the codebase
Files:
packages/react-router/tests/router.test.tsx
packages/{react-router,solid-router}/**
📄 CodeRabbit inference engine (AGENTS.md)
Implement React and Solid bindings/components only in packages/react-router/ and packages/solid-router/
Files:
packages/react-router/tests/router.test.tsx
🧬 Code graph analysis (1)
packages/react-router/tests/router.test.tsx (5)
packages/react-router/src/route.tsx (2)
createRootRoute(566-618)createRoute(310-390)
packages/react-router/src/Match.tsx (1)
Outlet(309-359)
packages/history/src/index.ts (1)
createMemoryHistory(568-614)
packages/react-router/src/router.ts (1)
createRouter(80-82)
packages/react-router/src/RouterProvider.tsx (1)
RouterProvider(47-56)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Test
- GitHub Check: Preview
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: solid-router
- packages/react-router/src/Transitioner.tsx lines 55 to 58Resolvedby leesb971204(GitHub profile, opens in a new tab)@@ -52,7 +52,7 @@5252 _includeValidateSearch: true,5353 })545455- if (56- trimPathRight(router.latestLocation.href) !==57- trimPathRight(nextLocation.href)58- ) {55+ const latestPublicHref = trimPathRight(router.latestLocation.publicHref)56+ const nextPublicHref = trimPathRight(nextLocation.publicHref)57+58+ if (latestPublicHref !== nextPublicHref) {
publicHref carries rewrite/basePath changes that should reflect in the browser URL
what about the case when the rewrite changes the origin? we would need to do a hard navigation here i think
if we can get this solved, we can merge this
what about the case when the rewrite changes the origin? we would need to do a hard navigation here i think
Sorry for the late reply — I was on vacation. Are there any cases where the origin can be changed through
basepathorrewrite? I think a bit more explanation would be helpful.yes you can for example map a path segment to a subdomain via a rewrite.
see this test for example
leesb971204(GitHub profile, opens in a new tab) changed the title
- packages/router-core/src/router.ts line 1694OutdatedResolvedby leesb971204(GitHub profile, opens in a new tab)@@ -1688,4 +1691,4 @@16881691 return {16891692 publicHref:16901693 rewrittenUrl.pathname + rewrittenUrl.search + rewrittenUrl.hash,1691- href: fullPath,1694+ href: rewrittenUrl.href.replace(rewrittenUrl.origin, ''),
i dont think this is right
hrefis the internal router's representation whilepublicHrefcontains the external one we need to keep both worldscc @tannerlinsley
i dont think this is right
hrefis the internal router's representation whilepublicHrefcontains the external one we need to keep both worldscc @tannerlinsley
Thank you for the review. At first, I thought this change was the right way to fix the issue, but it turned out that switching the URL comparison target from
hreftopublicHrefresolved it, so this fix was no longer needed. I’ve reverted the change.Yeah, pretty sure we want to keep origin here, or at least in one of them.
- packages/react-router/tests/router.test.tsx line 2859OutdatedResolvedby leesb971204(GitHub profile, opens in a new tab)@@ -2838,0 +2839,21 @@2839+ it('should handle basepath when accessing root path and maintain basepath in browser URL', async () => {2840+ const rootRoute = createRootRoute({2841+ component: () => <Outlet />,2842+ })2843+2844+ const indexRoute = createRoute({2845+ getParentRoute: () => rootRoute,2846+ path: '/',2847+ component: () => <div data-testid="home">Home</div>,2848+ })2849+2850+ const routeTree = rootRoute.addChildren([indexRoute])2851+2852+ const history = createMemoryHistory({2853+ initialEntries: ['/my-app/'],2854+ })2855+2856+ const router = createRouter({2857+ routeTree,2858+ history,2859+ rewrite: rewriteBasepath({ basepath: '/my-app' }),
please go back to
basepathprop for testing thiswe reverted the deprecation of that prop
- packages/react-router/src/Transitioner.tsx line 64OutdatedResolvedby leesb971204(GitHub profile, opens in a new tab)@@ -52,7 +52,13 @@5252 _includeValidateSearch: true,5353 })545455- if (56- trimPathRight(router.latestLocation.href) !==57- trimPathRight(nextLocation.href)58- ) {55+ const latestPublicHref = trimPathRight(router.latestLocation.publicHref)56+ const nextPublicHref = trimPathRight(nextLocation.publicHref)57+58+ if (latestPublicHref !== nextPublicHref) {59+ const latestOrigin = new URL(router.latestLocation.url).origin60+ const nextOrigin = new URL(nextLocation.url).origin61+62+ if (latestOrigin !== nextOrigin) {63+ if (typeof window !== 'undefined') {64+ window.location.href = nextLocation.url
Would it be more appropriate to use
router.navigate({ href: nextLocation.url })?we could just call
router.navigate()in any case (so regardless whether origin changed or not)however,
router.navigate()needs to be made origin aware (it currently isnt)we could just call
router.navigate()in any case (so regardless whether origin changed or not)however,
router.navigate()needs to be made origin aware (it currently isnt)Do we really need switch to using
router.navigate()? If it’s not necessary forrouter.navigate()to be aware of the origin, I think it’s better to leave it as is without changing it.i would prefer having it navigate() since it also enables navigation to other origins (this works with link already, but it has special treatment for this)
- @@ -1913,3 +1917,9 @@19131917 } catch {}19141918 }191519191920+ const nextLocation = this.buildLocation({1921+ to,1922+ ...rest,1923+ _includeValidateSearch: true,1924+ _isNavigate: true,1925+ } as any)
Call
buildLocationonly once and reuse the result.@@ -1926,8 +1946,10 @@19261946 return Promise.resolve()19271947 }192819481929- return this.buildAndCommitLocation({1930- ...rest,1931- href,1932- to: to as string,1933- _isNavigate: true,1949+ return this.commitLocation({1950+ ...nextLocation,1951+ replace: rest.replace,1952+ resetScroll: rest.resetScroll,1953+ hashScrollIntoView: rest.hashScrollIntoView,1954+ viewTransition: rest.viewTransition,1955+ ignoreBlocker: rest.ignoreBlocker,Since
buildLocationis always called above, onlycommitLocationis executed here.@@ -1913,3 +1917,18 @@19131917 } catch {}19141918 }191519191920+ const nextLocation = this.buildLocation({1921+ to,1922+ ...rest,1923+ _includeValidateSearch: true,1924+ _isNavigate: true,1925+ } as any)1926+1927+ if (!reloadDocument) {1928+ const currentOrigin = new URL(this.latestLocation.url).origin1929+ const nextOrigin = new URL(nextLocation.url).origin1930+1931+ if (currentOrigin !== nextOrigin) {1932+ reloadDocument = true1933+ href = nextLocation.url1934+ }Compare the origins to perform a hard navigation.
@schiller-manuel
Are there any other things we should consider?
@leesb971204 Do you need help pushing this over the finish line?
@leesb971204 Do you need help pushing this over the finish line?
I’ve handled the issue in what I believe is the appropriate way and am currently waiting for a review. If there’s any additional way I can contribute, I’d be more than happy to help!
@leesb971204 Do you need help pushing this over the finish line?
I’ve handled the issue in what I believe is the appropriate way and am currently waiting for a review. If there’s any additional way I can contribute, I’d be more than happy to help!
Maybe Manuel lost track of this PR. Have you thought about asking him on Discord?
@leesb971204 Do you need help pushing this over the finish line?
I’ve handled the issue in what I believe is the appropriate way and am currently waiting for a review. If there’s any additional way I can contribute, I’d be more than happy to help!
Maybe Manuel lost track of this PR. Have you thought about asking him on Discord?
Thank you for the feedback. I’ve sent a Discord message to Manuel and will wait for his response.
Heard something? @leesb971204
Heard something? @leesb971204
noting 🙁
Hi @leesb971204 there was quite a rewrite that was completed on router-core recently.
Would you be able to merge this with the latest main?
I just ran the tests you included, and they passed on the latest builds without any changes so not too sure if the tests aren't built from a failing initially point or if the problem has been resolved? Also took the original issue's repro, set the base in vite (to ensure its initially mounted in the correct path during dev) and did not see any specific issues.
I'm probably missing something here, so if you could merge with the latest main and ensure failing tests before your changes are applied and then obviously succeeding after your changes.
@nlynzaad
As you suggested, I just ran the tests on both the latest main branch and a PR branch that merges in the latest main. In both cases, the tests passed successfully.
However, when I merge main into the PR branch again, the issue reappears — even though it was previously resolved and working as expected.
I suspect that recent changes in the core are invalidating the fix I applied earlier. I’ll need to look into it more closely, but I believe I’ll have to find a new solution and rewrite the test accordingly.
Also, when you said that setting Vite’s base solves the issue — does that mean this work is unnecessary?
To be honest, I don’t clearly remember whether setting only the router’s basePath (without Vite’s base) was enough to make things work. This issue has been going on for quite some time, so I’m not sure anymore whether the problem occurred because the base wasn’t set at all.
@leesb971204 thanks for merging main into this.
When the app is served, through vite dev or another bundler or server in prod this will load at the route it is served at.
In vite dev's case this is by defualt at
/, but can be set to/customby configuring the base property in the vite config.Router needs to know if the app is served at any path other than the default
/. This is to ensure navigation happens correctly at that base path.While it can deduce this in vite dev's case, when served from another server or bundler there is no way for Router to know what the base path is. We therefore need to do this by configuration which is done by setting the basePath property in createRouter. In other words, these needs to match.
When updating the PR in the original issue and ensuring the app is served at the correct location I was no longer able to see a problem.
So before any more work is done on this PR I think we need to ensure the issue this PR is trying to solve is in actual fact still occuring.
@reihwald seeing as you are experiencing a similar issue could you please add your own reproducer onto the original issue, ensure its updated to the latest main, the app is served at the correct custom path and the basePath in router is set to match this custom path.
@leesb971204 thanks for merging main into this.
When the app is served, through vite dev or another bundler or server in prod this will load at the route it is served at.
In vite dev's case this is by defualt at
/, but can be set to/customby configuring the base property in the vite config.Router needs to know if the app is served at any path other than the default
/. This is to ensure navigation happens correctly at that base path.While it can deduce this in vite dev's case, when served from another server or bundler there is no way for Router to know what the base path is. We therefore need to do this by configuration which is done by setting the basePath property in createRouter. In other words, these needs to match.
When updating the PR in the original issue and ensuring the app is served at the correct location I was no longer able to see a problem.
So before any more work is done on this PR I think we need to ensure the issue this PR is trying to solve is in actual fact still occuring.
@reihwald seeing as you are experiencing a similar issue could you please add your own reproducer onto the original issue, ensure its updated to the latest main, the app is served at the correct custom path and the basePath in router is set to match this custom path.
I’m really sorry for asking again, but just to confirm — If we don’t set the
basein vite.config and only setbasePathin the router, is it expected foruseLocation().pathnameorwindow.location.pathnameto return'/'when accessed from the root path?On initial render yes. Router has not executed any navigations yet to actually update the url with the set basePath
On initial render yes. Router has not executed any navigations yet to actually update the url with the set basePath
In that case, this PR might not be necessary — but from what I recall, before
rewritewas added, the initial render would show the basePath in pathname regardless of the base setting invite.config.I tested it with version 1.129.2, and it does behave that way: https://stackblitz.com/edit/github-lebazlff-9dj5638p?file=package.json,src%2Froutes%2F__root.tsx,src%2Fmain.tsx
Below is the test case I ran using the latest version: https://stackblitz.com/edit/github-szphp94v?file=src%2Froutes%2Fabout.tsx,src%2Froutes%2F__root.tsx,src%2Froutes%2Findex.tsx
Shouldn't both versions behave the same way?
Thank you for looking into this @nlynzaad. I'm not facing the exact same issue as @leesb971204 it seems. Please take a look at https://github.com/TanStack/router/issues/5200#issuecomment-3627658496 for a reproducer for my issue. Would you agree that this is worth a different issue?
@reihwald yes agreed, please create a new issue. From the description it seems related but different enough for a seperate issue
@nlynzaad
How do you think this issue(#5200) should be handled in this PR?
@leesb971204 see the updated stackblitz apart from the bump in versions I added the base to the vite cconfig
@leesb971204 I'm going to close this in favour of #6157 which is updated with the latest main and addresses the issues in rewrite and buildLocation.
nlynzaad(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
Walkthrough#
parseLocation now sources internal href from the rewritten URL (origin removed) and sets url to the full rewritten href; publicHref remains pathname+search+hash. Documentation examples updated to use rewriteBasepath({ basepath }) and composeRewrites. Two new tests validate basepath rewrite and backward-compat behavior.
Changes#
packages/router-core/src/router.tshreffromrewrittenUrl.href.replace(rewrittenUrl.origin, '')instead offullPath;urlusesrewrittenUrl.href;publicHrefremainsrewrittenUrl.pathname + rewrittenUrl.search + rewrittenUrl.hash. Examples/docs switch torewriteBasepath({ basepath: '/basepath' })andcomposeRewrites([...]). No public API signature changes.packages/react-router/tests/router.test.tsxSequence Diagram(s)#
Estimated code review effort#
🎯 3 (Moderate) | ⏱️ ~20 minutes
Suggested reviewers#
Poem#
Pre-merge checks and finishing touches#
✅ Passed checks (5 passed)
✨ Finishing touches
🧪 Generate unit tests
Comment `@coderabbitai help` to get the list of available commands and usage tips.