Skip to content

Commit 57e8412

Browse files
arturovtthePunderWoman
authored andcommitted
fix(http): check whether Zone is defined (#51119)
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
1 parent 3106054 commit 57e8412

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

packages/common/http/src/fetch.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ export class FetchBackend implements HttpBackend {
109109
let decoder: TextDecoder;
110110
let partialText: string|undefined;
111111

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

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

133-
reqZone.run(() => observer.next({
135+
const reportProgress = () => observer.next({
134136
type: HttpEventType.DownloadProgress,
135137
total: contentLength ? +contentLength : undefined,
136138
loaded: receivedLength,
137139
partialText,
138-
} as HttpDownloadProgressEvent));
140+
} as HttpDownloadProgressEvent);
141+
reqZone ? reqZone.run(reportProgress) : reportProgress();
139142
}
140143
}
141144
});

0 commit comments

Comments
 (0)