3

I'm wondering if it is possible to resume an incomplete fetch request if the supposed request has failed due to a problem unrelated to the code, such as a lost network connection.

In a current project of mine, we transmit a large fetch download via AJAX to the client after the user logs in. This response is usually 70mb+ and can take some time to download. This is processed as a readable stream, and the download progress is displayed to the user.

My question, as it pertains to my issue, is if the user loses their network connection during this download, is it possible to resume from the point of failure or must the data be discarded and re-fetched?

I've done a good bit of reading into how the fetch API works, how browsers handle downloads and how the XMLHttpRequest object works under the hood of the fetch api. It appears that partial, incomplete downloads of files are supported by most major browsers (Looking at point #1 of this KeyCDN post) with special support from the web-server the file is sent from. However, I don't see any crossover of this behavior into the fetch API. Any insight would be appreciated.

Digglit
  • 576
  • 1
  • 4
  • 11
  • [Range Header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests) - instead of requesting all at once, load chunks and put them together. – Christopher Sep 10 '22 at 22:22
  • @Christopher thank you! That's exactly the solution I needed. – Digglit Sep 10 '22 at 22:44
  • @Christopher Is there a downside to splitting up a request into many smaller requests? More specifically, is the number of requests a variable needing considered? Or is the cumulative data quantity the only property needing considered? As I've implemented this, it's crossed my mind once or twice and I figure other people who view this question may wonder the same. – Digglit Sep 14 '22 at 19:10
  • 1
    It may be a bit slower because you need to reopen a connection after a chunk is complete. -- Using multiple downloads simultaneously isn't recommended: [almost all browsers have limitations](https://stackoverflow.com/a/985704/10304804) of max connections per host and total connections. -- But there is the [Network Information API](https://developer.mozilla.org/en-US/docs/Web/API/Network_Information_API) which helps you to decide, if the file should be downloaded in chunks (slow network) or completely (fast network). – Christopher Sep 14 '22 at 20:07

1 Answers1

1

As mentioned by @Christopher, HTTP Range Requests solve this problem perfectly.

In order to perform the range request, the server must support it by providing the following header:

Accept-Ranges: bytes

Then, on the client, we include the Range header in our request:

Range: bytes=0-1023

Where the number of bytes you'd like to receive in a particular request is up to you.

In the event that the server is responding with partial data, it will provide the status code 206, indicating Partial Content. More information can be found at the link provided.

Digglit
  • 576
  • 1
  • 4
  • 11