Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/src/hydration/event_replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@ export function withEventReplay(): Provider[] {

appsWithEventReplay.add(appRef);

const appId = injector.get(APP_ID);
appRef.onDestroy(() => {
appsWithEventReplay.delete(appRef);
// Ensure that we're always safe calling this in the browser.
if (typeof ngServerMode !== 'undefined' && !ngServerMode) {
const appId = injector.get(APP_ID);
// `_ejsa` should be deleted when the app is destroyed, ensuring that
// no elements are still captured in the global list and are not prevented
// from being garbage collected.
Expand Down
9 changes: 9 additions & 0 deletions packages/platform-server/test/event_replay_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,16 @@ describe('event replay', () => {
appRef.tick();
const appId = appRef.injector.get(APP_ID);

// Important: This is done intentionally because `ApplicationRef` registers
// `onDestroy` callbacks, and we want to ensure that they execute successfully
// without resulting in any errors. This is necessary because the bodies of
// these `onDestroy` callbacks use the `ngServerMode` variable.
// Prior to setting this flag, the unit test was throwing a "destroyed injector"
// error — but we weren't capturing it because we hadn't explicitly set the flag to false.
globalThis['ngServerMode'] = false;
appRef.destroy();
globalThis['ngServerMode'] = undefined;

// This ensure that `_ejsas` for the current application is cleaned up
// once the application is destroyed.
expect(window._ejsas![appId]).toBeUndefined();
Expand Down