A Guide on How to Cancel Duplicate Fetch Requests in JavaScript Enhanced Forms

Written by austingil | Published 2023/02/10
Tech Story Tags: web-development | javascript | webdev | programming | frontend | backend | software-development | hackernoon-top-story

TLDRThereā€™s a good chance youā€™ve accidentally introduced a duplicate-request/race-condition bug. Today, Iā€™ll walk you through the issue and my recommendations to avoid it. The key issue here is the`event.preventDefault()`. This method prevents the browser from performing the default behavior of loading the new page and submitting the form.via the TL;DR App

If youā€™ve ever used JavaScriptĀ fetchĀ API to enhance a form submission, thereā€™s a good chance youā€™ve accidentally introduced a duplicate-request/race-condition bug. Today, Iā€™ll walk you through the issue and my recommendations to avoid it.

(Video at the end if you prefer that)

Letā€™s consider a very basicĀ HTML formĀ with a single input and a submit button.

<form method="post">
  <label for="name">Name</label>
  <input id="name" name="name" />
  <button>Submit</button>
</form>

When we hit the submit button, the browser will do a whole page refresh.

Notice how the browser reloads after the submit button is clicked.

The page refresh isnā€™t always the experience we want to offer our users, so a common alternative is to useĀ JavaScriptĀ to add an event listener to the formā€™s ā€œsubmitā€ event, prevent the default behavior, and submit the form data using theĀ fetchĀ API.

A simplistic approach might look like the example below.

After the page (or component) mounts, we grab the form DOM node, add an event listener that constructs aĀ fetchĀ request using the formĀ action,Ā method, andĀ data, and at the end of the handler, we call the eventā€™sĀ preventDefault()Ā method.

const form = document.querySelector('form');
form.addEventListener('submit', handleSubmit);

function handleSubmit(event) {
  const form = event.currentTarget;
  fetch(form.action, {
    method: form.method,
    body: new FormData(form)
  });

  event.preventDefault();
}

Now, before any JavaScript hotshots start tweeting at me about GET vs. POST and request body andĀ Content-TypeĀ and whatever else, let me just say, I know. Iā€™m keeping theĀ fetchĀ request deliberately simple because thatā€™s not the main focus.

The key issue here is theĀ event.preventDefault(). This method prevents the browser from performing the default behavior of loading the new page and submitting the form.

Now, if we look at the screen and hit submit, we can see that the page doesnā€™t reload, but we do see the HTTP request in our network tab.

Notice the browser does not do a full page reload.

Unfortunately, by using JavaScript to prevent the default behavior, weā€™ve actually introduced a bug that the default browser behavior does not have.

When we use plainĀ HTMLĀ and you smash the submit button a bunch of times really quickly, youā€™ll notice that all the network requests except the most recent one turn red. This indicates that they were canceled, and only the most recent request is honored.

If we compare that to the JavaScript example, we will see that all of the requests are sent, and all of them are complete without any being canceled.

This may be an issue because although each request may take a different amount of time, they could resolve in a different order than they were initiated. This means if we add functionality to the resolution of those requests, we might have some unexpected behavior.

As an example, we could create a variable to increment for each request (ā€œtotalRequestCountā€œ). Every time we run theĀ handleSubmitĀ function, we can increment the total count as well as capture the current number to track the current request (ā€œthisRequestNumberā€œ).

When aĀ fetchĀ request resolves, we can log its corresponding number to the console.

const form = document.querySelector('form');
form.addEventListener('submit', handleSubmit);

let totalRequestCount = 0

function handleSubmit(event) {
  totalRequestCount += 1
  const thisRequestNumber = totalRequestCount 
  const form = event.currentTarget;
  fetch(form.action, {
    method: form.method,
    body: new FormData(form)
  }).then(() => {
    console.log(thisRequestNumber)
  })
  event.preventDefault();
}

Now, if we smash that submit button a bunch of times, we might see different numbers printed to the console out of order: 2, 3, 1, 4, 5. It depends on the network speed, but I think we can all agree that this is not ideal.

Consider a scenario where a user triggers severalĀ fetchĀ requests in close succession, and upon completion, your application updates the page with their changes. The user could ultimately see inaccurate information due to requests resolving out of order.

This is a non-issue in the non-JavaScript world because the browser cancels any previous request and loads the page after the most recent request completes, loading the most up-to-date version. But page refreshes are not as sexy.

The good news for JavaScript lovers is that we can have both aĀ sexy user experienceĀ AND a consistent UI!

We just need to do a bit more legwork.

If you look at theĀ fetchĀ API documentation, youā€™ll see that itā€™s possible to abort a fetch using anĀ AbortControllerĀ and theĀ signalĀ property of theĀ fetchĀ options. It looks something like this:

const controller = new AbortController();
fetch(url, { signal: controller.signal });

