Skip to content

Hook into router config loading #31800

Open

Description

@Necroskillz

🚀 feature request

Relevant Package

This feature request is for @angular/router

Description

Router.resetConfig allows you to basically post process router at application startup - e.g. replace static placeholders, translate urls, mass add route data etc.). However you don't get a chance to do this for lazy loaded components.

Describe the solution you'd like

Add some possibility to hook into the RouterConfigLoader when routes from the child modules are processed (e.g. a provider that would be called with the routes resolved from the just loaded module's injector - https://github.com/angular/angular/blob/master/packages/router/src/router_config_loader.ts#L43).

Describe alternatives you've considered

The way to get around this is implemented in localized-router: https://github.com/Greentube/localize-router/blob/master/src/localize-router-config-loader.ts. This is a hack in the first place and will not work with the new loadChildren syntax.

The solution which would support also the new syntax is here: https://github.com/gilsdav/ngx-translate-router/blob/master/projects/ngx-translate-router/src/lib/localized-router.ts which is an even bigger hack.

/cc @gilsdav @meeroslav Pls comment if you have any thoughts on this.

Activity

  1. added this to the needsTriage milestone on Jul 23, 2019
  2. meeroslav commented on Jul 25, 2019

    @meeroslav

    I would like to see a clean way to intercept the routes on lazy loading.

    While I don't consider the localize-router solution with overriding a System JS Loader a hack, it was clearly not future proof (although at that time best solution as discussed with @vsavkin).

    Linking the previous feature request here: #16282 as well as #14036

  3. imsingh commented on Jul 26, 2019

    @imsingh

    I was thinking about how to do this. Maybe it's too naive.

    What if RouterConfigLoader's loadModuleFactory takes a second argument modifier.

    modifier prototyope
    type Modifier = (factory: NgModuleFactory) => NgModuleFactory;

    Then Angular puts a default modifier which takes a NgModuleFactory and returns it as it, as following:

    modifier(factory: NgModuleFactory) => {
        return factory;
    }
    

    but then the user can create their own modifier and provide it within the provider's array.

    The loadModuleFactory would look something like this:

    private loadModuleFactory(loadChildren: LoadChildren, modifier: Modifier): Observable<NgModuleFactory<any>> {
        if (typeof loadChildren === 'string') {
          return from(modifier(this.loader.load(loadChildren)));
        } else {
          return wrapIntoObservable(loadChildren()).pipe(mergeMap((t: any) => {
            if (t instanceof NgModuleFactory) {
              return of (modifier(t));
            } else {
              return from(modifier(this.compiler.compileModuleAsync(t)));
            }
          }));
        }
      }
    
  4. added
    feature
    on Nov 8, 2019
  5. modified the milestones: needsTriage, Backlog on Nov 8, 2019
  6. angular-robot commented on Jun 4, 2021

    @angular-robot
    Contributor

    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.

  7. angular-robot commented on Jun 24, 2021

    @angular-robot
    Contributor

    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.

  8. angular-robot commented on Apr 15, 2022

    @angular-robot
    Contributor

    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.

  9. added
    canonical
    on Sep 22, 2022
  10. added
    feature: under consideration
    and removed on Oct 3, 2022
  11. muuvmuuv commented on Dec 3, 2025

    @muuvmuuv

    It is already possible to get access to it, here a workaround from my lib, but its not typed as its not exported in the public api. If we could change that and export it, that would be awesome!

    https://github.com/muuvmuuv/angular-router-menus/blob/main/projects/angular-router-menus/src/lib/builder.ts#L24

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: routercanonicalfeaturefeature: under considerationfreq1: low

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions