8000 refactor(router): drop forRoot guard in production (#59458) · angular/angular@c51c66e · GitHub
[go: up one dir, main page]

Skip to content

Commit c51c66e

Browse files
arturovtAndrewKushnir
authored andcommitted
refactor(router): drop forRoot guard in production (#59458)
In this commit, we switch from decorators (which also produce redundant metadata, such as in the `declareFactory` instruction) to the `inject` function to drop the `ROUTER_FORROOT_GUARD` token in production. This token factory function is only in development mode but is still referenced in the constructor due to the `@Inject(ROUTER_FORROOT_GUARD)` decorator. PR Close #59458
1 parent b76be83 commit c51c66e

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -861,11 +861,11 @@ export class RouterLinkActive implements OnChanges, OnDestroy, AfterContentInit
861861

862862
// @public
863863
export class RouterModule {
864-
constructor(guard: any);
864+
constructor();
865865
static forChild(routes: Routes): ModuleWithProviders<RouterModule>;
866866
static forRoot(routes: Routes, config?: ExtraOptions): ModuleWithProviders<RouterModule>;
867867
// (undocumented)
868-
static ɵfac: i0.ɵɵFactoryDeclaration<RouterModule, [{ optional: true; }]>;
868+
static ɵfac: i0.ɵɵFactoryDeclaration<RouterModule, never>;
869869
// (undocumented)
870870
static ɵinj: i0.ɵɵInjectorDeclaration<RouterModule>;
871871
// (undocumented)

packages/router/src/router_module.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ const ROUTER_DIRECTIVES = [RouterOutlet, RouterLink, RouterLinkActive, EmptyOutl
6363
* @docsNotRequired
6464
*/
6565
export const ROUTER_FORROOT_GUARD = new InjectionToken<void>(
66-
typeof ngDevMode === 'undefined' || ngDevMode
67-
? 'router duplicate forRoot guard'
68-
: 'ROUTER_FORROOT_GUARD',
66+
typeof ngDevMode === 'undefined' || ngDevMode ? 'router duplicate forRoot guard' : '',
6967
);
7068

7169
// TODO(atscott): All of these except `ActivatedRoute` are `providedIn: 'root'`. They are only kept
@@ -112,7 +110,11 @@ export const ROUTER_PROVIDERS: Provider[] = [
112110
exports: ROUTER_DIRECTIVES,
113111
})
114112
export class RouterModule {
115-
constructor(@Optional() @Inject(ROUTER_FORROOT_GUARD) guard: any) {}
113+
constructor() {
114+
if (typeof ngDevMode === 'undefined' || ngDevMode) {
115+
inject(ROUTER_FORROOT_GUARD, {optional: true});
116+
}
117+
}
116118

117119
/**
118120
* Creates and configures a module with all the router providers and directives.
@@ -143,11 +145,13 @@ export class RouterModule {
143145
: []
144146
: [],
145147
{provide: ROUTES, multi: true, useValue: routes},
146-
{
147-
provide: ROUTER_FORROOT_GUARD,
148-
useFactory: provideForRootGuard,
149-
deps: [[Router, new Optional(), new SkipSelf()]],
150-
},
148+
typeof ngDevMode === 'undefined' || ngDevMode
149+
? {
150+
provide: ROUTER_FORROOT_GUARD,
151+
useFactory: provideForRootGuard,
152+
deps: [[Router, new Optional(), new SkipSelf()]],
153+
}
154+
: [],
151155
config?.errorHandler
152156
? {
153157
provide: NAVIGATION_ERROR_HANDLER,
@@ -224,7 +228,7 @@ function providePathLocationStrategy(): Provider {
224228
}
225229

226230
export function provideForRootGuard(router: Router): any {
227-
if ((typeof ngDevMode === 'undefined' || ngDevMode) && router) {
231+
if (router) {
228232
throw new RuntimeError(
229233
RuntimeErrorCode.FOR_ROOT_CALLED_TWICE,
230234
`The Router was provided more than once. This can happen if 'forRoot' is used outside of the root injector.` +

0 commit comments

Comments
 (0)
0