Skip to content

Commit 080bbd2

Browse files
alan-agius4thePunderWoman
authored andcommitted
fix(http): create macrotask during request handling instead of load start (#50406)
This commit schedules the macrotask creation to happen before the XHR `loadStart` event. This is needed as in some cases, Zone.js becomes stable too early. With this commit, we also update the internal `createBackgroundMacroTask` method to use Zone.js `scheduleMacroTask` as otherwise the `setTimeout` would cause `fakeAsync` tests to fail due to pending timers. Closes #50405 PR Close #50406
1 parent 98e8fdf commit 080bbd2

1 file changed

Lines changed: 28 additions & 19 deletions

File tree

  • packages/common/http/src

‎packages/common/http/src/xhr.ts‎

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ export class HttpXhrBackend implements HttpBackend {
6060
`Cannot make a JSONP request without JSONP support. To fix the problem, either add the \`withJsonpSupport()\` call (if \`provideHttpClient()\` is used) or import the \`HttpClientJsonpModule\` in the root NgModule.`);
6161
}
6262

63+
// Schedule a macrotask. This will cause NgZone.isStable to be set to false,
64+
// Which delays server rendering until the request is completed.
65+
const macroTaskCanceller = createBackgroundMacroTask();
66+
6367
// Check whether this factory has a special function to load an XHR implementation
6468
// for various non-browser environments. We currently limit it to only `ServerXhr`
6569
// class, which needs to load an XHR implementation.
@@ -231,7 +235,11 @@ export class HttpXhrBackend implements HttpBackend {
231235
statusText: xhr.statusText || 'Unknown Error',
232236
url: url || undefined,
233237
});
238+
234239
observer.error(res);
240+
241+
// Cancel the background macrotask.
242+
macroTaskCanceller();
235243
};
236244

237245
// The sentHeaders flag tracks whether the HttpResponseHeaders event
@@ -309,35 +317,31 @@ export class HttpXhrBackend implements HttpBackend {
309317
}
310318
}
311319

312-
let macroTaskCanceller: VoidFunction|undefined;
313-
314320
/** Tear down logic to cancel the backround macrotask. */
315-
const onLoadStart = () => {
316-
macroTaskCanceller ??= createBackgroundMacroTask();
317-
};
318-
const onLoadEnd = () => {
319-
macroTaskCanceller?.();
320-
};
321+
const onLoadEnd = () => macroTaskCanceller();
321322

322-
xhr.addEventListener('loadstart', onLoadStart);
323323
xhr.addEventListener('loadend', onLoadEnd);
324324

325325
// Fire the request, and notify the event stream that it was fired.
326-
xhr.send(reqBody!);
326+
try {
327+
xhr.send(reqBody!);
328+
} catch (e: any) {
329+
onError(e);
330+
}
331+
327332
observer.next({type: HttpEventType.Sent});
328333
// This is the return from the Observable function, which is the
329334
// request cancellation handler.
330335
return () => {
331336
// On a cancellation, remove all registered event listeners.
332-
xhr.removeEventListener('loadstart', onLoadStart);
333337
xhr.removeEventListener('loadend', onLoadEnd);
334338
xhr.removeEventListener('error', onError);
335339
xhr.removeEventListener('abort', onError);
336340
xhr.removeEventListener('load', onLoad);
337341
xhr.removeEventListener('timeout', onError);
338342

339-
// Cancel the background macrotask.
340-
macroTaskCanceller?.();
343+
// Cancel the background macrotask.
344+
macroTaskCanceller();
341345

342346
if (req.reportProgress) {
343347
xhr.removeEventListener('progress', onDownProgress);
@@ -357,11 +361,8 @@ export class HttpXhrBackend implements HttpBackend {
357361
}
358362
}
359363

360-
// Cannot use `Number.MAX_VALUE` as it does not fit into a 32-bit signed integer.
361-
const MAX_INT = 2147483647;
362-
363364
/**
364-
* A method that creates a background macrotask of up to Number.MAX_VALUE.
365+
* A method that creates a background macrotask using Zone.js.
365366
*
366367
* This is so that Zone.js can intercept HTTP calls, this is important for server rendering,
367368
* as the application is only rendered once the application is stabilized, meaning there are pending
@@ -370,7 +371,15 @@ const MAX_INT = 2147483647;
370371
* @returns a callback method to cancel the macrotask.
371372
*/
372373
function createBackgroundMacroTask(): VoidFunction {
373-
const timeout = setTimeout(() => void 0, MAX_INT);
374+
// We use Zone.js when it's defined as otherwise a `setTimeout` will leave open timers which
375+
// causes `fakeAsync` tests to fail.
376+
const noop = () => {};
377+
if (typeof Zone !== 'undefined') {
378+
const zoneCurrent = Zone.current;
379+
const task = zoneCurrent.scheduleMacroTask('httpMacroTask', noop, undefined, noop, noop);
380+
381+
return () => zoneCurrent.cancelTask(task);
382+
}
374383

375-
return () => clearTimeout(timeout);
384+
return noop;
376385
}

0 commit comments

Comments
 (0)