question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

canLoad is never fired when you use lazy loading, but as soon as you try to navigate manually it fires

See original GitHub issue

I’m submitting a…


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
[ ] Other... Please describe:

Current behavior

I have used canLoad in router along with canActivate, and the modules are lazy loaded for ex:

{
    path: "",
    loadChildren: "./admin/admin.module#AdminModule",
    canActivate: [AdminGuard],
    canLoad: [AdminGuard]
  },

Where my AdminGuard is:

import {Injectable} from "@angular/core";
import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router, CanLoad, Route} from "@angular/router";
import {Observable} from "rxjs";
import {AuthService} from "@core/services/auth.service";

@Injectable()
export class AdminGuard implements CanActivate, CanLoad {

  constructor(private router: Router, private authService: AuthService) {
  }

  canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
    console.log("AdminGuard->CanActivate:", state, this.isAdmin());
    return this.isAdmin();
  }

  canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean {
    console.log("AdminGuard->CanLoad:", route, this.isAdmin());
    return this.isAdmin();
  }

  isAdmin() {
    if (this.authService.isAdmin) {
      return true;
    } else {
      this.router.navigate(["/"]);
      return false;
    }
  }
}

I am using a PreloadAllModules as preloadingStrategy; all the modules are preloaded except the above mentioned module on which I have used canLoad. (I observed this because no console log is recorded). But as soon as I try to navigate to admin module I see the console log given below:


admin.guard.ts:18 AdminGuard->CanLoad: {path: "", loadChildren: "./admin/admin.module#AdminModule", canActivate: Array(1), canLoad: Array(1), data: {…}} true
admin.guard.ts:13 AdminGuard->CanActivate: RouterStateSnapshot {_root: TreeNode, url: "/manage/dashboard"} true
admin.guard.ts:13 AdminGuard->CanActivate: RouterStateSnapshot {_root: TreeNode, url: "/manage/dashboard"} true

Expected behavior

  • canLoad should be called while lazy loading the modules
  • there should be a way to trigger lazy loading again for all not loaded modules for example is current user is changed.

Environment


Angular version: 6.1.4
 
For Tooling issues:
- Node version: 8.11.4
- Platform:  Linux

StackBlitz Link

https://stackblitz.com/edit/angular-canload-bug

I have created two modules here one user and other admin. Admin module has been restricted using canLoad guard. But canLoad is never fired for it during lazy loading while user module is lazy loaded successfully(not having canLoad guard). But as soon as I try no navigate manually on admin route It gets fired. You can see all this in console.log

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
deepakgondacommented, Sep 13, 2018

But there should be a way so that preloader can call canLoad Method and check if module can be loaded and load that module if condition matches.

0reactions
angular-automatic-lock-bot[bot]commented, Sep 14, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does route guard canLoad not fire, but canActivate does
The navigation works and the canActivate route guard is hit whenever I try to navigate to the shiftmanage path. PROBLEM: The canLoad route...
Read more >
Relationship Loading Techniques
The loading of relationships falls into three categories; lazy loading, eager loading, and no loading. Lazy loading refers to objects are ...
Read more >
Loading Third-Party JavaScript - web.dev
Analytics scripts should never slow down your page load experience, but if you defer the load too long you can miss valuable analytics...
Read more >
Manually Lazy Load Modules And Components In Angular
In this blog post, I want to demonstrate how modules and components can be lazy-loaded at runtime using Angular 9+. The following StackBlitz...
Read more >
Ionic Navigation and Angular Routing: The Ultimate Guide
Note: You don't have to neither import nor reference the lazy loaded modules, just use the route loadChildren property. To be clear, what...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found