Skip to content

Commit 228e992

Browse files
committed
docs(router): Deprecate canLoad guards in favor of canMatch (#48180)
As mentioned in #46021, `canMatch` guards can replace `canLoad`. There are slight differences between the two but the purpose of preventing user access to feature modules is still achievable. There are several reasons keeping `CanLoad` around is detrimental to the API surface: * Lazy loading should not be an architectural feature of an application. It's an optimization you do for code size. That is, there should not be an architectural feature in the router to directly specifically control whether to lazy load something or not based on conditions such as authentication. This slightly different from the `canMatch` guard: the guard controls whether you can use the route at all and as a side-effect, whether we download the code. `CanLoad` only specified whether the code should be downloaded so `canMatch` is more powerful and more appropriate. * The naming of `CanLoad` will be potentially misunderstood for the `loadComponent` feature. Because it applies to `loadChildren`, it feels reasonable to think that it will also apply to `loadComponent`. This isn’t the case: since we don't need to load the component until right before activation, we defer the loading until all guards/resolvers have run. * Unnecessary API surface bloat where two features (CanMatch and CanLoad) do essentially the same thing. This affects code size for supporting two nearly identical features as well as the learning and teaching journey for them both. * `CanLoad` guards have the downside of _only_ being run once to prevent loading child routes. Once that passes and children are loaded, the guard never runs again. As a result, developers need to always provide _both_ canLoad and a canActivate in case the answer to the guard flips back to `false`. This is not the case for `canMatch`, which will run on every navigation. DEPRECATED: CanLoad guards in the Router are deprecated. Use CanMatch instead. PR Close #48180
1 parent 12db492 commit 228e992

14 files changed

Lines changed: 41 additions & 81 deletions

aio/content/examples/router/src/app/admin/admin-routing.module.2.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ const adminRoutes: Routes = [{
1717
canActivate: [authGuard],
1818

1919
// #enddocregion admin-route
20-
// #docregion can-match
21-
canMatch: [authGuard],
22-
// #enddocregion can-match
2320
// #docregion admin-route
2421
children: [{
2522
path: '',

aio/content/examples/router/src/app/app-routing.module.5.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const appRoutes: Routes = [
2020
path: 'admin',
2121
loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule),
2222
// #enddocregion admin-1
23-
canLoad: [authGuard]
23+
canMatch: [authGuard]
2424
// #docregion admin-1
2525
},
2626
// #enddocregion admin, admin-1

aio/content/examples/router/src/app/app-routing.module.6.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const appRoutes: Routes = [
2222
{
2323
path: 'admin',
2424
loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule),
25-
canLoad: [authGuard]
25+
canMatch: [authGuard]
2626
},
2727
{
2828
path: 'crisis-center',

aio/content/examples/router/src/app/app-routing.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const appRoutes: Routes = [
1818
{
1919
path: 'admin',
2020
loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule),
21-
canLoad: [authGuard]
21+
canMatch: [authGuard]
2222
},
2323
// #docregion preload-v2
2424
{

aio/content/examples/router/src/app/auth/auth.guard.1.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// #docregion
2-
import { CanActivateFn } from '@angular/router';
32

4-
export const authGuard: CanActivateFn = () => {
3+
export const authGuard = () => {
54
console.log('authGuard#canActivate called');
65
return true;
76
};

aio/content/examples/router/src/app/auth/auth.guard.2.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
// #docregion
22
import {inject} from '@angular/core';
3-
import {
4-
CanActivateFn, CanMatchFn,
5-
Router, UrlTree
6-
} from '@angular/router';
3+
import { Router } from '@angular/router';
74

85
import {AuthService} from './auth.service';
96

10-
export const authGuard: CanMatchFn|CanActivateFn = () => {
7+
export const authGuard = () => {
118
const authService = inject(AuthService);
129
const router = inject(Router);
1310

aio/content/examples/router/src/app/auth/auth.guard.3.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
// #docregion can-activate-child
21
import { inject } from '@angular/core';
3-
import {
4-
CanActivateFn, CanMatchFn, Router,
5-
CanActivateChildFn,
6-
UrlTree
7-
} from '@angular/router';
2+
import { Router } from '@angular/router';
83
import { AuthService } from './auth.service';
94

10-
export const authGuard: CanMatchFn|CanActivateFn|CanActivateChildFn = () => {
5+
export const authGuard = () => {
116
const authService = inject(AuthService);
127
const router = inject(Router);
138

aio/content/examples/router/src/app/auth/auth.guard.4.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
// #docplaster
22
// #docregion
33
import { inject } from '@angular/core';
4-
import {
5-
CanActivateFn, CanMatchFn, Router,
6-
CanActivateChildFn,
7-
NavigationExtras,
8-
UrlTree
9-
} from '@angular/router';
4+
import { Router, NavigationExtras } from '@angular/router';
105
import { AuthService } from './auth.service';
116

12-
export const authGuard: CanMatchFn|CanActivateFn|CanActivateChildFn = () => {
7+
export const authGuard = () => {
138
const authService = inject(AuthService);
149
const router = inject(Router);
1510

aio/content/examples/router/src/app/auth/auth.guard.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
// #docplaster
22
import { inject } from '@angular/core';
33
import {
4-
CanActivateFn, CanMatchFn, Router,
5-
CanActivateChildFn,
4+
Router,
65
NavigationExtras,
7-
CanLoadFn, UrlTree
86
} from '@angular/router';
97
import { AuthService } from './auth.service';
108

11-
// #docregion canLoad
12-
export const authGuard: CanMatchFn|CanActivateChildFn|CanActivateFn|CanLoadFn = () => {
13-
// #enddocregion canLoad
9+
export const authGuard = () => {
1410
const router = inject(Router);
1511
const authService = inject(AuthService);
1612

aio/content/examples/router/src/app/selective-preloading-strategy.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class SelectivePreloadingStrategyService implements PreloadingStrategy {
1010
preloadedModules: string[] = [];
1111

1212
preload(route: Route, load: () => Observable<any>): Observable<any> {
13-
if (route.data?.['preload'] && route.path != null) {
13+
if (route.canMatch === undefined && route.data?.['preload'] && route.path != null) {
1414
// add the route path to the preloaded module array
1515
this.preloadedModules.push(route.path);
1616

0 commit comments

Comments
 (0)