diff --git a/.changeset/empty-path-basename-navigation.md b/.changeset/empty-path-basename-navigation.md new file mode 100644 index 0000000000..ff0d8e0c88 --- /dev/null +++ b/.changeset/empty-path-basename-navigation.md @@ -0,0 +1,5 @@ +--- +"@remix-run/router": patch +--- + +Fix basename handling when navigating without a path diff --git a/packages/router/__tests__/router-test.ts b/packages/router/__tests__/router-test.ts index 660cdd2f53..7720c49408 100644 --- a/packages/router/__tests__/router-test.ts +++ b/packages/router/__tests__/router-test.ts @@ -16220,5 +16220,29 @@ describe("a router", () => { expect(createPath(router.state.location)).toBe("/path"); expect(router.state.matches[2].route.index).toBe(true); }); + + it("handles pathless relative routing when a basename is present", () => { + let router = createRouter({ + routes: [{ path: "/path" }], + future: { v7_prependBasename: true }, + history: createMemoryHistory({ initialEntries: ["/base/path"] }), + basename: "/base", + }).initialize(); + + expect(createPath(router.state.location)).toBe("/base/path"); + expect(router.state.matches[0].route.path).toBe("/path"); + + router.navigate(".?a=1"); + expect(createPath(router.state.location)).toBe("/base/path?a=1"); + expect(router.state.matches[0].route.path).toBe("/path"); + + router.navigate("?b=2"); + expect(createPath(router.state.location)).toBe("/base/path?b=2"); + expect(router.state.matches[0].route.path).toBe("/path"); + + router.navigate("/path?c=3"); + expect(createPath(router.state.location)).toBe("/base/path?c=3"); + expect(router.state.matches[0].route.path).toBe("/path"); + }); }); }); diff --git a/packages/router/router.ts b/packages/router/router.ts index 10b0918df4..2fb88ccdde 100644 --- a/packages/router/router.ts +++ b/packages/router/router.ts @@ -3094,7 +3094,7 @@ function normalizeTo( let path = resolveTo( to ? to : ".", getPathContributingMatches(contextualMatches).map((m) => m.pathnameBase), - location.pathname, + stripBasename(location.pathname, basename) || location.pathname, relative === "path" );