Skip to content

Commit ae0a63a

Browse files
atscottAndrewKushnir
authored andcommitted
refactor(router): Move preloading to tree-shakeable provider (angular#46215)
Extracting preloading to a provider moves us towards thinking about a world where `ExtraOptions` doesn't exist to control behaviors that are opt-in/opt-out. Instead, these behaviors could be controlled by the presence (or lackthereof) of the providers which has the functionality. This is relevant to a world in which we no longer have the `RouterModule` but instead have something like `provideRouter` where the features are tree-shakeable. This change _does not_ make the `RouterPreloader` tree-shakeable inside `RouterModule.forRoot` because the compiler cannot statically determine whether it's needed. However, in the `provideRouter` world without `forRoot`, preloading could be exposed through the provider function instead, making the `RouterPreloader` and `PreloadingStrategy` implementations tree-shakeable for those that don't use it (which is the default - no preloading). PR Close angular#46215
1 parent 9a0e681 commit ae0a63a

File tree

7 files changed

+44
-35
lines changed

7 files changed

+44
-35
lines changed

goldens/public-api/router/index.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,10 @@ export class NavigationStart extends RouterEvent {
417417
export class NoPreloading implements PreloadingStrategy {
418418
// (undocumented)
419419
preload(route: Route, fn: () => Observable<any>): Observable<any>;
420+
// (undocumented)
421+
static ɵfac: i0.ɵɵFactoryDeclaration<NoPreloading, never>;
422+
// (undocumented)
423+
static ɵprov: i0.ɵɵInjectableDeclaration<NoPreloading>;
420424
}
421425

422426
// @public
@@ -452,6 +456,10 @@ export type Params = {
452456
export class PreloadAllModules implements PreloadingStrategy {
453457
// (undocumented)
454458
preload(route: Route, fn: () => Observable<any>): Observable<any>;
459+
// (undocumented)
460+
static ɵfac: i0.ɵɵFactoryDeclaration<PreloadAllModules, never>;
461+
// (undocumented)
462+
static ɵprov: i0.ɵɵInjectableDeclaration<PreloadAllModules>;
455463
}
456464

457465
// @public

goldens/public-api/router/testing/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class RouterTestingModule {
3737
export function setupTestingRouter(urlSerializer: UrlSerializer, contexts: ChildrenOutletContexts, location: Location_2, compiler: Compiler, injector: Injector, routes: Route[][], opts?: ExtraOptions | UrlHandlingStrategy | null, urlHandlingStrategy?: UrlHandlingStrategy, routeReuseStrategy?: RouteReuseStrategy, titleStrategy?: TitleStrategy): Router;
3838

3939
// @public
40-
export function setupTestingRouterInternal(urlSerializer: UrlSerializer, contexts: ChildrenOutletContexts, location: Location_2, compiler: Compiler, injector: Injector, routes: Route[][], opts?: ExtraOptions | UrlHandlingStrategy, urlHandlingStrategy?: UrlHandlingStrategy, routeReuseStrategy?: RouteReuseStrategy, defaultTitleStrategy?: DefaultTitleStrategy, titleStrategy?: TitleStrategy): Router;
40+
export function setupTestingRouterInternal(urlSerializer: UrlSerializer, contexts: ChildrenOutletContexts, location: Location_2, compiler: Compiler, injector: Injector, routes: Route[][], opts?: ExtraOptions | UrlHandlingStrategy | null, urlHandlingStrategy?: UrlHandlingStrategy, routeReuseStrategy?: RouteReuseStrategy, defaultTitleStrategy?: DefaultTitleStrategy, titleStrategy?: TitleStrategy): Router;
4141

4242
// (No @packageDocumentation comment for this package)
4343

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

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,6 @@
248248
{
249249
"name": "INJECTOR_SCOPE"
250250
},
251-
{
252-
"name": "Inject"
253-
},
254251
{
255252
"name": "InjectFlags"
256253
},
@@ -407,9 +404,6 @@
407404
{
408405
"name": "NoMatch"
409406
},
410-
{
411-
"name": "NoPreloading"
412-
},
413407
{
414408
"name": "NodeInjector"
415409
},
@@ -485,9 +479,15 @@
485479
{
486480
"name": "ROUTER_INITIALIZER"
487481
},
482+
{
483+
"name": "ROUTER_PRELOADER"
484+
},
488485
{
489486
"name": "ROUTER_PROVIDERS"
490487
},
488+
{
489+
"name": "ROUTER_SCROLLER"
490+
},
491491
{
492492
"name": "ROUTES"
493493
},
@@ -785,9 +785,6 @@
785785
{
786786
"name": "_randomChar"
787787
},
788-
{
789-
"name": "componentDefCount"
790-
},
791788
{
792789
"name": "_stripIndexHtml"
793790
},
@@ -890,6 +887,9 @@
890887
{
891888
"name": "compare"
892889
},
890+
{
891+
"name": "componentDefCount"
892+
},
893893
{
894894
"name": "computeStaticStyling"
895895
},
@@ -971,9 +971,6 @@
971971
{
972972
"name": "createRoot"
973973
},
974-
{
975-
"name": "createRouterScroller"
976-
},
977974
{
978975
"name": "createTView"
979976
},
@@ -1376,6 +1373,9 @@
13761373
{
13771374
"name": "initTNodeFlags"
13781375
},
1376+
{
1377+
"name": "inject"
1378+
},
13791379
{
13801380
"name": "injectArgs"
13811381
},
@@ -1697,9 +1697,6 @@
16971697
{
16981698
"name": "provideForRootGuard"
16991699
},
1700-
{
1701-
"name": "provideLocationStrategy2"
1702-
},
17031700
{
17041701
"name": "provideRoutes"
17051702
},
@@ -2027,4 +2024,4 @@
20272024
{
20282025
"name": "ɵɵtextInterpolate1"
20292026
}
2030-
]
2027+
]

packages/router/src/private_export.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99

1010
export {ɵEmptyOutletComponent} from './components/empty_outlet';
1111
export {RestoredState as ɵRestoredState} from './router';
12-
export {assignExtraOptionsToRouter as ɵassignExtraOptionsToRouter, ROUTER_PROVIDERS as ɵROUTER_PROVIDERS} from './router_module';
12+
export {assignExtraOptionsToRouter as ɵassignExtraOptionsToRouter, providePreloading as ɵprovidePreloading, ROUTER_PROVIDERS as ɵROUTER_PROVIDERS} from './router_module';
1313
export {flatten as ɵflatten} from './utils/collection';

packages/router/src/router_module.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {APP_BASE_HREF, HashLocationStrategy, Location, LOCATION_INITIALIZED, LocationStrategy, PathLocationStrategy, PlatformLocation, ViewportScroller} from '@angular/common';
10-
import {ANALYZE_FOR_ENTRY_COMPONENTS, APP_BOOTSTRAP_LISTENER, APP_INITIALIZER, ApplicationRef, Compiler, ComponentRef, ENVIRONMENT_INITIALIZER, Inject, inject, Injectable, InjectFlags, InjectionToken, Injector, ModuleWithProviders, NgModule, NgProbeToken, OnDestroy, Optional, Provider, SkipSelf} from '@angular/core';
10+
import {ANALYZE_FOR_ENTRY_COMPONENTS, APP_BOOTSTRAP_LISTENER, APP_INITIALIZER, ApplicationRef, Compiler, ComponentRef, ENVIRONMENT_INITIALIZER, Inject, inject, Injectable, InjectFlags, InjectionToken, Injector, ModuleWithProviders, NgModule, NgProbeToken, OnDestroy, Optional, Provider, SkipSelf, Type} from '@angular/core';
1111
import {Title} from '@angular/platform-browser';
1212
import {of, Subject} from 'rxjs';
1313

@@ -22,7 +22,7 @@ import {RouteReuseStrategy} from './route_reuse_strategy';
2222
import {ErrorHandler, Router} from './router';
2323
import {RouterConfigLoader, ROUTES} from './router_config_loader';
2424
import {ChildrenOutletContexts} from './router_outlet_context';
25-
import {NoPreloading, PreloadAllModules, PreloadingStrategy, RouterPreloader} from './router_preloader';
25+
import {PreloadingStrategy, RouterPreloader} from './router_preloader';
2626
import {ROUTER_SCROLLER, RouterScroller} from './router_scroller';
2727
import {ActivatedRoute} from './router_state';
2828
import {UrlHandlingStrategy} from './url_handling_strategy';
@@ -52,6 +52,8 @@ export const ROUTER_CONFIGURATION = new InjectionToken<ExtraOptions>('ROUTER_CON
5252
*/
5353
export const ROUTER_FORROOT_GUARD = new InjectionToken<void>('ROUTER_FORROOT_GUARD');
5454

55+
const ROUTER_PRELOADER = new InjectionToken<RouterPreloader>('');
56+
5557
export const ROUTER_PROVIDERS: Provider[] = [
5658
Location,
5759
{provide: UrlSerializer, useClass: DefaultUrlSerializer},
@@ -66,9 +68,6 @@ export const ROUTER_PROVIDERS: Provider[] = [
6668
},
6769
ChildrenOutletContexts,
6870
{provide: ActivatedRoute, useFactory: rootRoute, deps: [Router]},
69-
RouterPreloader,
70-
NoPreloading,
71-
PreloadAllModules,
7271
RouterConfigLoader,
7372
];
7473

@@ -138,11 +137,7 @@ export class RouterModule {
138137
{provide: ROUTER_CONFIGURATION, useValue: config ? config : {}},
139138
config?.useHash ? provideHashLocationStrategy() : providePathLocationStrategy(),
140139
provideRouterScroller(),
141-
{
142-
provide: PreloadingStrategy,
143-
useExisting: config && config.preloadingStrategy ? config.preloadingStrategy :
144-
NoPreloading
145-
},
140+
config?.preloadingStrategy ? providePreloading(config.preloadingStrategy) : [],
146141
{provide: NgProbeToken, multi: true, useFactory: routerNgProbeToken},
147142
provideRouterInitializer(),
148143
],
@@ -575,7 +570,6 @@ export class RouterInitializer implements OnDestroy {
575570

576571
bootstrapListener(bootstrappedComponentRef: ComponentRef<any>): void {
577572
const opts = this.injector.get(ROUTER_CONFIGURATION);
578-
const preloader = this.injector.get(RouterPreloader);
579573
const routerScroller: RouterScroller|null =
580574
this.injector.get(ROUTER_SCROLLER, null, InjectFlags.Optional);
581575
const router = this.injector.get(Router);
@@ -590,7 +584,7 @@ export class RouterInitializer implements OnDestroy {
590584
router.initialNavigation();
591585
}
592586

593-
preloader.setUpPreloading();
587+
this.injector.get(ROUTER_PRELOADER, null, InjectFlags.Optional)?.setUpPreloading();
594588
routerScroller?.init();
595589
router.resetRootComponentType(ref.componentTypes[0]);
596590
this.resultOfPreactivationDone.next(void 0);
@@ -653,4 +647,12 @@ function provideTracing(): Provider[] {
653647
} else {
654648
return [];
655649
}
656-
}
650+
}
651+
652+
export function providePreloading(preloadingStrategy: Type<PreloadingStrategy>): Provider[] {
653+
return [
654+
RouterPreloader,
655+
{provide: ROUTER_PRELOADER, useExisting: RouterPreloader},
656+
{provide: PreloadingStrategy, useExisting: preloadingStrategy},
657+
];
658+
}

packages/router/src/router_preloader.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
import {Compiler, createEnvironmentInjector, EnvironmentInjector, Injectable, OnDestroy} from '@angular/core';
1010
import {from, Observable, of, Subscription} from 'rxjs';
11-
import {catchError, concatMap, filter, map, mapTo, mergeAll, mergeMap, tap} from 'rxjs/operators';
11+
import {catchError, concatMap, filter, mergeAll, mergeMap} from 'rxjs/operators';
1212

13-
import {Event, NavigationEnd, RouteConfigLoadEnd, RouteConfigLoadStart} from './events';
13+
import {Event, NavigationEnd} from './events';
1414
import {LoadedRouterConfig, Route, Routes} from './models';
1515
import {Router} from './router';
1616
import {RouterConfigLoader} from './router_config_loader';
@@ -38,6 +38,7 @@ export abstract class PreloadingStrategy {
3838
*
3939
* @publicApi
4040
*/
41+
@Injectable({providedIn: 'root'})
4142
export class PreloadAllModules implements PreloadingStrategy {
4243
preload(route: Route, fn: () => Observable<any>): Observable<any> {
4344
return fn().pipe(catchError(() => of(null)));
@@ -53,6 +54,7 @@ export class PreloadAllModules implements PreloadingStrategy {
5354
*
5455
* @publicApi
5556
*/
57+
@Injectable({providedIn: 'root'})
5658
export class NoPreloading implements PreloadingStrategy {
5759
preload(route: Route, fn: () => Observable<any>): Observable<any> {
5860
return of(null);

packages/router/testing/src/router_testing_module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import {Location, LocationStrategy} from '@angular/common';
1010
import {MockLocationStrategy, SpyLocation} from '@angular/common/testing';
1111
import {Compiler, Injector, ModuleWithProviders, NgModule, Optional} from '@angular/core';
12-
import {ChildrenOutletContexts, DefaultTitleStrategy, ExtraOptions, NoPreloading, PreloadingStrategy, provideRoutes, Route, Router, ROUTER_CONFIGURATION, RouteReuseStrategy, RouterModule, ROUTES, Routes, TitleStrategy, UrlHandlingStrategy, UrlSerializer, ɵassignExtraOptionsToRouter as assignExtraOptionsToRouter, ɵflatten as flatten, ɵROUTER_PROVIDERS as ROUTER_PROVIDERS} from '@angular/router';
12+
import {ChildrenOutletContexts, DefaultTitleStrategy, ExtraOptions, NoPreloading, PreloadingStrategy, provideRoutes, Route, Router, ROUTER_CONFIGURATION, RouteReuseStrategy, RouterModule, ROUTES, Routes, TitleStrategy, UrlHandlingStrategy, UrlSerializer, ɵassignExtraOptionsToRouter as assignExtraOptionsToRouter, ɵflatten as flatten, ɵprovidePreloading as providePreloading, ɵROUTER_PROVIDERS as ROUTER_PROVIDERS} from '@angular/router';
1313

1414
import {EXTRA_ROUTER_TESTING_PROVIDERS} from './extra_router_testing_providers';
1515

@@ -119,7 +119,7 @@ export function setupTestingRouter(
119119
[TitleStrategy, new Optional()],
120120
]
121121
},
122-
{provide: PreloadingStrategy, useExisting: NoPreloading},
122+
providePreloading(NoPreloading),
123123
provideRoutes([]),
124124
]
125125
})

0 commit comments

Comments
 (0)