Skip to content

Commit 8704ae4

Browse files
authored
chore: Compatible with createRouter where basename does not start with / (#11671)
1 parent bd341ff commit 8704ae4

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

.changeset/tender-sheep-bake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router": patch
3+
---
4+
5+
Fix `basename` usage without a leading slash in data routers

contributors.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@
274274
- mtendekuyokwa19
275275
- mtliendo
276276
- namoscato
277+
- nanianlisao
277278
- ned-park
278279
- nenene3
279280
- ngbrown

packages/react-router/__tests__/router/router-test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,38 @@ describe("a router", () => {
237237
});
238238
});
239239

240+
it("supports a basename prop for route matching without a leading slash", async () => {
241+
let history = createMemoryHistory({
242+
initialEntries: ["/base/name/path"],
243+
});
244+
let router = createRouter({
245+
basename: "base/name",
246+
routes: [{ path: "path" }],
247+
history,
248+
});
249+
expect(router.state).toMatchObject({
250+
location: {
251+
hash: "",
252+
key: expect.any(String),
253+
pathname: "/base/name/path",
254+
search: "",
255+
state: null,
256+
},
257+
matches: [
258+
{
259+
params: {},
260+
pathname: "/path",
261+
pathnameBase: "/path",
262+
route: {
263+
id: "0",
264+
path: "path",
265+
},
266+
},
267+
],
268+
initialized: true,
269+
});
270+
});
271+
240272
it("supports subscribers", async () => {
241273
let history = createMemoryHistory({ initialEntries: ["/"] });
242274
let count = 0;

packages/react-router/lib/router/router.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,9 @@ export function createRouter(init: RouterInit): Router {
867867
);
868868
let inFlightDataRoutes: AgnosticDataRouteObject[] | undefined;
869869
let basename = init.basename || "/";
870+
if (!basename.startsWith("/")) {
871+
basename = `/${basename}`;
872+
}
870873
let dataStrategyImpl = init.dataStrategy || defaultDataStrategyWithMiddleware;
871874

872875
// Config driven behavior flags

0 commit comments

Comments
 (0)