Skip to content

Provide information on loaded lazy routes on RouteConfigLoadEnd #16282

Closed

Description

@meeroslav

I'm submitting a ... (check one with "x")

[ ] bug report => search github for a similar issue or PR before submitting
[X] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior
When accessing modules lazily, RouteConfigLoadEnd gets fired once the factory gets loaded, but before routes are loaded and assigned to route._loadedConfig.

Expected behavior
RouteConfigLoadEnd should be fired after _loadedConfig is assigned, or return routes as a parameter if it's fired from RouterConfigLoader.

Change proposal:

return map.call(moduleFactory$, (factory: NgModuleFactory<any>) => {
  const module = factory.create(parentInjector);
  const routes = flatten(module.injector.get(ROUTES));

  if (this.onLoadEndListener) {
    this.onLoadEndListener(route, routes);
  }

  return new LoadedRouterConfig(routes, module);
});

What is the motivation / use case for changing the behavior?
Most common use case of RouteConfigLoadEnd is to react once lazy loaded routes are loaded. In localize-router we can use this information for routes localization. However if even is fired before routes are de facto available in _loadedConfig and/or it doesn't provide this information it makes the entire event unusable as it's not that different from RouteConfigLoadStart.

Issue is already mentioned here: #14036

  • Angular version: 4.0.X
  • Browser: all

  • Language: all

Activity

  1. added
    feature
    on Apr 26, 2017
  2. TaridaGeorge commented on Aug 30, 2017

    @TaridaGeorge

    +1

  3. marko999 commented on Nov 29, 2017

    @marko999

    +1
    important, also some router implementations like localize-router depending on this

  4. meeroslav commented on Nov 29, 2017

    @meeroslav
    Author

    @marko999, actually localize-router no longer depends on this.

    With the help from @vsavkin, there is a workaround using SystemJsNgModuleLoader that solves the lazy loading problem and does not require changes in the Router's code.

    So unless there are some other interested parties in this feature, this issue can be closed.

    @conor-mac-aoidh, @brandonroberts, @johnpapa, are you still interested in having this event as mentioned in #14036?

    Maybe extending SystemJsNgModuleLoader would work for you as well?:

    export class YourCustomLoader extends SystemJsNgModuleLoader {
    
      load(path: string): Promise<NgModuleFactory<any>> {
        return super.load(path).then((factory: NgModuleFactory<any>) => {
          return {
            moduleType: factory.moduleType,
            create: (parentInjector: Injector) => {
              const module = factory.create(parentInjector);
              const getMethod = module.injector.get.bind(module.injector);
    
              module.injector['get'] = (token: any) => {
                const getResult = getMethod(token);
    
                if (token === ROUTES) {
                  // This is the place where routes are loaded, but not yet returned
                  // Do some custom magic here, throw a custom event etc.
                  return getResult;
                } else {
                  return getResult;
                }
              };
              return module;
            }
          };
        });
      }
    }

    You can see how it was (mis)used in localize-router here.

  5. hdrdiab commented on Nov 30, 2017

    @hdrdiab

    Hi,
    I have a problem with the nested routes of a Lazy Loaded module,throwing an error when navigating between child routes, is that a normal behavior from the angular router? I mean will we see any solution other than we extending the SystemJsNgModuleLoader.

  6. added this to the Backlog milestone on Jan 23, 2018
  7. HMubaireek commented on Jun 3, 2019

    @HMubaireek

    The mentiond solution by @meeroslav doesn't work anymore as SystemJsNgModuleLoader got depricated with Angular 8

  8. 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.

  9. angular-robot commented on Jun 25, 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.

  10. atscott commented on Feb 1, 2022

    @atscott
    Contributor

    Closing as duplicate of #40010 / #44577 / #24069

  11. angular-automatic-lock-bot commented on Mar 4, 2022

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  12. locked and limited conversation to collaborators on Mar 4, 2022
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: routerfeaturerouter: lazy loading

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions