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: support basename and relative routing in loader/action redirects #9447

Merged
merged 9 commits into from
Oct 21, 2022
Merged
5 changes: 5 additions & 0 deletions .changeset/smart-ants-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

Support basename and relative routing in loader/action redirects
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Did this as a patch/bug fix (as opposed to a minor/new feature) since redirects probably should have supported basename and relative routing from the get go. This would be new behavior for remix though when we bring it over - but I would argue non-breaking

1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
- loun4
- lqze
- lukerSpringTree
- manzano78
- marc2332
- markivancho
- marvinruder
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
},
"filesize": {
"packages/router/dist/router.js": {
"none": "100 kB"
"none": "103 kB"
},
"packages/react-router/dist/react-router.production.min.js": {
"none": "12.5 kB"
Expand Down
117 changes: 117 additions & 0 deletions packages/react-router/__tests__/data-memory-router-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "@testing-library/react";
import "@testing-library/jest-dom";
import type { FormMethod, Router, RouterInit } from "@remix-run/router";
import { joinPaths } from "@remix-run/router";
import type { RouteObject } from "react-router";
import {
Await,
Expand All @@ -20,6 +21,7 @@ import {
createMemoryRouter,
createRoutesFromElements,
defer,
redirect,
useActionData,
useAsyncError,
useAsyncValue,
Expand Down Expand Up @@ -158,6 +160,116 @@ describe("<DataMemoryRouter>", () => {
`);
});

it("prepends basename to loader/action redirects", async () => {
let { container } = render(
<DataMemoryRouter
basename="/my/base/path"
initialEntries={["/my/base/path"]}
>
<Route path="/" element={<Root />}>
<Route path="thing" loader={() => redirect("/other")} />
<Route path="other" element={<h1>Other</h1>} />
</Route>
</DataMemoryRouter>
);

function Root() {
return (
<>
<MemoryNavigate to="/thing">Link to thing</MemoryNavigate>
<Outlet />
</>
);
}

expect(getHtml(container)).toMatchInlineSnapshot(`
"<div>
<a
href=\\"/my/base/path/thing\\"
>
Link to thing
</a>
</div>"
`);

fireEvent.click(screen.getByText("Link to thing"));
await waitFor(() => screen.getByText("Other"));
expect(getHtml(container)).toMatchInlineSnapshot(`
"<div>
<a
href=\\"/my/base/path/thing\\"
>
Link to thing
</a>
<h1>
Other
</h1>
</div>"
`);
});

it("supports relative routing in loader/action redirects", async () => {
let { container } = render(
<DataMemoryRouter
basename="/my/base/path"
initialEntries={["/my/base/path"]}
>
<Route path="/" element={<Root />}>
<Route path="parent" element={<Parent />}>
<Route path="child" loader={() => redirect("../other")} />
<Route path="other" element={<h2>Other</h2>} />
</Route>
</Route>
</DataMemoryRouter>
);

function Root() {
return (
<>
<MemoryNavigate to="/parent/child">Link to child</MemoryNavigate>
<Outlet />
</>
);
}

function Parent() {
return (
<>
<h1>Parent</h1>
<Outlet />
</>
);
}

expect(getHtml(container)).toMatchInlineSnapshot(`
"<div>
<a
href=\\"/my/base/path/parent/child\\"
>
Link to child
</a>
</div>"
`);

fireEvent.click(screen.getByText("Link to child"));
await waitFor(() => screen.getByText("Parent"));
expect(getHtml(container)).toMatchInlineSnapshot(`
"<div>
<a
href=\\"/my/base/path/parent/child\\"
>
Link to child
</a>
<h1>
Parent
</h1>
<h2>
Other
</h2>
</div>"
`);
});

it("renders with hydration data", async () => {
let { container } = render(
<DataMemoryRouter
Expand Down Expand Up @@ -2817,6 +2929,11 @@ function MemoryNavigate({
}) {
let dataRouterContext = React.useContext(DataRouterContext);

let basename = dataRouterContext?.basename;
if (basename && basename !== "/") {
to = to === "/" ? basename : joinPaths([basename, to]);
}

let onClickHandler = React.useCallback(
async (event: React.MouseEvent) => {
event.preventDefault();
Expand Down
Loading