Skip to content

Commit

Permalink
fix(http): check whether Zone is defined (#51119)
Browse files Browse the repository at this point in the history
Accessing the `Zone` variable without checking if it's defined or not
leads to an error "Zone is not defined" if zone.js is not imported (nooped).
This commit adds an additional check before getting the current zone where
the `doRequest` is being called.

PR Close #51119
  • Loading branch information
arturovt authored and thePunderWoman committed Jul 21, 2023
1 parent 3106054 commit 57e8412
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/common/http/src/fetch.ts
Expand Up @@ -109,7 +109,9 @@ export class FetchBackend implements HttpBackend {
let decoder: TextDecoder;
let partialText: string|undefined;

const reqZone = Zone.current;
// We have to check whether the Zone is defined in the global scope because this may be called
// when the zone is nooped.
const reqZone = typeof Zone !== 'undefined' && Zone.current;

// Perform response processing outside of Angular zone to
// ensure no excessive change detection runs are executed
Expand All @@ -130,12 +132,13 @@ export class FetchBackend implements HttpBackend {
(partialText ?? '') + (decoder ??= new TextDecoder).decode(value, {stream: true}) :
undefined;

reqZone.run(() => observer.next({
const reportProgress = () => observer.next({
type: HttpEventType.DownloadProgress,
total: contentLength ? +contentLength : undefined,
loaded: receivedLength,
partialText,
} as HttpDownloadProgressEvent));
} as HttpDownloadProgressEvent);
reqZone ? reqZone.run(reportProgress) : reportProgress();
}
}
});
Expand Down

0 comments on commit 57e8412

Please sign in to comment.