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

Root router have a path different to '' or '/' #10852

Merged
merged 8 commits into from
Oct 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/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