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 FormData submitter check #10627

Merged
merged 3 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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;
Comment on lines +132 to +145
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this to a function so it wouldn't try to execute in the module scope on the server and document wasn't defined

}

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