Skip to content

Commit 919b5c7

Browse files
committed
chore: Compatible with createRouter where basename does not start with /
1 parent 5d66dbd commit 919b5c7

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

packages/router/__tests__/router-test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,38 @@ describe("a router", () => {
244244
});
245245
});
246246

247+
it("supports a basename prop for route matching and make sure it has a leading /", async () => {
248+
let history = createMemoryHistory({
249+
initialEntries: ["/base/name/path"],
250+
});
251+
let router = createRouter({
252+
basename: "base/name",
253+
routes: [{ path: "path" }],
254+
history,
255+
});
256+
expect(router.state).toMatchObject({
257+
location: {
258+
hash: "",
259+
key: expect.any(String),
260+
pathname: "/base/name/path",
261+
search: "",
262+
state: null,
263+
},
264+
matches: [
265+
{
266+
params: {},
267+
pathname: "/path",
268+
pathnameBase: "/path",
269+
route: {
270+
id: "0",
271+
path: "path",
272+
},
273+
},
274+
],
275+
initialized: true,
276+
});
277+
});
278+
247279
it("supports subscribers", async () => {
248280
let history = createMemoryHistory({ initialEntries: ["/"] });
249281
let count = 0;

packages/router/router.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,8 @@ export function createRouter(init: RouterInit): Router {
773773
manifest
774774
);
775775
let inFlightDataRoutes: AgnosticDataRouteObject[] | undefined;
776-
let basename = init.basename || "/";
776+
// make sure it has a leading /
777+
let basename = init.basename?.replace(/^\/*/, "/") || "/";
777778
let dataStrategyImpl = init.unstable_dataStrategy || defaultDataStrategy;
778779
// Config driven behavior flags
779780
let future: FutureConfig = {

0 commit comments

Comments
 (0)