10000 fix(core): inject `APP_ID` before injector is destroyed (#61885) · angular/angular@67f657e · GitHub
[go: up one dir, main page]

Skip to content

Commit 67f657e

Browse files
arturovtpkozlowski-opensource
authored andcommitted
fix(core): inject APP_ID before injector is destroyed (#61885)
In this commit, we request `APP_ID` outside the `onDestroy` callback because the injector might already be in a destroyed state when the callback runs. PR Close #61885
1 parent de7aa60 commit 67f657e

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

packages/core/src/hydration/event_replay.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,11 @@ export function withEventReplay(): Provider[] {
145145

146146
appsWithEventReplay.add(appRef);
147147

148+
const appId = injector.get(APP_ID);
148149
appRef.onDestroy(() => {
149150
appsWithEventReplay.delete(appRef);
150151
// Ensure that we're always safe calling this in the browser.
151152
if (typeof ngServerMode !== 'undefined' && !ngServerMode) {
152-
const appId = injector.get(APP_ID);
153153
// `_ejsa` should be deleted when the app is destroyed, ensuring that
154154
// no elements are still captured in the global list and are not prevented
155155
// from being garbage collected.

packages/platform-server/test/event_replay_spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,16 @@ describe('event replay', () => {
207207
appRef.tick();
208208
const appId = appRef.injector.get(APP_ID);
209209

210+
// Important: This is done intentionally because `ApplicationRef` registers
211+
// `onDestroy` callbacks, and we want to ensure that they execute successfully
212+
// without resulting in any errors. This is necessary because the bodies of
213+
// these `onDestroy` callbacks use the `ngServerMode` variable.
214+
// Prior to setting this flag, the unit test was throwing a "destroyed injector"
215+
// error — but we weren't capturing it because we hadn't explicitly set the flag to false.
216+
globalThis['ngServerMode'] = false;
210217
appRef.destroy();
218+
globalThis['ngServerMode'] = undefined;
219+
211220
// This ensure that `_ejsas` for the current application is cleaned up
212221
// once the application is destroyed.
213222
expect(window._ejsas![appId]).toBeUndefined();

0 commit comments

Comments
 (0)
0