From df85f3727f02605705a3d60c74b520f1e2180307 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Thu, 10 Dec 2020 07:59:06 -0800 Subject: [PATCH] refactor(router): Produce error message when canActivate is used with redirectTo (#40067) Redirects in the router are processed before activations. This means that a canActivate will never execute if a route has a redirect. Rather than silently ignoring the invalid config, developers should be notified so they know why it doesn't work. Closes #18605 The feature request for a function/class redirect is covered in #13373. PR Close #40067 --- packages/router/src/utils/config.ts | 6 ++++++ packages/router/test/config.spec.ts | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/packages/router/src/utils/config.ts b/packages/router/src/utils/config.ts index f8f53c6cc6..ed45e9b151 100644 --- a/packages/router/src/utils/config.ts +++ b/packages/router/src/utils/config.ts @@ -58,6 +58,12 @@ function validateNode(route: Route, fullPath: string): void { throw new Error(`Invalid configuration of route '${ fullPath}': redirectTo and component cannot be used together`); } + if (route.redirectTo && route.canActivate) { + throw new Error( + `Invalid configuration of route '${ + fullPath}': redirectTo and canActivate cannot be used together. Redirects happen before activation ` + + `so canActivate will never be executed.`); + } if (route.path && route.matcher) { throw new Error( `Invalid configuration of route '${fullPath}': path and matcher cannot be used together`); diff --git a/packages/router/test/config.spec.ts b/packages/router/test/config.spec.ts index 76d32f3f5a..248767d415 100644 --- a/packages/router/test/config.spec.ts +++ b/packages/router/test/config.spec.ts @@ -98,6 +98,15 @@ describe('config', () => { `Invalid configuration of route 'a': redirectTo and component cannot be used together`); }); + it('should throw when component and redirectTo are used together', () => { + expect(() => { + validateConfig([{path: 'a', redirectTo: 'b', canActivate: []}]); + }) + .toThrowError( + `Invalid configuration of route 'a': redirectTo and canActivate cannot be used together. ` + + `Redirects happen before activation so canActivate will never be executed.`); + }); + it('should throw when path and matcher are used together', () => { expect(() => { validateConfig([{path: 'a', matcher: 'someFunc', children: []}]);