Skip to content

Commit 9022200

Browse files
fix(react-router): strip search params from FOW path parameter (#13911)
* Strip search params from FOW path parameter * Update .changeset/honest-socks-fail.md Co-authored-by: Michaël De Boey <[email protected]> * Fix unit tests --------- Co-authored-by: Michaël De Boey <[email protected]>
1 parent 7768cdd commit 9022200

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

.changeset/honest-socks-fail.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router": patch
3+
---
4+
5+
Strip search parameters from `patchRoutesOnNavigation` `path` param for fetcher calls

packages/react-router/__tests__/router/lazy-discovery-test.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2333,5 +2333,92 @@ describe("Lazy Route Discovery (Fog of War)", () => {
23332333
expect(router.getFetcher(key).state).toBe("idle");
23342334
expect(fetcherData.get(key)).toBe("C ACTION");
23352335
});
2336+
2337+
it("does not include search params in the `path` (fetcher.load)", async () => {
2338+
let capturedPath;
2339+
2340+
router = createRouter({
2341+
history: createMemoryHistory(),
2342+
routes: [
2343+
{
2344+
path: "/",
2345+
},
2346+
{
2347+
id: "parent",
2348+
path: "parent",
2349+
},
2350+
],
2351+
async patchRoutesOnNavigation({ path, patch }) {
2352+
capturedPath = path;
2353+
patch("parent", [
2354+
{
2355+
id: "child",
2356+
path: "child",
2357+
loader: () => "CHILD",
2358+
},
2359+
]);
2360+
},
2361+
});
2362+
2363+
let key = "key";
2364+
2365+
let data;
2366+
router.subscribe((state) => {
2367+
if (state.fetchers.has("key")) {
2368+
data = state.fetchers.get("key")!.data;
2369+
}
2370+
});
2371+
2372+
router.fetch(key, "0", "/parent/child?a=b");
2373+
await tick();
2374+
expect(router.getFetcher(key).state).toBe("idle");
2375+
expect(data).toBe("CHILD");
2376+
expect(capturedPath).toBe("/parent/child");
2377+
});
2378+
2379+
it("does not include search params in the `path` (fetcher.submit)", async () => {
2380+
let capturedPath;
2381+
2382+
router = createRouter({
2383+
history: createMemoryHistory(),
2384+
routes: [
2385+
{
2386+
path: "/",
2387+
},
2388+
{
2389+
id: "parent",
2390+
path: "parent",
2391+
},
2392+
],
2393+
async patchRoutesOnNavigation({ path, patch }) {
2394+
capturedPath = path;
2395+
patch("parent", [
2396+
{
2397+
id: "child",
2398+
path: "child",
2399+
action: () => "CHILD",
2400+
},
2401+
]);
2402+
},
2403+
});
2404+
2405+
let key = "key";
2406+
2407+
let data;
2408+
router.subscribe((state) => {
2409+
if (state.fetchers.has("key")) {
2410+
data = state.fetchers.get("key")!.data;
2411+
}
2412+
});
2413+
2414+
router.fetch(key, "0", "/parent/child?a=b", {
2415+
formMethod: "post",
2416+
formData: createFormData({}),
2417+
});
2418+
await tick();
2419+
expect(router.getFetcher(key).state).toBe("idle");
2420+
expect(data).toBe("CHILD");
2421+
expect(capturedPath).toBe("/parent/child");
2422+
});
23362423
});
23372424
});

packages/react-router/lib/router/router.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2330,7 +2330,7 @@ export function createRouter(init: RouterInit): Router {
23302330
if (isFogOfWar) {
23312331
let discoverResult = await discoverRoutes(
23322332
requestMatches,
2333-
path,
2333+
new URL(fetchRequest.url).pathname,
23342334
fetchRequest.signal,
23352335
key
23362336
);
@@ -2634,7 +2634,7 @@ export function createRouter(init: RouterInit): Router {
26342634
if (isFogOfWar) {
26352635
let discoverResult = await discoverRoutes(
26362636
matches,
2637-
path,
2637+
new URL(fetchRequest.url).pathname,
26382638
fetchRequest.signal,
26392639
key
26402640
);

0 commit comments

Comments
 (0)