Skip to content

Commit 5128ba0

Browse files
committed
refactor(router): Warn if a navigation will change in the upcoming v16 release (#48688)
v16 will have a breaking change to the way `UrlTree`s are constructed. This change is actually a bug fix that makes `UrlTree` creation correct in more scenarios (see #48508). However, this can affect applications that are relying on the current incorrect behavior. This commit adds a dev mode warning when the target of a navigation will change once #48508 is submitted. PR Close #48688
1 parent 4b8b5e3 commit 5128ba0

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
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
@@ -890,6 +890,9 @@
890890
{
891891
"name": "createTemplateRef"
892892
},
893+
{
894+
"name": "createUrlTree"
895+
},
893896
{
894897
"name": "deactivateRouteAndItsChildren"
895898
},

packages/router/src/create_url_tree_strategy.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,20 @@ export class LegacyCreateUrlTree implements CreateUrlTreeStrategy {
2121
relativeTo: ActivatedRoute|null|undefined, currentState: RouterState, currentUrlTree: UrlTree,
2222
commands: any[], queryParams: Params|null, fragment: string|null): UrlTree {
2323
const a = relativeTo || currentState.root;
24-
return createUrlTree(a, currentUrlTree, commands, queryParams, fragment);
24+
const tree = createUrlTree(a, currentUrlTree, commands, queryParams, fragment);
25+
if (NG_DEV_MODE) {
26+
const treeFromSnapshotStrategy = new CreateUrlTreeUsingSnapshot().createUrlTree(
27+
relativeTo, currentState, currentUrlTree, commands, queryParams, fragment);
28+
if (treeFromSnapshotStrategy.toString() !== tree.toString()) {
29+
let warningString = `The navigation to ${tree.toString()} will instead go to ${
30+
treeFromSnapshotStrategy.toString()} in an upcoming version of Angular.`;
31+
if (!!relativeTo) {
32+
warningString += ' `relativeTo` might need to be removed from the `UrlCreationOptions`.';
33+
}
34+
tree._warnIfUsedForNavigation = warningString;
35+
}
36+
}
37+
return tree;
2538
}
2639
}
2740

packages/router/src/router.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -555,10 +555,14 @@ export class Router {
555555
navigateByUrl(url: string|UrlTree, extras: NavigationBehaviorOptions = {
556556
skipLocationChange: false
557557
}): Promise<boolean> {
558-
if (typeof ngDevMode === 'undefined' ||
559-
ngDevMode && this.isNgZoneEnabled && !NgZone.isInAngularZone()) {
560-
this.console.warn(
561-
`Navigation triggered outside Angular zone, did you forget to call 'ngZone.run()'?`);
558+
if (NG_DEV_MODE) {
559+
if (this.isNgZoneEnabled && !NgZone.isInAngularZone()) {
560+
this.console.warn(
561+
`Navigation triggered outside Angular zone, did you forget to call 'ngZone.run()'?`);
562+
}
563+
if (url instanceof UrlTree && url._warnIfUsedForNavigation) {
564+
this.console.warn(url._warnIfUsedForNavigation);
565+
}
562566
}
563567

564568
const urlTree = isUrlTree(url) ? url : this.parseUrl(url);

packages/router/src/url_tree.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ function matrixParamsMatch(
190190
export class UrlTree {
191191
/** @internal */
192192
_queryParamMap?: ParamMap;
193+
/** @internal */
194+
_warnIfUsedForNavigation?: string;
193195

194196
constructor(
195197
/** The root segment group of the URL tree */

0 commit comments

Comments
 (0)