Skip to content
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

Router initializer never completes if initial navigation fails #44355

Closed
znikola opened this issue Dec 2, 2021 · 4 comments
Closed

Router initializer never completes if initial navigation fails #44355

znikola opened this issue Dec 2, 2021 · 4 comments

Comments

@znikola
Copy link

@znikola znikola commented Dec 2, 2021

Which @angular/* package(s) are relevant/releated to the feature request?

router

Description

Rendering an app in SSR (using nguniversal) with an invalid URL (e.g. http://localhost:4200/electronics-spa/en/USD/Brands/Canon/c/brand_10%20or%20(1,2)=(select*from(select%20name_const(CHAR(82,88,106,99,113,78,74,70,73,118,87),1),name_const(CHAR(82,88,106,99,113,78,74,70,73,118,87),1))a)%20--%20and%201%3D1) causes the render to hang, and never release the taken resources.

The reason is that the navigation never completes on error, when the Router option initialNavigation is set to enabled.

Proposed solution

Expose RouterInitializer.
EDIT: or make the router complete the navigation on error, if that makes sense.

Alternatives considered

We came up with this workaround, but it would be nice if ɵangular_packages_router_router_h could be exposed.

import { Injectable, Injector, Provider } from '@angular/core';
import {
  NavigationError,
  Router,
  ROUTER_CONFIGURATION,
  ɵangular_packages_router_router_h as RouterInitializer,
} from '@angular/router';
import { filter, take } from 'rxjs/operators';

@Injectable()
export class CustomRouterInitializer extends RouterInitializer {
  appInitializer(): Promise<any> {
    const injector: Injector = (this as any).injector;
    const opts = injector.get(ROUTER_CONFIGURATION);

    if (
      opts.initialNavigation ===
      'enabled' /* for Ng11: || opts.initialNavigation === 'enabledBlocking' */
    ) {
      let resolve: any;
      const res = new Promise((r) => (resolve = r));

      injector
        .get(Router)
        .events.pipe(
          // We want to explicitly react on NavigationError event occurs just
          // after the first navigation start event
          take(2),
          filter((event) => event instanceof NavigationError)
        )
        .subscribe(() => resolve(false));

      super.appInitializer().then((val) => resolve(val));
      return res;
    }

    return super.appInitializer();
  }
}

export const routerFixProvider: Provider = {
  provide: RouterInitializer,
  useClass: CustomRouterInitializer,
};

The above workaround is for ng12.
I noticed that in ng13 there's no similar private API available, unless I'm missing something.

@ngbot ngbot bot added this to the needsTriage milestone Dec 3, 2021
@atscott atscott added the feature label Dec 16, 2021
@ngbot ngbot bot removed this from the needsTriage milestone Dec 16, 2021
@ngbot ngbot bot added this to the Backlog milestone Dec 16, 2021
@angular-robot
Copy link

@angular-robot angular-robot bot commented Dec 17, 2021

This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we'll move it to our consideration list.

You can find more details about the feature request process in our documentation.

@angular-robot
Copy link

@angular-robot angular-robot bot commented Jan 25, 2022

Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.

Find more details about Angular's feature request process in our documentation.

@angular-robot
Copy link

@angular-robot angular-robot bot commented Feb 14, 2022

Thank you for submitting your feature request! Looks like during the polling process it didn't collect a sufficient number of votes to move to the next stage.

We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular's scope, we'd encourage you to collaborate with the community on publishing it as an open source package.

You can find more details about the feature request process in our documentation.

@atscott atscott removed insufficient votes feature labels Apr 22, 2022
@ngbot ngbot bot removed this from the Backlog milestone Apr 22, 2022
@ngbot ngbot bot added this to the needsTriage milestone Apr 22, 2022
@atscott atscott changed the title Expose RouterInitializer Router initializer never completes if initial navigation fails Apr 22, 2022
@atscott
Copy link
Contributor

@atscott atscott commented Apr 22, 2022

reproduction: https://stackblitz.com/edit/angular-ivy-router-base-4rgd9r?file=src%2Fapp%2Fapp.module.ts

Opening the app to a URL that doesn't exist shows loading forever because the initial navigation fails and the APP_INITIALIZER never completes. This is a bug and should be fixed. I don't think exposing RouterInitializer is the right way to do that but instead should be fixed to not hang indefinitely.

atscott added a commit to atscott/angular that referenced this issue Apr 22, 2022
Previously, if initialNavigation were set to `enabledBlocking`, the
Router's `APP_INITIALIZER` would never resolve if that initial
navigation failed. This results in the application load hanging and
never completing.

fixes angular#44355
atscott added a commit to atscott/angular that referenced this issue Apr 22, 2022
Previously, if initialNavigation were set to `enabledBlocking`, the
Router's `APP_INITIALIZER` would never resolve if that initial
navigation failed. This results in the application load hanging and
never completing.

