Skip to content

Skip events that already have default prevented#27

Merged
koddsson merged 1 commit into
mainfrom
dg-skip-prevented-events
Jun 17, 2022
Merged

Skip events that already have default prevented#27
koddsson merged 1 commit into
mainfrom
dg-skip-prevented-events

Conversation

@dgreif
Copy link
Copy Markdown
Contributor

@dgreif dgreif commented Jun 17, 2022

There may be scenarios where you want to do client-side validation or have some other specialty handler for a form. In these scenarios, it would be nice to have an escape hatch that prevents remoteForm from handling the event.

There are also some rare (but possible) edge cases with code bundlers where an app could double load this module and execute it twice, creating two registered listeners for form submit events. In this scenario, both instances of remoteForm would trigger their handlers and send the fetch request, causing a double submission.

In both cases, a solution is to check if the event has already been handled by checking event.defaultPrevented. This allows custom handlers to prevent further action with the event, and handles the double load scenario because we already call preventDefault within remoteForm.

This should fix an outstanding issue in github/github where comment creation and other actions can be executed twice.

Note: We may need to consider this a breaking change as it could prevent forms from submitting in apps that are already preventing default somewhere. Not sure how we typically handle unintended side-effects like this 😅

@dgreif dgreif requested a review from a team as a code owner June 17, 2022 05:08
@dgreif dgreif requested a review from koddsson June 17, 2022 05:08
Copy link
Copy Markdown
Contributor

@koddsson koddsson left a comment

Choose a reason for hiding this comment

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

LGTM

@koddsson koddsson merged commit 35eee19 into main Jun 17, 2022
@koddsson koddsson deleted the dg-skip-prevented-events branch June 17, 2022 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants