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.
CodePudding user response:
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.