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 • Allow to have a PreloadingStrategy in lazy loaded modules #20912
Comments
Not sure if it should be a core feature, but you can create your own preloadingStrategy. This is how I preload certain lazy modules: {
path: 'mypath', loadChildren: './path/to/my.module#MyModule',
data: { preload: true }
}, /**
* Preload Routes with data.preload true
* https://angular.io/guide/router#custom-preloading-strategy
*/
@Injectable()
export class SelectivePreloadingStrategy implements PreloadingStrategy {
preloadedModules: string[] = [];
preload(route: Route, load: () => Observable<any>): Observable<any> {
if (route.data && route.data['preload']) {
// add the route path to the preloaded module array
this.preloadedModules.push(route.path);
return load();
} else {
return Observable.of(null);
}
}
} imports: [
RouterModule.forRoot(routes, { preloadingStrategy: SelectivePreloadingStrategy })
], |
@arlowhite No I know about the |
Hi Jeremy I came back to see the status of this issue. I'd really like to have this implemented. In the mid time I leave this PreloadingStrategy I used to achieve lazy loading nested modules only when I wanted. First some context, the requirements of my app:
I had three types of "module loading"
I could achieve exactly what I wanted with this strategy:
Then in the AppRoutingModule:
If we take a look at each part:
If the module is set to always be preloaded, do it Else
If the route has the property What does the observer do each time permissions are emitted?
Finally if the module should not be preloaded It was my first project handling observers so probably there are better ways of achiving this, but this worked as a charm without performance nor memory issues. |
Even if you defined a different preloading strategy in a child, I found that preloading was too eager. If you preloaded your registration module, for example, then the app would also preload the page after registration—even when you never even went to to registration. Not ideal. I ended up implementing "checkpoints" in my routes, and define which checkpoint I need to reach before I preload a module. You can read more about it here |
Does anyone know if this feature request is being worked on in anyway? |
Why you want for forChild when preloadingStratergy is applied to forRoot, indirectly applies to all forChild also ? |
We need it too! In case when I start loading my app from a page that has SelectivePreloadingStrategy in forRoot - it doesn't apply same strategy for it's |
In my case I have 2 main lazy loaded module that lazy loads route childrens.
It logs only 'I exist!' I really wish that preloadingStratergy applied to forRoot, indirectly applies to all forChild also but it's not. Angular version: 7.2.0 |
@wideLandscape You probably need to return load(); |
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. |
Current behavior
Currently, we can only set a
PreloadingStrategy
fromforRoot
by usingconfig
like inRouterModule.forRoot(routes: Routes, config?: ExtraOptions)
.Why can't this be done in
forChild
RouterModule.forChild(routes: Routes)
?https://angular.io/api/router/RouterModule
Expected behavior
Being able to preload a module from another lazy loaded module.
What is the motivation / use case for changing the behavior?
My
registration
page is lazy loaded since only members subscribing need to load it.The page right after could be lazy loaded.
The text was updated successfully, but these errors were encountered: