-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit wraps the tracking functionality to run outside of the Angular zone. Previously, it hindered server-side rendering and hydration, causing instability in the app. The app achieves stability when no microtasks or macrotasks are running. On the client side, this change also prevents unnecessary view updates when asynchronous tasks are set up by `trackPageView`.
- Loading branch information
Showing
2 changed files
with
29 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
projects/applicationinsights-angularplugin-js/src/lib/run-outside-angular.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// This would be exposed on the `globalThis` whenever `zone.js` is | ||
// included in the `polyfills` configuration property. Starting from Angular 17, | ||
// users can opt-in to use zoneless change detection. | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
declare const Zone: any; | ||
|
||
/** | ||
* The function that does the same job as `NgZone.runOutsideAngular`. | ||
* It doesn't require an injection context to be specified. | ||
* | ||
* ⚠️ Note: Most of the Application Insights functionality called from | ||
* inside the Angular execution context must be wrapped in this function. | ||
* Angular's rendering relies on asynchronous tasks being scheduled within | ||
* its execution context. | ||
* Since the plugin schedules tasks that do not interact with Angular's rendering, | ||
* it may prevent Angular from functioning reliably. Consequently, it may disrupt | ||
* processes such as server-side rendering or client-side hydration. | ||
*/ | ||
export const runOutsideAngular = <T>(callback: () => T): T => | ||
// Running the `callback` within the root execution context enables Angular | ||
// processes (such as SSR and hydration) to continue functioning normally without | ||
// timeouts and delays that could affect the user experience. | ||
typeof Zone !== "undefined" ? Zone.root.run(callback) : callback() | ||
; |