Skip to content
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/react-router/src/Transitioner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ export function Transitioner() {
_includeValidateSearch: true,
})

if (
trimPathRight(router.latestLocation.href) !==
trimPathRight(nextLocation.href)
) {
const latestPublicHref = trimPathRight(router.latestLocation.publicHref)
const nextPublicHref = trimPathRight(nextLocation.publicHref)

if (latestPublicHref !== nextPublicHref) {
Comment on lines -55 to +58
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

publicHref carries rewrite/basePath changes that should reflect in the browser URL

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about the case when the rewrite changes the origin?
we would need to do a hard navigation here i think

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we can get this solved, we can merge this

router.commitLocation({ ...nextLocation, replace: true })
}

Expand Down
66 changes: 66 additions & 0 deletions packages/react-router/tests/router.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2836,6 +2836,72 @@ describe('rewriteBasepath utility', () => {
expect(router.state.location.pathname).toBe('/test')
})

it('should handle basepath when accessing root path and maintain basepath in browser URL', 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: ['/my-app/'],
})

const router = createRouter({
routeTree,
history,
rewrite: rewriteBasepath({ basepath: '/my-app' }),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please go back to basepath prop for testing this

we reverted the deprecation of that prop

})

render(<RouterProvider router={router} />)

await waitFor(() => {
expect(screen.getByTestId('home')).toBeInTheDocument()
})

expect(router.state.location.pathname).toBe('/')
expect(history.location.pathname).toBe('/my-app/')
})

it('should handle basepath option for backward compatibility', 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: ['/my-app/'],
})

const router = createRouter({
routeTree,
history,
basepath: '/my-app',
})

render(<RouterProvider router={router} />)

await waitFor(() => {
expect(screen.getByTestId('home')).toBeInTheDocument()
})

expect(router.state.location.pathname).toBe('/')
expect(history.location.pathname).toBe('/my-app/')
})

it('should combine basepath with additional input rewrite logic', async () => {
const rootRoute = createRootRoute({
component: () => <Outlet />,
Expand Down
31 changes: 20 additions & 11 deletions packages/router-core/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,15 @@ export interface RouterOptions<
* ```ts
* const router = createRouter({
* routeTree,
* rewrite: rewriteBasepath('/basepath')
* // Or wrap existing rewrite functionality
* rewrite: rewriteBasepath('/basepath', {
* output: ({ url }) => {...},
* input: ({ url }) => {...},
* })
* rewrite: rewriteBasepath({ basepath: '/basepath' })
* // Or compose with existing rewrite functionality
* rewrite: composeRewrites([
* rewriteBasepath({ basepath: '/basepath', caseSensitive: true }),
* {
* input: ({ url }) => {...},
* output: ({ url }) => {...},
* }
* ])
* })
* ```
* @default '/'
Expand Down Expand Up @@ -1688,7 +1691,7 @@ export class RouterCore<
return {
publicHref:
rewrittenUrl.pathname + rewrittenUrl.search + rewrittenUrl.hash,
href: fullPath,
href: rewrittenUrl.href.replace(rewrittenUrl.origin, ''),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i dont think this is right

href is the internal router's representation while publicHref contains the external one
we need to keep both worlds

cc @tannerlinsley

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i dont think this is right

href is the internal router's representation while publicHref contains the external one we need to keep both worlds

cc @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 href to publicHref resolved it, so this fix was no longer needed.
I’ve reverted the change.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, pretty sure we want to keep origin here, or at least in one of them.

url: rewrittenUrl.href,
pathname: nextPathname,
search: nextSearch,
Expand Down Expand Up @@ -1784,8 +1787,11 @@ export class RouterCore<
return isEqual
}

const latestPublicHref = this.latestLocation.publicHref ?? this.latestLocation.href
const nextPublicHref = next.publicHref ?? next.href

const isSameUrl =
trimPathRight(this.latestLocation.href) === trimPathRight(next.href)
trimPathRight(latestPublicHref) === trimPathRight(nextPublicHref)

const previousCommitPromise = this.commitLocationPromise
this.commitLocationPromise = createControlledPromise<void>(() => {
Expand Down Expand Up @@ -1942,11 +1948,14 @@ export class RouterCore<
}
}

const latestPublicHref = this.latestLocation.publicHref
const nextPublicHref = nextLocation.publicHref

if (
trimPath(normalizeUrl(this.latestLocation.href)) !==
trimPath(normalizeUrl(nextLocation.href))
trimPath(normalizeUrl(latestPublicHref)) !==
trimPath(normalizeUrl(nextPublicHref))
) {
throw redirect({ href: nextLocation.href })
throw redirect({ href: nextPublicHref })
}
}

Expand Down
8 changes: 4 additions & 4 deletions packages/solid-router/src/Transitioner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ export function Transitioner() {
_includeValidateSearch: true,
})

if (
trimPathRight(router.latestLocation.href) !==
trimPathRight(nextLocation.href)
) {
const latestPublicHref = trimPathRight(router.latestLocation.publicHref)
const nextPublicHref = trimPathRight(nextLocation.publicHref)

if (latestPublicHref !== nextPublicHref) {
router.commitLocation({ ...nextLocation, replace: true })
}

Expand Down
Loading