diff --git a/goldens/size-tracking/integration-payloads.json b/goldens/size-tracking/integration-payloads.json index 27214ec51c5..605a3035ab9 100644 --- a/goldens/size-tracking/integration-payloads.json +++ b/goldens/size-tracking/integration-payloads.json @@ -42,7 +42,7 @@ "master": { "uncompressed": { "runtime": 2843, - "main": 237487, + "main": 238453, "polyfills": 37064, "src_app_lazy_lazy_module_ts": 795 } @@ -68,4 +68,4 @@ } } } -} \ No newline at end of file +} diff --git a/packages/core/test/bundling/router/bundle.golden_symbols.json b/packages/core/test/bundling/router/bundle.golden_symbols.json index c03fdacc8fd..06559de6d0e 100644 --- a/packages/core/test/bundling/router/bundle.golden_symbols.json +++ b/packages/core/test/bundling/router/bundle.golden_symbols.json @@ -1145,6 +1145,9 @@ { "name": "defaultMalformedUriErrorHandler" }, + { + "name": "defaultRouterHook" + }, { "name": "defaultScheduler" }, diff --git a/packages/router/src/router.ts b/packages/router/src/router.ts index 9a16e9685f0..20e5736e8c7 100644 --- a/packages/router/src/router.ts +++ b/packages/router/src/router.ts @@ -327,6 +327,30 @@ export interface NavigationTransition { guardsResult: boolean|UrlTree|null; } +/** + * @internal + */ +export type RouterHook = (snapshot: RouterStateSnapshot, runExtras: { + appliedUrlTree: UrlTree, + rawUrlTree: UrlTree, + skipLocationChange: boolean, + replaceUrl: boolean, + navigationId: number +}) => Observable; + +/** + * @internal + */ +function defaultRouterHook(snapshot: RouterStateSnapshot, runExtras: { + appliedUrlTree: UrlTree, + rawUrlTree: UrlTree, + skipLocationChange: boolean, + replaceUrl: boolean, + navigationId: number +}): Observable { + return of(null) as any; +} + /** * The equivalent `IsActiveMatchOptions` options for `Router.isActive` is called with `true` * (exact = true). @@ -463,12 +487,16 @@ export class Router { private lastSuccessfulId: number = -1; /** - * Hook that enables you to pause navigation after the preactivation phase. + * Hooks that enable you to pause navigation, + * either before or after the preactivation phase. * Used by `RouterModule`. * * @internal */ - afterPreactivation: () => Observable = () => of(void 0); + hooks: { + beforePreactivation: RouterHook, + afterPreactivation: RouterHook + } = {beforePreactivation: defaultRouterHook, afterPreactivation: defaultRouterHook}; /** * A strategy for extracting and merging URLs. @@ -731,6 +759,24 @@ export class Router { } }), + // Before Preactivation + switchTap(t => { + const { + targetSnapshot, + id: navigationId, + extractedUrl: appliedUrlTree, + rawUrl: rawUrlTree, + extras: {skipLocationChange, replaceUrl} + } = t; + return this.hooks.beforePreactivation(targetSnapshot!, { + navigationId, + appliedUrlTree, + rawUrlTree, + skipLocationChange: !!skipLocationChange, + replaceUrl: !!replaceUrl, + }); + }), + // --- GUARDS --- tap(t => { const guardsStart = new GuardsCheckStart( @@ -808,7 +854,23 @@ export class Router { return undefined; }), - switchTap(() => this.afterPreactivation()), + // --- AFTER PREACTIVATION --- + switchTap((t: NavigationTransition) => { + const { + targetSnapshot, + id: navigationId, + extractedUrl: appliedUrlTree, + rawUrl: rawUrlTree, + extras: {skipLocationChange, replaceUrl} + } = t; + return this.hooks.afterPreactivation(targetSnapshot!, { + navigationId, + appliedUrlTree, + rawUrlTree, + skipLocationChange: !!skipLocationChange, + replaceUrl: !!replaceUrl, + }); + }), map((t: NavigationTransition) => { const targetRouterState = createRouterState( diff --git a/packages/router/src/router_module.ts b/packages/router/src/router_module.ts index 05461ab5cdd..6036a2efb49 100644 --- a/packages/router/src/router_module.ts +++ b/packages/router/src/router_module.ts @@ -527,7 +527,7 @@ export class RouterInitializer implements OnDestroy { } else if ( // TODO: enabled is deprecated as of v11, can be removed in v13 opts.initialNavigation === 'enabled' || opts.initialNavigation === 'enabledBlocking') { - router.afterPreactivation = () => { + router.hooks.afterPreactivation = () => { // only the initial navigation should be delayed if (!this.initNavigation) { this.initNavigation = true; @@ -536,7 +536,7 @@ export class RouterInitializer implements OnDestroy { // subsequent navigations should not be delayed } else { - return of(void 0); + return of(null) as any; } }; router.initialNavigation(); @@ -567,7 +567,7 @@ export class RouterInitializer implements OnDestroy { preloader.setUpPreloading(); routerScroller.init(); router.resetRootComponentType(ref.componentTypes[0]); - this.resultOfPreactivationDone.next(void 0); + this.resultOfPreactivationDone.next(null!); this.resultOfPreactivationDone.complete(); } diff --git a/packages/router/test/integration.spec.ts b/packages/router/test/integration.spec.ts index 23a04b2250a..fbe0ad14e13 100644 --- a/packages/router/test/integration.spec.ts +++ b/packages/router/test/integration.spec.ts @@ -871,7 +871,7 @@ describe('Integration', () => { }); - it('should eagerly update the URL with urlUpdateStrategy="eager"', + it('should eagerly update the URL with urlUpdateStrategy="eagar"', fakeAsync(inject([Router, Location], (router: Router, location: Location) => { const fixture = TestBed.createComponent(RootCmp); advance(fixture); @@ -885,21 +885,18 @@ describe('Integration', () => { expect(fixture.nativeElement).toHaveText('team 22 [ , right: ]'); router.urlUpdateStrategy = 'eager'; - router.events.subscribe(e => { - if (!(e instanceof GuardsCheckStart)) { - return; - } + (router as any).hooks.beforePreactivation = () => { expect(location.path()).toEqual('/team/33'); expect(fixture.nativeElement).toHaveText('team 22 [ , right: ]'); return of(null); - }); + }; router.navigateByUrl('/team/33'); advance(fixture); expect(fixture.nativeElement).toHaveText('team 33 [ , right: ]'); }))); - it('should eagerly update the URL with urlUpdateStrategy="eager"', + it('should eagerly update the URL with urlUpdateStrategy="eagar"', fakeAsync(inject([Router, Location], (router: Router, location: Location) => { const fixture = TestBed.createComponent(RootCmp); advance(fixture); @@ -929,7 +926,7 @@ describe('Integration', () => { expect(location.path()).toEqual('/login'); }))); - it('should set browserUrlTree with urlUpdateStrategy="eager" and false `shouldProcessUrl`', + it('should set browserUrlTree with urlUpdateStrategy="eagar" and false `shouldProcessUrl`', fakeAsync(inject([Router, Location], (router: Router, location: Location) => { const fixture = TestBed.createComponent(RootCmp); advance(fixture); @@ -956,7 +953,7 @@ describe('Integration', () => { expect((router as any).browserUrlTree.toString()).toBe('/team/22'); }))); - it('should eagerly update URL after redirects are applied with urlUpdateStrategy="eager"', + it('should eagerly update URL after redirects are applied with urlUpdateStrategy="eagar"', fakeAsync(inject([Router, Location], (router: Router, location: Location) => { const fixture = TestBed.createComponent(RootCmp); advance(fixture); @@ -990,7 +987,7 @@ describe('Integration', () => { expect(fixture.nativeElement).toHaveText('team 33 [ , right: ]'); }))); - it('should set `state` with urlUpdateStrategy="eager"', + it('should set `state` with urlUpdateStrategy="eagar"', fakeAsync(inject([Router, Location], (router: Router, location: SpyLocation) => { router.urlUpdateStrategy = 'eager'; router.resetConfig([