Skip to content

Commit aa65dbd

Browse files
smithkibrophdawg11
andauthored
fix(react-router-dom): fix getSearchParamsForLocation in Firefox (#10620)
Co-authored-by: Matt Brophy <matt@brophy.org>
1 parent 1b8ee16 commit aa65dbd

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router-dom": patch
3+
---
4+
5+
Fixes an edge-case affecting web extensions in Firefox that use `URLSearchParams` and the `useSearchParams` hook.

contributors.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,5 +222,6 @@
222222
- yionr
223223
- yuleicul
224224
- zheng-chuang
225+
- smithki
225226
- istarkov
226227
- louis-young

packages/react-router-dom/__tests__/search-params-test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,25 @@ describe("useSearchParams", () => {
161161

162162
expect(node.innerHTML).toMatch(/The current value is ""/);
163163
});
164+
165+
it("returns initial default values in search params", () => {
166+
function SearchPage() {
167+
let [searchParams] = useSearchParams({ a: "1", b: "2" });
168+
return <p>{searchParams.toString()}</p>;
169+
}
170+
171+
act(() => {
172+
ReactDOM.createRoot(node).render(
173+
<MemoryRouter initialEntries={["/search?value=initial"]}>
174+
<Routes>
175+
<Route path="search" element={<SearchPage />} />
176+
</Routes>
177+
</MemoryRouter>
178+
);
179+
});
180+
181+
expect(node.innerHTML).toMatchInlineSnapshot(
182+
`"<p>value=initial&amp;a=1&amp;b=2</p>"`
183+
);
184+
});
164185
});

packages/react-router-dom/dom.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,18 @@ export function getSearchParamsForLocation(
9797
let searchParams = createSearchParams(locationSearch);
9898

9999
if (defaultSearchParams) {
100-
for (let key of defaultSearchParams.keys()) {
100+
// Use `defaultSearchParams.forEach(...)` here instead of iterating of
101+
// `defaultSearchParams.keys()` to work-around a bug in Firefox related to
102+
// web extensions. Relevant Bugzilla tickets:
103+
// https://bugzilla.mozilla.org/show_bug.cgi?id=1414602
104+
// https://bugzilla.mozilla.org/show_bug.cgi?id=1023984
105+
defaultSearchParams.forEach((_, key) => {
101106
if (!searchParams.has(key)) {
102107
defaultSearchParams.getAll(key).forEach((value) => {
103108
searchParams.append(key, value);
104109
});
105110
}
106-
}
111+
});
107112
}
108113

109114
return searchParams;

0 commit comments

Comments
 (0)