Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/tender-sheep-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router": patch
---

Fix `basename` usage without a leading slash in data routers
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@
- mtendekuyokwa19
- mtliendo
- namoscato
- nanianlisao
- ned-park
- nenene3
- ngbrown
Expand Down
32 changes: 32 additions & 0 deletions packages/react-router/__tests__/router/router-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,38 @@ describe("a router", () => {
});
});

it("supports a basename prop for route matching without a leading slash", async () => {
let history = createMemoryHistory({
initialEntries: ["/base/name/path"],
});
let router = createRouter({
basename: "base/name",
routes: [{ path: "path" }],
history,
});
expect(router.state).toMatchObject({
location: {
hash: "",
key: expect.any(String),
pathname: "/base/name/path",
search: "",
state: null,
},
matches: [
{
params: {},
pathname: "/path",
pathnameBase: "/path",
route: {
id: "0",
path: "path",
},
},
],
initialized: true,
});
});

it("supports subscribers", async () => {
let history = createMemoryHistory({ initialEntries: ["/"] });
let count = 0;
Expand Down
3 changes: 3 additions & 0 deletions packages/react-router/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,9 @@ export function createRouter(init: RouterInit): Router {
);
let inFlightDataRoutes: AgnosticDataRouteObject[] | undefined;
let basename = init.basename || "/";
if (!basename.startsWith("/")) {
basename = `/${basename}`;
}
let dataStrategyImpl = init.dataStrategy || defaultDataStrategyWithMiddleware;

// Config driven behavior flags
Expand Down
Loading