Skip to content

Commit

Permalink
fix FormData submitter check (#10627)
Browse files Browse the repository at this point in the history
* fix FormData submitter check

* Remove duplicate headers test polyfill

* Bundle bump
  • Loading branch information
brophdawg11 committed Jun 21, 2023
1 parent 8b95f2b commit 19a71df
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-mails-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router-dom": patch
---

(Remove) Fix FormData submitter feature detection check
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
"none": "16.2 kB"
},
"packages/react-router-dom/dist/react-router-dom.production.min.js": {
"none": "12.5 kB"
"none": "12.6 kB"
},
"packages/react-router-dom/dist/umd/react-router-dom.production.min.js": {
"none": "18.6 kB"
Expand Down
1 change: 0 additions & 1 deletion packages/react-router-dom/__tests__/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ if (!globalThis.fetch) {
// @ts-expect-error
globalThis.Response = Response;
globalThis.Headers = Headers;
globalThis.Headers = Headers;
}

if (!globalThis.AbortController) {
Expand Down
24 changes: 17 additions & 7 deletions packages/react-router-dom/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,22 @@ export type SubmitTarget =
| null;

// One-time check for submitter support
let formDataSupportsSubmitter = false;
try {
// @ts-expect-error if FormData supports the submitter parameter, this will throw
new FormData(undefined, 0);
} catch (e) {
formDataSupportsSubmitter = true;
let _formDataSupportsSubmitter: boolean | null = null;

function isFormDataSubmitterSupported() {
if (_formDataSupportsSubmitter === null) {
try {
new FormData(
document.createElement("form"),
// @ts-expect-error if FormData supports the submitter parameter, this will throw
0
);
_formDataSupportsSubmitter = false;
} catch (e) {
_formDataSupportsSubmitter = true;
}
}
return _formDataSupportsSubmitter;
}

export interface SubmitOptions {
Expand Down Expand Up @@ -257,7 +267,7 @@ export function getFormSubmissionInfo(
// then tack on the submitter value at the end. This is a lightweight
// solution that is not 100% spec compliant. For complete support in older
// browsers, consider using the `formdata-submitter-polyfill` package
if (!formDataSupportsSubmitter) {
if (!isFormDataSubmitterSupported()) {
let { name, type, value } = target;
if (type === "image") {
let prefix = name ? `${name}.` : "";
Expand Down
2 changes: 1 addition & 1 deletion packages/react-router/__tests__/setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fetch, Request, Response } from "@remix-run/web-fetch";
import { fetch, Request, Response, Headers } from "@remix-run/web-fetch";

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

0 comments on commit 19a71df

Please sign in to comment.