Skip to content

fix: gracefully degrade when onUploadProgress lacks stream support#822

Open
wotan-allfather wants to merge 3 commits intosindresorhus:mainfrom
wotan-allfather:fix/graceful-degradation-upload-progress
Open

fix: gracefully degrade when onUploadProgress lacks stream support#822
wotan-allfather wants to merge 3 commits intosindresorhus:mainfrom
wotan-allfather:fix/graceful-degradation-upload-progress

Conversation

@wotan-allfather
Copy link

When environments don't support request streams (e.g., Safari, iOS WebView), the onUploadProgress callback now silently degrades instead of throwing an error. This allows requests to complete successfully without progress reporting in environments with partial ReadableStream support.

Closes #691

When environments don't support request streams (e.g., Safari, iOS WebView),
the onUploadProgress callback now silently degrades instead of throwing an error.
This allows requests to complete successfully without progress reporting in
environments with partial ReadableStream support.

Closes sindresorhus#691
@sholladay
Copy link
Collaborator

Do people really want it to fail silently? I was thinking we would still report 100% progress, if there's a simple way to implement it.

Per @sholladay's feedback: instead of silently degrading, report
100% progress completion after the request finishes in environments
where request streams aren't supported (Safari, iOS WebView).

This gives users feedback that their upload completed, even when
incremental progress tracking isn't available.

- Track body size when streams aren't supported
- Report { percent: 1, transferredBytes: totalBytes, totalBytes }
  after fetch completes
- Supports Blob, ArrayBuffer, TypedArray, string, URLSearchParams
- FormData reports totalBytes: 0 (unknown size)
@sindresorhus
Copy link
Owner

A couple of things:

  • Fallback progress is emitted even when the request is never sent. If a beforeRequest hook returns a Response, #fetch() exits early, but the new fallback still reports upload completion. That is an incorrect signal to consumers and should be gated to only fire after an actual network upload attempt.
  • Fallback progress is emitted even when there is no upload body. In non-stream environments, onUploadProgress will fire 100% for requests where request.body is empty (for example GET), but stream-enabled environments correctly skip upload progress when !request.body. This creates cross-environment behavior mismatch and false progress events.

1. Only emit fallback progress when there's actually a body to upload
   (skip GET/HEAD requests with no body)
2. Only emit fallback progress when an actual network request was made
   (not when beforeRequest hook returns a Response early)

This fixes the cross-environment behavior mismatch and false progress
events noted in the review.
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.

Enhance browser compatibility for ReadableStream in POST requests

3 participants