Skip to content

fix(core): inject APP_ID before injector is destroyed #61885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
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
fix(core): inject APP_ID before injector is destroyed
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.
  • Loading branch information
arturovt committed Jun 5, 2025
commit 6fead75a29f03fd9a48f93684b5082802fb26fda
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