By providing theĀ AbortContollerā€˜s signal to theĀ fetchĀ request, we can cancel the request any time theĀ AbortContollerā€˜sĀ abortĀ method is triggered.

You can see a clearer example in the JavaScript console. Try creating anĀ AbortController, initiating theĀ fetchĀ request, then immediately executing theĀ abortĀ method.

const controller = new AbortController();
fetch('', { signal: controller.signal });
controller.abort()

You should immediately see an exception printed to the console. In Chromium browsers, it should say, ā€œUncaught (in promise) DOMException: The user aborted a request.ā€ And if you explore the Network tab, you should see a failed request with the Status Text ā€œ(canceled).ā€

With that in mind, we can add anĀ AbortControllerĀ to our formā€™s submit handler. The logic will be as follows:

  • First, check for anĀ AbortControllerĀ for any previous requests. If one exists, abort it.

  • Next, create anĀ AbortControllerĀ for the current request that can be aborted on subsequent requests.

  • Finally, when a request resolves, remove its correspondingĀ AbortController.

There are several ways to do this, but Iā€™ll use aĀ WeakMapĀ to store relationships between each submittedĀ <form>Ā DOM node and its respectiveĀ AbortController. When a form is submitted, we can check and update theĀ WeakMapĀ accordingly.

const pendingForms = new WeakMap();

function handleSubmit(event) {
  const form = event.currentTarget;
  const previousController = pendingForms.get(form);

  if (previousController) {
    previousController.abort();
  }
    
  const controller = new AbortController();
  pendingForms.set(form, controller);

  fetch(form.action, {
    method: form.method,
    body: new FormData(form),
    signal: controller.signal,
  }).then(() => {
    pendingForms.delete(form);
  });
  event.preventDefault();
}

const forms = document.querySelectorAll('form');
for (const form of forms) {
  form.addEventListener('submit', handleSubmit);
}

The key thing is being able to associate an abort controller with its corresponding form. Using the formā€™s DOM node as theĀ WeakMapā€˜s key is a convenient way to do that.

With that in place, we can add theĀ AbortControllerā€˜s signal to theĀ fetchĀ request, abort any previous controllers, add new ones, and delete them upon completion.

Hopefully, that all makes sense.

Now, if we smash that formā€™s submit button a bunch of times, we can see that all of the API requests except the most recent one get canceled.

This means any function responding to that HTTP response will behave more as you would expect.

Now, if we use that same counting and logging logic we have above, we can smash the submit button seven times and would see six exceptions (due to theĀ AbortController) and one log of ā€œ7ā€ in the console.

If we submit again and allow enough time for the request to resolve, weā€™d see ā€œ8ā€ in the console. And if we smash the submit button a bunch of times, again, weā€™ll continue to see the exceptions and final request count in the right order.

If you want to add some more logic to avoid seeing DOMExceptions in the console when a request is aborted, you can add aĀ .catch()Ā block after yourĀ fetchĀ request and check if the errorā€™s name matches ā€œAbortErrorā€œ:

fetch(url, {
  signal: controller.signal,
}).catch((error) => {
  // If the request was aborted, do nothing
  if (error.name === 'AbortError') return;
  // Otherwise, handle the error here or throw it back to the console
  throw error
});

Closing

This whole post was focused on JavaScript-enhanced forms, but itā€™s probably a good idea to include anĀ AbortControllerĀ any time you create aĀ fetchĀ request. Itā€™s really too bad itā€™s not built into the API already. But hopefully, this shows you a good method for including it.

Itā€™s also worth mentioning that this approach does not prevent the user from spamming the submit button a bunch of times. The button is still clickable, and the request still fires off, it just provides a more consistent way of dealing with responses.

Unfortunately, if a userĀ doesĀ spam a submit button, those requests would still go to your backend and could use consume a bunch of unnecessary resources.

Some naive solutions may be disabling the submit button, using aĀ debounce, or only creating new requests after previous ones resolve. I donā€™t like these options because they rely on slowing down the userā€™s experience and only work on the client side.

They donā€™t address abuse via scripted requests.

To address abuse from too many requests to your server, you would probably want to set up someĀ rate limiting. That goes beyond the scope of this post, but it was worth mentioning.

Itā€™s also worth mentioning that rate limiting doesnā€™t solve the original problem of duplicate requests, race conditions, and inconsistent UI updates. Ideally, we should use both to cover both ends.

Anyway, thatā€™s all Iā€™ve got for today. If you want to watch a video that covers this same subject, watch this.

https://www.youtube.com/watch?v=w8ZIoLnh1Dc&embedable=true

Thank you so much for reading. If you liked this article, pleaseĀ share it. It's one of the best ways to support me. You can alsoĀ sign up for my newsletterĀ orĀ follow me on TwitterĀ if you want to know when new articles are published.


Also published here.


Written by austingil | I want to help you build better websites. It's fun!
Published by HackerNoon on 2023/02/10