Skip to content

Commit

Permalink
Root router have a path different to '' or '/' (#10852)
Browse files Browse the repository at this point in the history
  • Loading branch information
yracnet authored Oct 2, 2023
1 parent ebe2491 commit 8af53e7
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/404-root-with-path.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

Allow 404 detection to leverage root route error boundary if path contains a URL segment
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -237,5 +237,6 @@
- xcsnowcity
- yionr
- yuleicul
- yracnet
- zheng-chuang
- sgrishchenko
79 changes: 79 additions & 0 deletions packages/router/__tests__/router-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4823,6 +4823,85 @@ describe("a router", () => {
});
});

it("handles 404 routes when the root route contains a path (initialization)", () => {
let t = setup({
routes: [
{
id: "root",
path: "/path",
children: [
{
index: true,
},
],
},
],
initialEntries: ["/junk"],
});
expect(t.router.state).toMatchObject({
errors: {
root: new ErrorResponseImpl(
404,
"Not Found",
new Error('No route matches URL "/junk"'),
true
),
},
initialized: true,
location: {
pathname: "/junk",
},
matches: [
{
route: {
id: "root",
},
},
],
});
});

it("handles 404 routes when the root route contains a path (navigation)", () => {
let t = setup({
routes: [
{
id: "root",
path: "/path",
children: [
{
index: true,
},
],
},
],
initialEntries: ["/path"],
});
expect(t.router.state).toMatchObject({
errors: null,
});
t.navigate("/junk");
expect(t.router.state).toMatchObject({
errors: {
root: new ErrorResponseImpl(
404,
"Not Found",
new Error('No route matches URL "/junk"'),
true
),
},
location: {
pathname: "/junk",
},
matches: [
{
route: {
id: "root",
},
},
],
});
});

it("converts formData to URLSearchParams for unspecified formMethod", async () => {
let t = setup({
routes: TASK_ROUTES,
Expand Down
9 changes: 6 additions & 3 deletions packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4074,9 +4074,12 @@ function getShortCircuitMatches(routes: AgnosticDataRouteObject[]): {
route: AgnosticDataRouteObject;
} {
// Prefer a root layout route if present, otherwise shim in a route object
let route = routes.find((r) => r.index || !r.path || r.path === "/") || {
id: `__shim-error-route__`,
};
let route =
routes.length === 1
? routes[0]
: routes.find((r) => r.index || !r.path || r.path === "/") || {
id: `__shim-error-route__`,
};

return {
matches: [
Expand Down

0 comments on commit 8af53e7

Please sign in to comment.