Question presented to candidate:
"We lazy load an admin reports screen with loadComponent and protect it with a functional guard. A security review says the admin JavaScript still downloads for normal users. Walk me through how functional guards and loadComponent interact in modern Angular, and how you would fix this."
What a strong answer should cover:
loadComponentpoints a route at a lazily imported standalone component; the loader runs at most once and its result is cached.- A functional guard is a function using
inject(); which kind of guard you attach decides when it runs relative to the lazy download. canMatchruns while matching: if it refuses, the route entry is skipped, the next entry with the same path can match (an "upsell" or 404 page), and the lazy component is not downloaded.canActivateruns after matching; forloadComponentthe loader ran only after the guard passed in Angular 22.2.3, but forloadChildrenthe children are loaded beforecanActivate, because they are needed for matching.- Preloading ignores
canMatch(only the deprecatedcanLoadblocks it), soPreloadAllModuleswould still download the chunk. - Route-level
providersare visible to the guards and the lazy component: the same instance.
Clarifying questions expected:
- "Is preloading enabled in this app?"
- "What should a non-admin see at that URL: a 404, a login page, or an upsell?"
Code / implementation expected: Yes β the same check as canMatch on one lazy route and canActivate on another, with loader calls logged.