Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can modules using canLoad be preloaded?

This question has this example:

{
    path: 'admin',
    loadChildren: 
'app/admin/admin.module#AdminModule',
    canLoad: [AuthGuard]
}

Can the AdminModule still be preloaded / prefetched with the canLoad guard in place?

If not is the only other option to put the AuthGuard canActivate property on all the routes within the AdminModule?

like image 557
Ole Avatar asked Apr 11 '26 09:04

Ole


1 Answers

It doesn't really make sense, because preloading will happen eagerly when the application is started. So how would your AuthGuard know who the user is or what they have access to. It will happen before any services have had a chance to restore a previous session or communicate with anything.

canLoad and canActivate are asynchronous operations, and preloading is ineffective if it has to wait before it can preload.

Ask yourself this question. Do I need the application to change routes to the admin section very quickly. If you answer yes, then use canActivate and preload but if latency is fine and you want to save on bandwidth then use canLoad without preload.

With all that said, it is not supported according to the documentation.

The PreloadAllModules strategy does not load feature areas protected by a CanLoad guard. This is by design.

https://angular.io/guide/router#canload-blocks-preload


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!