Skip to content

Commit 19a71df

Browse files
authored
fix FormData submitter check (#10627)
* fix FormData submitter check * Remove duplicate headers test polyfill * Bundle bump
1 parent 8b95f2b commit 19a71df

File tree

5 files changed

+24
-10
lines changed

5 files changed

+24
-10
lines changed

.changeset/brave-mails-relate.md

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+
(Remove) Fix FormData submitter feature detection check

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
"none": "16.2 kB"
119119
},
120120
"packages/react-router-dom/dist/react-router-dom.production.min.js": {
121-
"none": "12.5 kB"
121+
"none": "12.6 kB"
122122
},
123123
"packages/react-router-dom/dist/umd/react-router-dom.production.min.js": {
124124
"none": "18.6 kB"

packages/react-router-dom/__tests__/setup.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ if (!globalThis.fetch) {
2121
// @ts-expect-error
2222
globalThis.Response = Response;
2323
globalThis.Headers = Headers;
24-
globalThis.Headers = Headers;
2524
}
2625

2726
if (!globalThis.AbortController) {

packages/react-router-dom/dom.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,22 @@ export type SubmitTarget =
127127
| null;
128128

129129
// One-time check for submitter support
130-
let formDataSupportsSubmitter = false;
131-
try {
132-
// @ts-expect-error if FormData supports the submitter parameter, this will throw
133-
new FormData(undefined, 0);
134-
} catch (e) {
135-
formDataSupportsSubmitter = true;
130+
let _formDataSupportsSubmitter: boolean | null = null;
131+
132+
function isFormDataSubmitterSupported() {
133+
if (_formDataSupportsSubmitter === null) {
134+
try {
135+
new FormData(
136+
document.createElement("form"),
137+
// @ts-expect-error if FormData supports the submitter parameter, this will throw
138+
0
139+
);
140+
_formDataSupportsSubmitter = false;
141+
} catch (e) {
142+
_formDataSupportsSubmitter = true;
143+
}
144+
}
145+
return _formDataSupportsSubmitter;
136146
}
137147

138148
export interface SubmitOptions {
@@ -257,7 +267,7 @@ export function getFormSubmissionInfo(
257267
// then tack on the submitter value at the end. This is a lightweight
258268
// solution that is not 100% spec compliant. For complete support in older
259269
// browsers, consider using the `formdata-submitter-polyfill` package
260-
if (!formDataSupportsSubmitter) {
270+
if (!isFormDataSubmitterSupported()) {
261271
let { name, type, value } = target;
262272
if (type === "image") {
263273
let prefix = name ? `${name}.` : "";

packages/react-router/__tests__/setup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fetch, Request, Response } from "@remix-run/web-fetch";
1+
import { fetch, Request, Response, Headers } from "@remix-run/web-fetch";
22

33
// https://reactjs.org/blog/2022/03/08/react-18-upgrade-guide.html#configuring-your-testing-environment
44
globalThis.IS_REACT_ACT_ENVIRONMENT = true;

0 commit comments

Comments
 (0)