From dc5fa859727768a3e66a3eea9c180246a9171c9a Mon Sep 17 00:00:00 2001 From: Willyams Yujra Date: Thu, 7 Sep 2023 16:45:14 -0400 Subject: [PATCH 1/7] + FIX when root router have a path different to '/' --- packages/router/router.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/router/router.ts b/packages/router/router.ts index 2980986063..2362ff7a9b 100644 --- a/packages/router/router.ts +++ b/packages/router/router.ts @@ -4075,7 +4075,8 @@ 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 === "/") || { + let route = routes.find((r) => r.index || !r.path || r.path === "/") || + routes.find((r) => r.id === "root") || { id: `__shim-error-route__`, }; From 9b167475eb32b0a1c70876387a0e27fef026adc2 Mon Sep 17 00:00:00 2001 From: Willyams Yujra Date: Thu, 7 Sep 2023 16:46:11 -0400 Subject: [PATCH 2/7] + Contributor Name --- contributors.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/contributors.yml b/contributors.yml index be7b29366d..9eecba2cb4 100644 --- a/contributors.yml +++ b/contributors.yml @@ -234,4 +234,5 @@ - xcsnowcity - yionr - yuleicul +- yracnet - zheng-chuang From 9a587d6243aab2a76b061aba82e0e00e07f9f02c Mon Sep 17 00:00:00 2001 From: Willyams Yujra Date: Fri, 29 Sep 2023 07:01:46 -0400 Subject: [PATCH 3/7] Update packages/router/router.ts Co-authored-by: Matt Brophy --- packages/router/router.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/router/router.ts b/packages/router/router.ts index 2362ff7a9b..f9d9d996d3 100644 --- a/packages/router/router.ts +++ b/packages/router/router.ts @@ -4075,10 +4075,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 === "/") || - routes.find((r) => r.id === "root") || { - 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: [ From efeb8317b2857b006feba218b8732165ade44f24 Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Mon, 2 Oct 2023 11:46:58 -0400 Subject: [PATCH 4/7] Add unit tests --- packages/router/__tests__/router-test.ts | 83 +++++++++++++++++++++++- 1 file changed, 81 insertions(+), 2 deletions(-) diff --git a/packages/router/__tests__/router-test.ts b/packages/router/__tests__/router-test.ts index 64521aada0..5897fb5115 100644 --- a/packages/router/__tests__/router-test.ts +++ b/packages/router/__tests__/router-test.ts @@ -309,13 +309,13 @@ function setup({ // active navigation loader/action function enhanceRoutes(_routes: TestRouteObject[]) { return _routes.map((r) => { - let enhancedRoute: AgnosticDataRouteObject = { + let enhancedRoute: AgnosticRouteObject = { ...r, lazy: undefined, loader: undefined, action: undefined, children: undefined, - id: r.id || `route-${guid++}`, + id: r.id, }; if (r.lazy) { // @ts-expect-error @@ -4817,6 +4817,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 ErrorResponse( + 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 ErrorResponse( + 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, From 481fef33195045c3c5c1325deafce2e6622f461f Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Mon, 2 Oct 2023 11:48:19 -0400 Subject: [PATCH 5/7] Add changeset --- .changeset/404-root-with-path.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/404-root-with-path.md diff --git a/.changeset/404-root-with-path.md b/.changeset/404-root-with-path.md new file mode 100644 index 0000000000..207bb2b5b2 --- /dev/null +++ b/.changeset/404-root-with-path.md @@ -0,0 +1,5 @@ +--- +"@remix-run/router": patch +--- + +Allow 404 detection to leverage root route error boundary if path contains a URL segment From 1168e9d23dbac46b5c3b3eb6ebf55ce79cd50e5f Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Mon, 2 Oct 2023 12:06:54 -0400 Subject: [PATCH 6/7] Fix constructor --- packages/router/__tests__/router-test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/router/__tests__/router-test.ts b/packages/router/__tests__/router-test.ts index 76a128ae08..cdc6aa860f 100644 --- a/packages/router/__tests__/router-test.ts +++ b/packages/router/__tests__/router-test.ts @@ -316,7 +316,7 @@ function setup({ loader: undefined, action: undefined, children: undefined, - id: r.id, + id: r.id || "unknown-route-id", }; if (r.lazy) { // @ts-expect-error @@ -4840,7 +4840,7 @@ describe("a router", () => { }); expect(t.router.state).toMatchObject({ errors: { - root: new ErrorResponse( + root: new ErrorResponseImpl( 404, "Not Found", new Error('No route matches URL "/junk"'), @@ -4882,7 +4882,7 @@ describe("a router", () => { t.navigate("/junk"); expect(t.router.state).toMatchObject({ errors: { - root: new ErrorResponse( + root: new ErrorResponseImpl( 404, "Not Found", new Error('No route matches URL "/junk"'), From 9033e73f39588d95b5742480fc9f1864ff830bc4 Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Mon, 2 Oct 2023 12:38:22 -0400 Subject: [PATCH 7/7] Fix test --- packages/router/__tests__/router-test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/router/__tests__/router-test.ts b/packages/router/__tests__/router-test.ts index cdc6aa860f..edc7109df7 100644 --- a/packages/router/__tests__/router-test.ts +++ b/packages/router/__tests__/router-test.ts @@ -310,13 +310,13 @@ function setup({ // active navigation loader/action function enhanceRoutes(_routes: TestRouteObject[]) { return _routes.map((r) => { - let enhancedRoute: AgnosticRouteObject = { + let enhancedRoute: AgnosticDataRouteObject = { ...r, lazy: undefined, loader: undefined, action: undefined, children: undefined, - id: r.id || "unknown-route-id", + id: r.id || `route-${guid++}`, }; if (r.lazy) { // @ts-expect-error