Skip to content

Commit 907f9bd

Browse files
alan-agius4pkozlowski-opensource
authored andcommitted
refactor(router): produce error message when canMatch is used with redirectTo (angular#60958)
Redirects in the router are handled before `canMatch` guards are evaluated. As a result, `canMatch` will not run for routes that include a redirect. Instead of silently ignoring this misconfiguration, developers should be alerted to help them understand why it doesn't behave as expected. Closes: angular#60957 PR Close angular#60958
1 parent 953c4b2 commit 907f9bd

1 file changed

Lines changed: 16 additions & 12 deletions

File tree

packages/router/src/utils/config.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -141,25 +141,29 @@ function validateNode(route: Route, fullPath: string, requireStandaloneComponent
141141
`Invalid configuration of route '${fullPath}': children and loadChildren cannot be used together`,
142142
);
143143
}
144-
if (route.redirectTo && (route.component || route.loadComponent)) {
145-
throw new RuntimeError(
146-
RuntimeErrorCode.INVALID_ROUTE_CONFIG,
147-
`Invalid configuration of route '${fullPath}': redirectTo and component/loadComponent cannot be used together`,
148-
);
149-
}
150144
if (route.component && route.loadComponent) {
151145
throw new RuntimeError(
152146
RuntimeErrorCode.INVALID_ROUTE_CONFIG,
153147
`Invalid configuration of route '${fullPath}': component and loadComponent cannot be used together`,
154148
);
155149
}
156-
if (route.redirectTo && route.canActivate) {
157-
throw new RuntimeError(
158-
RuntimeErrorCode.INVALID_ROUTE_CONFIG,
159-
`Invalid configuration of route '${fullPath}': redirectTo and canActivate cannot be used together. Redirects happen before activation ` +
160-
`so canActivate will never be executed.`,
161-
);
150+
151+
if (route.redirectTo) {
152+
if (route.component || route.loadComponent) {
153+
throw new RuntimeError(
154+
RuntimeErrorCode.INVALID_ROUTE_CONFIG,
155+
`Invalid configuration of route '${fullPath}': redirectTo and component/loadComponent cannot be used together`,
156+
);
157+
}
158+
if (route.canMatch || route.canActivate) {
159+
throw new RuntimeError(
160+
RuntimeErrorCode.INVALID_ROUTE_CONFIG,
161+
`Invalid configuration of route '${fullPath}': redirectTo and ${route.canMatch ? 'canMatch' : 'canActivate'} cannot be used together.` +
162+
`Redirects happen before guards are executed.`,
163+
);
164+
}
162165
}
166+
163167
if (route.path && route.matcher) {
164168
throw new RuntimeError(
165169
RuntimeErrorCode.INVALID_ROUTE_CONFIG,

0 commit comments

Comments
 (0)