question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

request.formdata will fail if request is issued by a fetch in page

See original GitHub issue

Library Affected: background-sync

Browser & Platform: Google Chrome for Linux

I have a page with a form. This form is submitted via a fetch() in the page. This fetch is captured by the SW and if the page is offline the catch will trigger, and the request will be queued. All is well until now. But, if I want to use request.clone().formData(), while offline, this will fail.

Now, if the form is submitted directly (without using a fetch) request.clone().formData() will work.

self.addEventListener('fetch', (event) => {
  // Clone the request to ensure it's safe to read when adding to the Queue.
  const promiseChain = fetch(event.request.clone()).catch(async(err) => {
    const formdata = await event.request.clone().formData()
     // will fail if the fetch is first issued from a page fetch
     // the request will fail with a 'Fail to fetch' error
    console.log(...formdata)
    return queue.pushRequest({request: event.request})
  });

  event.waitUntil(promiseChain);
});

The only difference is that in the first case (form submitted through fetch), the request.mode is “cors”, and in the second case (form submitted directly) the request.mode is “navigate”, which makes sense.

Is there a way to get the content of formData when request.mode is “cors” ?

Thank you for any insight. I know this happens before the call to pushRequest

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
planeth44commented, Mar 30, 2021

I think I solved this :

async function handleSubmit(e)
{
    e.preventDefault()
    const formDataToSubmit = await buildFormData(e)
   // more code
}
0reactions
K85commented, May 21, 2022

I think I solved this :

async function handleSubmit(e)
{
    e.preventDefault()
    const formDataToSubmit = await buildFormData(e)
   // more code
}

This works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Request formData in Service Worker - TypeError: Failed to fetch
I store the file in FormData and await request.formData() work just fine for most of the cases. But, if it fails for specific...
Read more >
Fetch API and FormData in HTML world | by Arun Rajeevan
By default,fetch() won't send or receive any cookies from the server, resulting in unauthenticated requests if the site relies on maintaining a user...
Read more >
Using the Fetch API - MDN Web Docs
Both methods of creating a copy will fail if the body of the original request or response has already been read, but reading...
Read more >
How to use fetch to POST form data as JSON to your API
This error normally occurs when the URL which you are making the fetch request to doesn't respond with JSON. If it is intentional...
Read more >
Fetch: Cross-Origin Requests - The Modern JavaScript Tutorial
If we send a fetch request to another web-site, it will probably fail. For instance, let's try fetching http://example.com :.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found