fixes angular#44355
atscott added a commit to atscott/angular that referenced this issue Apr 22, 2022
Previously, if initialNavigation were set to `enabledBlocking`, the
Router's `APP_INITIALIZER` would never resolve if that initial
navigation failed. This results in the application load hanging and
never completing.

fixes angular#44355
atscott added a commit to atscott/angular that referenced this issue Apr 22, 2022
Adds additional tests to verify `enabledBlocking` functionality. The
initial attempt to fix angular#44355 would have broken the scenario where a new
navigation cancels the initial navigation. We also cannot rely on
`NavigationError` like the example workaround in that issue report is
doing because there won't be one if a guard simply rejects
the initial nav (angular#16211).
atscott added a commit to atscott/angular that referenced this issue May 2, 2022
Adds additional tests to verify `enabledBlocking` functionality. The
initial attempt to fix angular#44355 would have broken the scenario where a new
navigation cancels the initial navigation. We also cannot rely on
`NavigationError` like the example workaround in that issue report is
doing because there won't be one if a guard simply rejects
the initial nav (angular#16211).
atscott added a commit to atscott/angular that referenced this issue May 3, 2022
Adds additional tests to verify `enabledBlocking` functionality. The
initial attempt to fix angular#44355 would have broken the scenario where a new
navigation cancels the initial navigation. We also cannot rely on
`NavigationError` like the example workaround in that issue report is
doing because there won't be one if a guard simply rejects
the initial nav (angular#16211).
atscott added a commit to atscott/angular that referenced this issue May 3, 2022
Adds additional tests to verify `enabledBlocking` functionality. The
initial attempt to fix angular#44355 would have broken the scenario where a new
navigation cancels the initial navigation. We also cannot rely on
`NavigationError` like the example workaround in that issue report is
doing because there won't be one if a guard simply rejects
the initial nav (angular#16211).
atscott added a commit to atscott/angular that referenced this issue May 17, 2022
Adds additional tests to verify `enabledBlocking` functionality. The
initial attempt to fix angular#44355 would have broken the scenario where a new
navigation cancels the initial navigation. We also cannot rely on
`NavigationError` like the example workaround in that issue report is
doing because there won't be one if a guard simply rejects
the initial nav (angular#16211).
atscott added a commit to atscott/angular that referenced this issue May 17, 2022
Adds additional tests to verify `enabledBlocking` functionality. The
initial attempt to fix angular#44355 would have broken the scenario where a new
navigation cancels the initial navigation. We also cannot rely on
`NavigationError` like the example workaround in that issue report is
doing because there won't be one if a guard simply rejects
the initial nav (angular#16211).
atscott added a commit to atscott/angular that referenced this issue May 17, 2022
…pletes

Previously, if `initialNavigation` were set to `enabledBlocking`, the
Router's `APP_INITIALIZER` would never resolve if that initial
navigation failed. This results in the application load hanging and
never completing.

fixes angular#44355
atscott added a commit to atscott/angular that referenced this issue May 17, 2022
…pletes

Previously, if `initialNavigation` were set to `enabledBlocking`, the
Router's `APP_INITIALIZER` would never resolve if that initial
navigation failed. This results in the application load hanging and
never completing.

fixes angular#44355
atscott added a commit to atscott/angular that referenced this issue May 17, 2022
Adds additional tests to verify `enabledBlocking` functionality. The
initial attempt to fix angular#44355 would have broken the scenario where a new
navigation cancels the initial navigation. We also cannot rely on
`NavigationError` like the example workaround in that issue report is
doing because there won't be one if a guard simply rejects
the initial nav (angular#16211).
atscott added a commit to atscott/angular that referenced this issue May 17, 2022
Adds additional tests to verify `enabledBlocking` functionality. The
initial attempt to fix angular#44355 would have broken the scenario where a new
navigation cancels the initial navigation. We also cannot rely on
`NavigationError` like the example workaround in that issue report is
doing because there won't be one if a guard simply rejects
the initial nav (angular#16211).
atscott added a commit to atscott/angular that referenced this issue May 17, 2022
…pletes

Previously, if `initialNavigation` were set to `enabledBlocking`, the
Router's `APP_INITIALIZER` would never resolve if that initial
navigation failed. This results in the application load hanging and
never completing.

fixes angular#44355
atscott added a commit to atscott/angular that referenced this issue May 17, 2022
…pletes

Previously, if `initialNavigation` were set to `enabledBlocking`, the
Router's `APP_INITIALIZER` would never resolve if that initial
navigation failed. This results in the application load hanging and
never completing.

fixes angular#44355
jessicajaniuk pushed a commit that referenced this issue May 18, 2022
Adds additional tests to verify `enabledBlocking` functionality. The
initial attempt to fix #44355 would have broken the scenario where a new
navigation cancels the initial navigation. We also cannot rely on
`NavigationError` like the example workaround in that issue report is
doing because there won't be one if a guard simply rejects
the initial nav (#16211).

PR Close #45733
@atscott atscott reopened this May 18, 2022
atscott added a commit to atscott/angular that referenced this issue May 18, 2022
…pletes

Previously, if `initialNavigation` were set to `enabledBlocking`, the
Router's `APP_INITIALIZER` would never resolve if that initial
navigation failed. This results in the application load hanging and
never completing.

fixes angular#44355
atscott added a commit to atscott/angular that referenced this issue May 18, 2022
…pletes

Previously, if `initialNavigation` were set to `enabledBlocking`, the
Router's `APP_INITIALIZER` would never resolve if that initial
navigation failed. This results in the application load hanging and
never completing.

fixes angular#44355
atscott added a commit to atscott/angular that referenced this issue May 18, 2022
…pletes

Previously, if `initialNavigation` were set to `enabledBlocking`, the
Router's `APP_INITIALIZER` would never resolve if that initial
navigation failed. This results in the application load hanging and
never completing.

fixes angular#44355
atscott added a commit to atscott/angular that referenced this issue Jun 27, 2022
…pletes

Previously, if `initialNavigation` were set to `enabledBlocking`, the
Router's `APP_INITIALIZER` would never resolve if that initial
navigation failed. This results in the application load hanging and
never completing.

fixes angular#44355
atscott added a commit to atscott/angular that referenced this issue Jun 27, 2022
…pletes

Previously, if `initialNavigation` were set to `enabledBlocking`, the
Router's `APP_INITIALIZER` would never resolve if that initial
navigation failed. This results in the application load hanging and
never completing.

fixes angular#44355
atscott added a commit to atscott/angular that referenced this issue Jun 27, 2022
…pletes

Previously, if `initialNavigation` were set to `enabledBlocking`, the
Router's `APP_INITIALIZER` would never resolve if that initial
navigation failed. This results in the application load hanging and
never completing.

fixes angular#44355
@atscott atscott removed this from the needsTriage milestone Jun 27, 2022
@atscott atscott added this to the Fixit H1'2022 milestone Jun 27, 2022
atscott added a commit to atscott/angular that referenced this issue Jun 29, 2022
…pletes

Previously, if `initialNavigation` were set to `enabledBlocking`, the
Router's `APP_INITIALIZER` would never resolve if that initial
navigation failed. This results in the application load hanging and
never completing.

fixes angular#44355
atscott added a commit to atscott/angular that referenced this issue Jun 29, 2022
…pletes

Previously, if `initialNavigation` were set to `enabledBlocking`, the
Router's `APP_INITIALIZER` would never resolve if that initial
navigation failed. This results in the application load hanging and
never completing.

fixes angular#44355
atscott added a commit to atscott/angular that referenced this issue Jun 29, 2022
…pletes

Previously, if `initialNavigation` were set to `enabledBlocking`, the
Router's `APP_INITIALIZER` would never resolve if that initial
navigation failed. This results in the application load hanging and
never completing.

fixes angular#44355
atscott added a commit to atscott/angular that referenced this issue Jun 29, 2022
…pletes

Previously, if `initialNavigation` were set to `enabledBlocking`, the
Router's `APP_INITIALIZER` would never resolve if that initial
navigation failed. This results in the application load hanging and
never completing.

fixes angular#44355
atscott added a commit to atscott/angular that referenced this issue Jun 29, 2022
…pletes

Previously, if `initialNavigation` were set to `enabledBlocking`, the
Router's `APP_INITIALIZER` would never resolve if that initial
navigation failed. This results in the application load hanging and
never completing.

fixes angular#44355
atscott added a commit that referenced this issue Jun 29, 2022
…pletes (#46634)

Previously, if `initialNavigation` were set to `enabledBlocking`, the
Router's `APP_INITIALIZER` would never resolve if that initial
navigation failed. This results in the application load hanging and
never completing.

fixes #44355

PR Close #46634
atscott added a commit to atscott/angular that referenced this issue Jul 1, 2022
…pletes

Previously, if `initialNavigation` were set to `enabledBlocking`, the
Router's `APP_INITIALIZER` would never resolve if that initial
navigation failed. This results in the application load hanging and
never completing.

fixes angular#44355
atscott added a commit to atscott/angular that referenced this issue Jul 1, 2022
…pletes

Previously, if `initialNavigation` were set to `enabledBlocking`, the
Router's `APP_INITIALIZER` would never resolve if that initial
navigation failed. This results in the application load hanging and
never completing.

fixes angular#44355
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment