Skip to content

Commit 86a3dd5

Browse files
perf: avoid slow searchParams mutation in manifest prefetching (#14084)
1 parent 239039d commit 86a3dd5

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

.changeset/good-months-hope.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+
Switch Lazy Route Discovery manifest URL generation to usea standalone `URLSearchParams` instance instead of `URL.searchParams` to avoid a major performance bottleneck in Chrome

contributors.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@
310310
- remorses
311311
- renyu-io
312312
- reyronald
313+
- richardscarrott
313314
- rifaidev
314315
- rimian
315316
- robbtraister

packages/react-router/lib/dom/ssr/fog-of-war.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,18 @@ export async function fetchAndApplyManifestPatches(
217217
patchRoutes: DataRouter["patchRoutes"],
218218
signal?: AbortSignal,
219219
): Promise<void> {
220+
// NOTE: Intentionally using a standalone `URLSearchParams` instance
221+
// instead of mutating `url.searchParams`, which is *significantly* slower:
222+
// https://issues.chromium.org/issues/331406951
223+
// https://github.com/nodejs/node/issues/51518
224+
const searchParams = new URLSearchParams();
225+
paths.sort().forEach((path) => searchParams.append("p", path));
226+
searchParams.set("version", manifest.version);
220227
let url = new URL(
221228
getManifestPath(manifestPath, basename),
222229
window.location.origin,
223230
);
224-
paths.sort().forEach((path) => url.searchParams.append("p", path));
225-
url.searchParams.set("version", manifest.version);
231+
url.search = searchParams.toString();
226232

227233
// If the URL is nearing the ~8k limit on GET requests, skip this optimization
228234
// step and just let discovery happen on link click. We also wipe out the

0 commit comments

Comments
 (0)