Skip to content

Commit 7dd3bef

Browse files
committed
fix(@angular/ssr): check whether injector is destroyed
In this commit, we check whether the injector is destroyed before calling to `envInjector.get`. The application might be destroyed and `whenStable()` would resolve immediately; as thus, calling to `injector.get` is unsafe and should be guarded.
1 parent 51272f7 commit 7dd3bef

File tree

1 file changed

+6
-4
lines changed
  • packages/angular/ssr/src/utils

1 file changed

+6
-4
lines changed

packages/angular/ssr/src/utils/ng.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,15 @@ export async function renderAngular(
9696
// Block until application is stable.
9797
await applicationRef.whenStable();
9898

99+
const { destroyed } = applicationRef;
100+
99101
// TODO(alanagius): Find a way to avoid rendering here especially for redirects as any output will be discarded.
100102
const envInjector = applicationRef.injector;
101-
const routerIsProvided = !!envInjector.get(ActivatedRoute, null);
102-
const router = envInjector.get(Router);
103-
const lastSuccessfulNavigation = router.lastSuccessfulNavigation;
103+
const routerIsProvided = destroyed ? false : !!envInjector.get(ActivatedRoute, null);
104+
const router = destroyed ? null : envInjector.get(Router);
105+
const lastSuccessfulNavigation = router?.lastSuccessfulNavigation;
104106

105-
if (!routerIsProvided) {
107+
if (!router || !routerIsProvided) {
106108
hasNavigationError = false;
107109
} else if (lastSuccessfulNavigation?.finalUrl) {
108110
hasNavigationError = false;

0 commit comments

Comments
 (0)