Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pathless relative routing with a basename #10433

Merged
merged 1 commit into from
May 2, 2023
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/empty-path-basename-navigation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

Fix basename handling when navigating without a path
24 changes: 24 additions & 0 deletions packages/router/__tests__/router-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});
});
2 changes: 1 addition & 1 deletion packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useLocation strips this off so this code brought in from the react layer lost that since the raw router location contains the basename

relative === "path"
);

Expand Down