Skip to content

Commit ac8eb5b

Browse files
committed
Revert "fix(router): Ensure canMatch guards run on wildcard routes (#53239)" (#53339)
This reverts commit 1940280. PR Close #53339
1 parent 2174138 commit ac8eb5b

File tree

4 files changed

+27
-31
lines changed

4 files changed

+27
-31
lines changed

packages/core/test/bundling/router/bundle.golden_symbols.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,9 @@
10971097
{
10981098
"name": "createUrlTreeFromSegmentGroup"
10991099
},
1100+
{
1101+
"name": "createWildcardMatchResult"
1102+
},
11001103
{
11011104
"name": "deactivateRouteAndItsChildren"
11021105
},

packages/router/src/recognize.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {EnvironmentInjector, Type, ɵRuntimeError as RuntimeError} from '@angular/core';
1010
import {from, Observable, of} from 'rxjs';
11-
import {catchError, concatMap, defaultIfEmpty, first, last, map, mergeMap, scan, switchMap, tap} from 'rxjs/operators';
11+
import {catchError, concatMap, defaultIfEmpty, first, last as rxjsLast, map, mergeMap, scan, switchMap, tap} from 'rxjs/operators';
1212

1313
import {AbsoluteRedirect, ApplyRedirects, canLoadFails, noMatch, NoMatch} from './apply_redirects';
1414
import {createUrlTreeFromSnapshot} from './create_url_tree';
@@ -19,8 +19,9 @@ import {RouterConfigLoader} from './router_config_loader';
1919
import {ActivatedRouteSnapshot, getInherited, ParamsInheritanceStrategy, RouterStateSnapshot} from './router_state';
2020
import {PRIMARY_OUTLET} from './shared';
2121
import {UrlSegment, UrlSegmentGroup, UrlSerializer, UrlTree} from './url_tree';
22+
import {last} from './utils/collection';
2223
import {getOutlet, sortByMatchingOutlets} from './utils/config';
23-
import {isImmediateMatch, match, matchWithChecks, noLeftoversInUrl, split} from './utils/config_matching';
24+
import {isImmediateMatch, match, MatchResult, matchWithChecks, noLeftoversInUrl, split} from './utils/config_matching';
2425
import {TreeNode} from './utils/tree';
2526
import {isEmptyError} from './utils/type_guards';
2627

@@ -161,7 +162,7 @@ export class Recognizer {
161162
return children;
162163
}),
163164
defaultIfEmpty(null as TreeNode<ActivatedRouteSnapshot>[] | null),
164-
last(),
165+
rxjsLast(),
165166
mergeMap(children => {
166167
if (children === null) return noMatch(segmentGroup);
167168
// Because we may have matched two outlets to the same empty path segment, we can have
@@ -234,7 +235,8 @@ export class Recognizer {
234235
consumedSegments,
235236
positionalParamSegments,
236237
remainingSegments,
237-
} = match(segmentGroup, route, segments);
238+
} = route.path === '**' ? createWildcardMatchResult(segments) :
239+
match(segmentGroup, route, segments);
238240
if (!matched) return noMatch(segmentGroup);
239241

240242
// TODO(atscott): Move all of this under an if(ngDevMode) as a breaking change and allow stack
@@ -266,13 +268,17 @@ export class Recognizer {
266268
matchSegmentAgainstRoute(
267269
injector: EnvironmentInjector, rawSegment: UrlSegmentGroup, route: Route,
268270
segments: UrlSegment[], outlet: string): Observable<TreeNode<ActivatedRouteSnapshot>> {
269-
const matchResult = matchWithChecks(rawSegment, route, segments, injector, this.urlSerializer);
271+
let matchResult: Observable<MatchResult>;
272+
270273
if (route.path === '**') {
274+
matchResult = of(createWildcardMatchResult(segments));
271275
// Prior versions of the route matching algorithm would stop matching at the wildcard route.
272276
// We should investigate a better strategy for any existing children. Otherwise, these
273277
// child segments are silently dropped from the navigation.
274278
// https://github.com/angular/angular/issues/40089
275279
rawSegment.children = {};
280+
} else {
281+
matchResult = matchWithChecks(rawSegment, route, segments, injector, this.urlSerializer);
276282
}
277283

278284
return matchResult.pipe(switchMap((result) => {
@@ -431,3 +437,13 @@ function getData(route: Route): Data {
431437
function getResolve(route: Route): ResolveData {
432438
return route.resolve || {};
433439
}
440+
441+
function createWildcardMatchResult(segments: UrlSegment[]): MatchResult {
442+
return {
443+
matched: true,
444+
parameters: segments.length > 0 ? last(segments)!.parameters : {},
445+
consumedSegments: segments,
446+
remainingSegments: [],
447+
positionalParamSegments: {},
448+
};
449+
}

packages/router/src/utils/config_matching.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {runCanMatchGuards} from '../operators/check_guards';
1515
import {defaultUrlMatcher, PRIMARY_OUTLET} from '../shared';
1616
import {UrlSegment, UrlSegmentGroup, UrlSerializer} from '../url_tree';
1717

18-
import {last} from './collection';
1918
import {getOrCreateRouteInjectorIfNeeded, getOutlet} from './config';
2019

2120
export interface MatchResult {
@@ -53,10 +52,6 @@ export function matchWithChecks(
5352

5453
export function match(
5554
segmentGroup: UrlSegmentGroup, route: Route, segments: UrlSegment[]): MatchResult {
56-
if (route.path === '**') {
57-
return createWildcardMatchResult(segments);
58-
}
59-
6055
if (route.path === '') {
6156
if (route.pathMatch === 'full' && (segmentGroup.hasChildren() || segments.length > 0)) {
6257
return {...noMatch};
@@ -93,16 +88,6 @@ export function match(
9388
};
9489
}
9590

96-
function createWildcardMatchResult(segments: UrlSegment[]): MatchResult {
97-
return {
98-
matched: true,
99-
parameters: segments.length > 0 ? last(segments)!.parameters : {},
100-
consumedSegments: segments,
101-
remainingSegments: [],
102-
positionalParamSegments: {},
103-
};
104-
}
105-
10691
export function split(
10792
segmentGroup: UrlSegmentGroup, consumedSegments: UrlSegment[], slicedSegments: UrlSegment[],
10893
config: Route[]) {
@@ -198,6 +183,9 @@ export function isImmediateMatch(
198183
(outlet === PRIMARY_OUTLET || !emptyPathMatch(rawSegment, segments, route))) {
199184
return false;
200185
}
186+
if (route.path === '**') {
187+
return true;
188+
}
201189
return match(rawSegment, route, segments).matched;
202190
}
203191

packages/router/test/recognize.spec.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -653,17 +653,6 @@ describe('recognize', async () => {
653653
expect(s.root.fragment).toEqual('f1');
654654
});
655655
});
656-
657-
describe('guards', () => {
658-
it('should run canMatch guards on wildcard routes', async () => {
659-
const config = [
660-
{path: '**', component: ComponentA, data: {id: 'a'}, canMatch: [() => false]},
661-
{path: '**', component: ComponentB, data: {id: 'b'}}
662-
];
663-
const s = await recognize(config, 'a');
664-
expect(s.root.firstChild!.data['id']).toEqual('b');
665-
});
666-
});
667656
});
668657

669658
async function recognize(

0 commit comments

Comments
 (0)