From 22aed25176cd02b3f6e2c25feb86e8c30295a8d6 Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Wed, 12 Oct 2022 17:57:13 -0400 Subject: [PATCH 1/3] fix: submissions should ignore pathless layout routes --- packages/router/__tests__/router-test.ts | 50 ++++++++++++++++++++++++ packages/router/router.ts | 11 ++++-- packages/router/utils.ts | 32 +++++++++++++++ 3 files changed, 90 insertions(+), 3 deletions(-) diff --git a/packages/router/__tests__/router-test.ts b/packages/router/__tests__/router-test.ts index ad0abca9f9..e81d6b12a2 100644 --- a/packages/router/__tests__/router-test.ts +++ b/packages/router/__tests__/router-test.ts @@ -2859,6 +2859,56 @@ describe("a router", () => { }); }); + it("uses the proper action for pathless layout routes", async () => { + let t = setup({ + routes: [ + { + id: "parent", + path: "/parent", + action: true, + children: [ + { + hasErrorBoundary: true, + children: [ + { + id: "index", + index: true, + action: true, + }, + ], + }, + ], + }, + ], + }); + debugger; + let A = await t.navigate("/parent", { + formMethod: "post", + formData: createFormData({ gosh: "dang" }), + }); + await A.actions.parent.resolve("PARENT"); + expect(t.router.state).toMatchObject({ + location: { pathname: "/parent" }, + actionData: { + parent: "PARENT", + }, + errors: null, + }); + + let B = await t.navigate("/parent?index", { + formMethod: "post", + formData: createFormData({ gosh: "dang" }), + }); + await B.actions.index.resolve("INDEX"); + expect(t.router.state).toMatchObject({ + location: { pathname: "/parent", search: "?index" }, + actionData: { + index: "INDEX", + }, + errors: null, + }); + }); + it("retains the index match when submitting to a layout route", async () => { let t = setup({ routes: [ diff --git a/packages/router/router.ts b/packages/router/router.ts index 2e7e495c38..b8313c3619 100644 --- a/packages/router/router.ts +++ b/packages/router/router.ts @@ -25,6 +25,7 @@ import { ErrorResponse, ResultType, convertRoutesToDataRoutes, + getPathContributingMatches, invariant, isRouteErrorResponse, matchRoutes, @@ -2836,11 +2837,15 @@ function getTargetMatch( typeof location === "string" ? parsePath(location).search : location.search; if ( matches[matches.length - 1].route.index && - !hasNakedIndexQuery(search || "") + hasNakedIndexQuery(search || "") ) { - return matches.slice(-2)[0]; + // Return the leaf index route when index is present + return matches[matches.length - 1]; } - return matches.slice(-1)[0]; + // Otherwise grab the deepest "path contributing" match (ignoring index and + // pathless layout routes) + let pathMatches = getPathContributingMatches(matches); + return pathMatches[pathMatches.length - 1]; } function createURL(location: Location | string): URL { diff --git a/packages/router/utils.ts b/packages/router/utils.ts index 7fe1cec1df..6994ba152b 100644 --- a/packages/router/utils.ts +++ b/packages/router/utils.ts @@ -835,6 +835,38 @@ function getInvalidPathError( ); } +/** + * When processing relative navigation we want to ignore ancestor routes that + * do not contribute to the path, such that index/pathless layout routes don't + * interfere. + * + * For example, when moving a route element into an index route and/or a + * pathless layout route, relative link behavior contained within should stay + * the same. Both of the following examples should link back to the root: + * + * + * + * + * + * + * + * }> // <-- Does not contribute + * // <-- Does not contribute + * + * + */ +export function getPathContributingMatches< + T extends AgnosticRouteMatch = AgnosticRouteMatch +>(matches: T[]) { + return matches.filter( + (match, index) => + index === 0 || + (!match.route.index && + match.pathnameBase !== matches[index - 1].pathnameBase) + ); +} + /** * @private */ From 127f8d0db9f0878ee7eb57112fdf38027df914de Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Wed, 12 Oct 2022 17:59:00 -0400 Subject: [PATCH 2/3] Add changset --- .changeset/moody-bulldogs-enjoy.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/moody-bulldogs-enjoy.md diff --git a/.changeset/moody-bulldogs-enjoy.md b/.changeset/moody-bulldogs-enjoy.md new file mode 100644 index 0000000000..68141fe5de --- /dev/null +++ b/.changeset/moody-bulldogs-enjoy.md @@ -0,0 +1,5 @@ +--- +"@remix-run/router": patch +--- + +Ignore pathless layout routes when looking for proper submission action function From 710195d31ce9e9b8db1f91fe64625757696e54f4 Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Wed, 12 Oct 2022 18:06:43 -0400 Subject: [PATCH 3/3] bump bundle threshold --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c60f250387..7a5bdc2480 100644 --- a/package.json +++ b/package.json @@ -107,7 +107,7 @@ }, "filesize": { "packages/router/dist/router.js": { - "none": "100 kB" + "none": "101 kB" }, "packages/react-router/dist/react-router.production.min.js": { "none": "12.5 kB"