Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
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 @@ -2698,6 +2698,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' }),
})

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
17 changes: 10 additions & 7 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, ''),
url: rewrittenUrl.href,
pathname: nextPathname,
search: nextSearch,
Expand Down
Loading