Repository: angular/angular-cli
File: packages/angular/ssr/src/utils/ng.ts (TypeScript)

Code before:
  let redirectTo: string | undefined;
  let hasNavigationError = true;

  try {
    let applicationRef: ApplicationRef;
    if (isNgModule(bootstrap)) {
      const moduleRef = await platformRef.bootstrapModule(bootstrap);
      applicationRef = moduleRef.injector.get(ApplicationRef);
    } else {
      applicationRef = await bootstrap({ platformRef });
    }

    // Block until application is stable.
    await applicationRef.whenStable();

    // This code protect againsts app destruction during bootstrapping which is a
    // valid case. We should not assume the `applicationRef` is not in destroyed state.
    // Calling `envInjector.get` would throw `NG0205: Injector has already been destroyed`.
    if (applicationRef.destroyed) {
      return {
        hasNavigationError,
        redirectTo: undefined,
        // TODO(alanagius): let's think on the content?
        // or should we use `Promise.resolve('...')`?
        content: () => Promise.reject(new Error('Application was destroyed during bootstrapping')),
      };
    }

    // TODO(alanagius): Find a way to avoid rendering here especially for redirects as any output will be discarded.
    const envInjector = applicationRef.injector;
    const routerIsProvided = !!envInjector.get(ActivatedRoute, null);
    const router = envInjector.get(Router);
    const lastSuccessfulNavigation = router.lastSuccessfulNavigation();

    if (!routerIsProvided) {
      hasNavigationError = false;
    } else if (lastSuccessfulNavigation?.finalUrl) {
      hasNavigationError = false;

      const requestPrefix =
        envInjector.get(APP_BASE_HREF, null, { optional: true }) ??
        envInjector.get(REQUEST, null, { optional: true })?.headers.get('X-Forwarded-Prefix');

      const { pathname, search, hash } = envInjector.get(PlatformLocation);
      const finalUrl = constructDecodedUrl({ pathname, search, hash }, requestPrefix);
      const urlToRenderString = constructDecodedUrl(urlToRender, requestPrefix);

      if (urlToRenderString !== finalUrl) {
        redirectTo = [pathname, search, hash].join('');
      }
    }

Review comment (suggestion):
You could change the return type of this function to 

```ts
): Promise<
  | { hasNavigationError: boolean; redirectTo?: string; content: () => Promise<string> }
  | { hasNavigationError: true }
> {
```

and change the return to 
```suggestion
      return {
        hasNavigationError: true
     };
```

Code after:
    ...platformProviders,
  ]);

  let redirectTo: string | undefined;
  let hasNavigationError = true;

  try {
    let applicationRef: ApplicationRef;
    if (isNgModule(bootstrap)) {
      const moduleRef = await platformRef.bootstrapModule(bootstrap);
      applicationRef = moduleRef.injector.get(ApplicationRef);
    } else {
      applicationRef = await bootstrap({ platformRef });
    }

    // Block until application is stable.
    await applicationRef.whenStable();

    // This code protect against app destruction during bootstrapping which is a
    // valid case. We should not assume the `applicationRef` is not in destroyed state.
    // Calling `envInjector.get` would throw `NG0205: Injector has already been destroyed`.
    if (applicationRef.destroyed) {
      return { hasNavigationError: true };
    }

    // TODO(alanagius): Find a way to avoid rendering here especially for redirects as any output will be discarded.
    const envInjector = applicationRef.injector;
    const routerIsProvided = !!envInjector.get(ActivatedRoute, null);
    const router = envInjector.get(Router);
    const lastSuccessfulNavigation = router.lastSuccessfulNavigation();

    if (!routerIsProvided) {
      hasNavigationError = false;
    } else if (lastSuccessfulNavigation?.finalUrl) {
      hasNavigationError = false;

      const requestPrefix =
        envInjector.get(APP_BASE_HREF, null, { optional: true }) ??
        envInjector.get(REQUEST, null, { optional: true })?.headers.get('X-Forwarded-Prefix');

      const { pathname, search, hash } = envInjector.get(PlatformLocation);
      const finalUrl = constructDecodedUrl({ pathname, search, hash }, requestPrefix);
      const urlToRenderString = constructDecodedUrl(urlToRender, requestPrefix);

      if (urlToRenderString !== finalUrl) {
        redirectTo = [pathname, search, hash].join('');
      }
    }

    return {
      hasNavigationError,