Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(router): Fix route recognition behavior with some versions of rxj… #47112

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/core/test/bundling/router/bundle.golden_symbols.json
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,9 @@
{
"name": "isDirectiveHost"
},
{
"name": "isEmptyError"
},
{
"name": "isFunction"
},
Expand Down
5 changes: 3 additions & 2 deletions packages/router/src/apply_redirects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import {EnvironmentInjector, ɵRuntimeError as RuntimeError} from '@angular/core';
import {EmptyError, from, Observable, of, throwError} from 'rxjs';
import {from, Observable, of, throwError} from 'rxjs';
import {catchError, concatMap, first, last, map, mergeMap, scan, switchMap, tap} from 'rxjs/operators';

import {RuntimeErrorCode} from './errors';
Expand All @@ -21,6 +21,7 @@ import {createRoot, squashSegmentGroup, UrlSegment, UrlSegmentGroup, UrlSerializ
import {forEach} from './utils/collection';
import {getOrCreateRouteInjectorIfNeeded, getOutlet, sortByMatchingOutlets} from './utils/config';
import {isImmediateMatch, match, matchWithChecks, noLeftoversInUrl, split} from './utils/config_matching';
import {isEmptyError} from './utils/type_guards';

const NG_DEV_MODE = typeof ngDevMode === 'undefined' || ngDevMode;

Expand Down Expand Up @@ -201,7 +202,7 @@ class ApplyRedirects {
}));
}),
first((s): s is UrlSegmentGroup => !!s), catchError((e: any, _: any) => {
if (e instanceof EmptyError || e.name === 'EmptyError') {
if (isEmptyError(e)) {
if (noLeftoversInUrl(segmentGroup, segments, outlet)) {
return of(new UrlSegmentGroup([], {}));
}
Expand Down
5 changes: 3 additions & 2 deletions packages/router/src/operators/resolve_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
*/

import {Injector} from '@angular/core';
import {EMPTY, EmptyError, from, MonoTypeOperatorFunction, Observable, of, throwError} from 'rxjs';
import {EMPTY, from, MonoTypeOperatorFunction, Observable, of, throwError} from 'rxjs';
import {catchError, concatMap, first, map, mapTo, mergeMap, takeLast, tap} from 'rxjs/operators';

import {ResolveData, Route} from '../models';
import {NavigationTransition} from '../router';
import {ActivatedRouteSnapshot, inheritedParamsDataResolve, RouterStateSnapshot} from '../router_state';
import {wrapIntoObservable} from '../utils/collection';
import {getToken} from '../utils/preactivation';
import {isEmptyError} from '../utils/type_guards';

/**
* A private symbol used to store the value of `Route.title` inside the `Route.data` if it is a
Expand Down Expand Up @@ -80,7 +81,7 @@ function resolveNode(
}))),
takeLast(1),
mapTo(data),
catchError((e: unknown) => e instanceof EmptyError ? EMPTY : throwError(e)),
catchError((e: unknown) => isEmptyError(e as Error) ? EMPTY : throwError(e)),
);
}

Expand Down
9 changes: 5 additions & 4 deletions packages/router/src/recognize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@
* found in the LICENSE file at https://angular.io/license
*/

import {createEnvironmentInjector, EnvironmentInjector, Type, ɵRuntimeError as RuntimeError} from '@angular/core';
import {EnvironmentInjector, Type, ɵRuntimeError as RuntimeError} from '@angular/core';
import {EmptyError, from, Observable, Observer, of} from 'rxjs';
import {catchError, concatMap, defaultIfEmpty, first, last as rxjsLast, map, scan, startWith, switchMap, takeLast, takeWhile} from 'rxjs/operators';
import {catchError, concatMap, defaultIfEmpty, first, last as rxjsLast, map, scan, switchMap, takeWhile} from 'rxjs/operators';

import {RuntimeErrorCode} from './errors';
import {Data, ResolveData, Route, Routes} from './models';
import {ActivatedRouteSnapshot, inheritedParamsDataResolve, ParamsInheritanceStrategy, RouterStateSnapshot} from './router_state';
import {PRIMARY_OUTLET} from './shared';
import {UrlSegment, UrlSegmentGroup, UrlSerializer, UrlTree} from './url_tree';
import {last} from './utils/collection';
import {getOrCreateRouteInjectorIfNeeded, getOutlet, sortByMatchingOutlets} from './utils/config';
import {getOutlet, sortByMatchingOutlets} from './utils/config';
import {isImmediateMatch, matchWithChecks, noLeftoversInUrl, split} from './utils/config_matching';
import {TreeNode} from './utils/tree';
import {isEmptyError} from './utils/type_guards';

const NG_DEV_MODE = typeof ngDevMode === 'undefined' || !!ngDevMode;

Expand Down Expand Up @@ -156,7 +157,7 @@ export class Recognizer {
r._injector ?? injector, r, segmentGroup, segments, outlet);
}),
first((x): x is TreeNode<ActivatedRouteSnapshot>[] => !!x), catchError(e => {
if (e instanceof EmptyError) {
if (isEmptyError(e)) {
if (noLeftoversInUrl(segmentGroup, segments, outlet)) {
return of([]);
}
Expand Down
6 changes: 6 additions & 0 deletions packages/router/src/utils/type_guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* found in the LICENSE file at https://angular.io/license
*/

import {EmptyError} from 'rxjs';

import {CanActivate, CanActivateChild, CanDeactivate, CanLoad, CanMatch} from '../models';
import {NAVIGATION_CANCELING_ERROR, NavigationCancelingError, RedirectingNavigationCancelingError} from '../navigation_canceling_error';
import {isUrlTree} from '../url_tree';
Expand Down Expand Up @@ -59,3 +61,7 @@ export function isRedirectingNavigationCancelingError(
export function isNavigationCancelingError(error: unknown): error is NavigationCancelingError {
return error && (error as any)[NAVIGATION_CANCELING_ERROR];
}

export function isEmptyError(e: Error): e is EmptyError {
return e instanceof EmptyError || e?.name === 'EmptyError';
}