Skip to content
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

Standalone components loadChildren() not calling canLoad #50224

Closed
AbdealiLoKo opened this issue May 9, 2023 · 3 comments
Closed

Standalone components loadChildren() not calling canLoad #50224

AbdealiLoKo opened this issue May 9, 2023 · 3 comments

Comments

@AbdealiLoKo
Copy link

AbdealiLoKo commented May 9, 2023

Which @angular/* package(s) are the source of the bug?

router

Is this a regression?

No

Description

Moving this SO question to the angular tracker.

I was trying to use standalone components instead of modules based on the "Virtual NgModule mental model" described in #43784

I had a module which had just 1 component in it, and decided to replace that with the standalone component instead

On doing that, I noticed that the canLoad guard was not getting called.
And after some searching saw that in the loadComponent PR the last point mentioned was:

CanLoad guards do not apply to loadComponent. canActivate should be used instead, just like you would do if it were simply component instead.

I could not find this mentioned anywhere in the documentation, I checked:

I couldn't conclude if this is an expected behavior or a bug ...
And if expected is this something planned for standalone components or am I using standalone components for something it should not be using

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/angular-standalone-component-canload

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 15.2.2
Node: 16.18.1
Package Manager: npm 9.6.0
OS: linux x64

Angular: 15.2.2
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1502.2
@angular-devkit/build-angular   15.2.2
@angular-devkit/core            15.2.2
@angular-devkit/schematics      15.2.2
@schematics/angular             15.2.7
rxjs                            7.8.1
typescript                      4.9.5

Anything else?

NOTE: The original SO post says canMatch does not work either - but seemed to work for me.

Looks like canMatch() works but canLoad() does not work from my experiments in: https://stackblitz.com/edit/angular-standalone-component-canload

@AbdealiLoKo AbdealiLoKo changed the title Standalone components loadChildren() not calling canLoad/canMatch Standalone components loadChildren() not calling canLoad May 9, 2023
@JeanMeche
Copy link
Member

JeanMeche commented May 10, 2023

Hi, just quoting the source code :

  // Note that `canLoad` is only checked as a condition that prevents `loadChildren` and not
  // `loadComponent`. `canLoad` guards only block loading of child routes by design. This
  // happens as a consequence of needing to descend into children for route matching immediately
  // while component loading is deferred until route activation. Because `canLoad` guards can
  // have side effects, we cannot execute them here so we instead skip preloading altogether
  // when present. Lastly, it remains to be decided whether `canLoad` should behave this way
  // at all. Code splitting and lazy loading is separate from client-side authorization checks
  // and should not be used as a security measure to prevent loading of code.

https://github.com/angular/angular/blob/cc286be7af31c7f999d6c469d3fd8f9becb28ce7/packages/router/src/router_preloader.ts#L113C32-L120

So the behavior you see is expected.

@ngbot ngbot bot added this to the needsTriage milestone May 10, 2023
@pkozlowski-opensource pkozlowski-opensource closed this as not planned Won't fix, can't repro, duplicate, stale May 10, 2023
@AbdealiLoKo
Copy link
Author

Understood, should that be documented in the docs ?
Or it doesn't really matter as it's deprecated anyway ?

@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jun 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants