Ole
Ole

Reputation: 47132

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?

Upvotes: 3

Views: 322

Answers (2)

jonyeezs
jonyeezs

Reputation: 41

With Angular 7 and above. It could be possible to preload with a guard through a custom PreloadStrategy

Upvotes: 1

Reactgular
Reactgular

Reputation: 54811

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

Upvotes: 2

Related Questions