Skip to content

fix(http): omit fetch progress total when the response exposes Content-Encoding - #72229

Open
spokodev wants to merge 4 commits into
angular:mainfrom
spokodev:fix/fetch-progress-encoded-total
Open

spokodev wants to merge 4 commits into
angular:mainfrom
spokodev:fix/fetch-progress-encoded-total

Conversation

@spokodev

@spokodev spokodev commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.dev application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

FetchBackend uses Content-Length as the total of download progress events. When the response has a Content-Encoding such as gzip, Content-Length is the size of the compressed body, while loaded counts the decoded bytes read from the stream. A 1.2 MB JSON response served with gzip (100 KB on the wire) ends with loaded: 1197781 and total: 102460, so the progress example from the HTTP guide prints more downloaded bytes than the total. Measured with 22.3.0 in Chromium 145 and Firefox 146. With withXhr(), Chromium reports no total for the same response.

Issue Number: N/A

What is the new behavior?

FetchBackend leaves total undefined when the response headers include a Content-Encoding other than identity. Responses without one, or with identity, keep their total. The progress example in the HTTP guide now prints only the downloaded bytes when there is no total, and the guide explains when that happens.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

For a cross-origin response, the response headers only include Content-Encoding when the server lists it in Access-Control-Expose-Headers, so without that the wrong total remains.

…t-Encoding

When a response is sent with a `Content-Encoding` such as gzip, the
`Content-Length` header holds the size of the compressed body, while
the fetch stream yields the decompressed bytes. `FetchBackend` used
`Content-Length` as the `total` of its download progress events, so
`loaded` and `total` counted different bytes. For a 1.2 MB JSON body
served with gzip (100 KB on the wire), the events reported
`loaded: 1197781` with `total: 102460`.

Omit `total` when `Content-Encoding` is present in the response
headers, since the encoded size cannot be compared with the decoded
bytes read from the stream. This matches `HttpXhrBackend` in Chrome,
which reports no total for the same gzip response. A cross-origin
response only exposes `Content-Encoding` when the server lists it in
`Access-Control-Expose-Headers`.
@pullapprove
pullapprove Bot requested a review from crisbeto November 26, 2026 12:01
@angular-robot angular-robot Bot added area: docs Related to the documentation area: common/http Issues related to HTTP and HTTP Client labels Nov 26, 2026
@ngbot ngbot Bot added this to the Backlog milestone Nov 26, 2026

@JeanMeche JeanMeche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AGENT: Thanks for the PR! This addresses a real mismatch between compressed Content-Length and decompressed stream bytes in FetchBackend and brings it closer to HttpXhrBackend. I left two small suggestions inline.

Comment thread packages/common/http/src/fetch.ts Outdated
const contentLength = response.headers.get('content-length');
const contentLengthValue = contentLength !== null ? Number(contentLength) : NaN;
const total =
Number.isFinite(contentLengthValue) && !response.headers.has('content-encoding')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AGENT: response.headers.has('content-encoding') will also match Content-Encoding: identity (or an empty header value), where no decompression is applied and Content-Length still matches the decoded byte count.

Consider checking the normalized header value instead (e.g., treating "" and "identity" as unencoded):

const contentEncoding = response.headers.get('content-encoding')?.trim().toLowerCase();
const isEncoded = Boolean(contentEncoding && contentEncoding !== 'identity');
const total = Number.isFinite(contentLengthValue) && !isEncoded ? contentLengthValue : undefined;

It would also be great to add a companion test case in fetch_spec.ts verifying that Content-Encoding: identity still reports total.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 8a4eb51, using your suggestion. identity, an empty value and Identity with surrounding spaces now keep the total (checked in Chromium and Firefox), and fetch_spec.ts has a test for identity.

switch (event.type) {
case HttpEventType.DownloadProgress:
console.log('Downloaded ' + event.loaded + ' out of ' + event.total + ' bytes');
if (event.total !== undefined) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AGENT: Consider adding a brief sentence in the surrounding prose explaining why event.total can be undefined (for example, compressed or chunked responses where the uncompressed size is not known), so readers understand why this guard is needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a sentence after the example in 0baf4e8.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: common/http Issues related to HTTP and HTTP Client area: docs Related to the documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants