From 16563748922d0de204cec8eff6f081a6e6703596 Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Wed, 29 Jan 2025 15:10:16 -0500 Subject: [PATCH] Strip search params from FOW path parameter (#12899) --- .changeset/honest-socks-fail.md | 5 ++ .../router/__tests__/lazy-discovery-test.ts | 71 +++++++++++++++++++ packages/router/router.ts | 4 +- 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 .changeset/honest-socks-fail.md diff --git a/.changeset/honest-socks-fail.md b/.changeset/honest-socks-fail.md new file mode 100644 index 0000000000..544c28f551 --- /dev/null +++ b/.changeset/honest-socks-fail.md @@ -0,0 +1,5 @@ +--- +"@remix-run/router": patch +--- + +Strip search parameters from `patchRoutesOnNavigation` `path` param for fetcher calls diff --git a/packages/router/__tests__/lazy-discovery-test.ts b/packages/router/__tests__/lazy-discovery-test.ts index 0f7e83182b..aceb602275 100644 --- a/packages/router/__tests__/lazy-discovery-test.ts +++ b/packages/router/__tests__/lazy-discovery-test.ts @@ -2435,5 +2435,76 @@ describe("Lazy Route Discovery (Fog of War)", () => { expect(router.getFetcher(key).state).toBe("idle"); expect(router.getFetcher(key).data).toBe("C ACTION"); }); + + it("does not include search params in the `path` (fetcher.load)", async () => { + let capturedPath; + + router = createRouter({ + history: createMemoryHistory(), + routes: [ + { + path: "/", + }, + { + id: "parent", + path: "parent", + }, + ], + async patchRoutesOnNavigation({ path, patch }) { + capturedPath = path; + patch("parent", [ + { + id: "child", + path: "child", + loader: () => "CHILD", + }, + ]); + }, + }); + + let key = "key"; + router.fetch(key, "0", "/parent/child?a=b"); + await tick(); + expect(router.getFetcher(key).state).toBe("idle"); + expect(router.getFetcher(key).data).toBe("CHILD"); + expect(capturedPath).toBe("/parent/child"); + }); + + it("does not include search params in the `path` (fetcher.submit)", async () => { + let capturedPath; + + router = createRouter({ + history: createMemoryHistory(), + routes: [ + { + path: "/", + }, + { + id: "parent", + path: "parent", + }, + ], + async patchRoutesOnNavigation({ path, patch }) { + capturedPath = path; + patch("parent", [ + { + id: "child", + path: "child", + action: () => "CHILD", + }, + ]); + }, + }); + + let key = "key"; + router.fetch(key, "0", "/parent/child?a=b", { + formMethod: "post", + formData: createFormData({}), + }); + await tick(); + expect(router.getFetcher(key).state).toBe("idle"); + expect(router.getFetcher(key).data).toBe("CHILD"); + expect(capturedPath).toBe("/parent/child"); + }); }); }); diff --git a/packages/router/router.ts b/packages/router/router.ts index 0e48075e25..13ec68b71d 100644 --- a/packages/router/router.ts +++ b/packages/router/router.ts @@ -2264,7 +2264,7 @@ export function createRouter(init: RouterInit): Router { if (isFogOfWar) { let discoverResult = await discoverRoutes( requestMatches, - path, + new URL(fetchRequest.url).pathname, fetchRequest.signal ); @@ -2556,7 +2556,7 @@ export function createRouter(init: RouterInit): Router { if (isFogOfWar) { let discoverResult = await discoverRoutes( matches, - path, + new URL(fetchRequest.url).pathname, fetchRequest.signal );