Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(react-router-dom): fix getSearchParamsForLocation in Firefox #10620

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/firefox-addons-and-web-extensions-bug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router-dom": patch
---

Fixes an edge-case affecting web extensions in Firefox that use `URLSearchParams` and the `useSearchParams` hook.
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -222,5 +222,6 @@
- yionr
- yuleicul
- zheng-chuang
- smithki
- istarkov
- louis-young
21 changes: 21 additions & 0 deletions packages/react-router-dom/__tests__/search-params-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,25 @@ describe("useSearchParams", () => {

expect(node.innerHTML).toMatch(/The current value is ""/);
});

it("returns initial default values in search params", () => {
function SearchPage() {
let [searchParams] = useSearchParams({ a: "1", b: "2" });
return <p>{searchParams.toString()}</p>;
}

act(() => {
ReactDOM.createRoot(node).render(
<MemoryRouter initialEntries={["/search?value=initial"]}>
<Routes>
<Route path="search" element={<SearchPage />} />
</Routes>
</MemoryRouter>
);
});

expect(node.innerHTML).toMatchInlineSnapshot(
`"<p>value=initial&amp;a=1&amp;b=2</p>"`
);
});
});
9 changes: 7 additions & 2 deletions packages/react-router-dom/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,18 @@ export function getSearchParamsForLocation(
let searchParams = createSearchParams(locationSearch);

if (defaultSearchParams) {
for (let key of defaultSearchParams.keys()) {
// Use `defaultSearchParams.forEach(...)` here instead of iterating of
// `defaultSearchParams.keys()` to work-around a bug in Firefox related to
// web extensions. Relevant Bugzilla tickets:
// https://bugzilla.mozilla.org/show_bug.cgi?id=1414602
// https://bugzilla.mozilla.org/show_bug.cgi?id=1023984
defaultSearchParams.forEach((_, key) => {
if (!searchParams.has(key)) {
defaultSearchParams.getAll(key).forEach((value) => {
searchParams.append(key, value);
});
}
}
});
}
brophdawg11 marked this conversation as resolved.
Show resolved Hide resolved

return searchParams;
Expand Down