From 558d76ec5d975483e0561a1147a31d9d4a0304dd Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Wed, 2 Apr 2025 21:35:44 -0700 Subject: [PATCH] refactor(docs-infra): Move router-specific app configuration to separate file (#60722) This moves the router configuration for the app to a separate file since there is a lot of it. PR Close #60722 --- adev/src/app/app.config.ts | 44 ++------------------------ adev/src/app/router_providers.ts | 54 ++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 42 deletions(-) create mode 100644 adev/src/app/router_providers.ts diff --git a/adev/src/app/app.config.ts b/adev/src/app/app.config.ts index 868eed181de..649dd120242 100644 --- a/adev/src/app/app.config.ts +++ b/adev/src/app/app.config.ts @@ -26,56 +26,21 @@ import { windowProvider, } from '@angular/docs'; import {provideClientHydration} from '@angular/platform-browser'; -import { - RouteReuseStrategy, - Router, - TitleStrategy, - createUrlTreeFromSnapshot, - provideRouter, - withComponentInputBinding, - withInMemoryScrolling, - withViewTransitions, -} from '@angular/router'; import environment from './environment'; import {PREVIEWS_COMPONENTS_MAP} from './../assets/previews/previews'; -import {ADevTitleStrategy} from './core/services/a-dev-title-strategy'; import {AnalyticsService} from './core/services/analytics/analytics.service'; import {ContentLoader} from './core/services/content-loader.service'; import {CustomErrorHandler} from './core/services/errors-handling/error-handler'; import {ExampleContentLoader} from './core/services/example-content-loader.service'; -import {ReuseTutorialsRouteStrategy} from './features/tutorial/tutorials-route-reuse-strategy'; -import {routes} from './routes'; import {CURRENT_MAJOR_VERSION} from './core/providers/current-version'; -import {AppScroller} from './app-scroller'; +import {routerProviders} from './router_providers'; export const appConfig: ApplicationConfig = { providers: [ - provideRouter( - routes, - withInMemoryScrolling(), - withViewTransitions({ - onViewTransitionCreated: ({transition, to}) => { - const router = inject(Router); - const toTree = createUrlTreeFromSnapshot(to, []); - // Skip the transition if the only thing changing is the fragment and queryParams - if ( - router.isActive(toTree, { - paths: 'exact', - matrixParams: 'exact', - fragment: 'ignored', - queryParams: 'ignored', - }) - ) { - transition.skipTransition(); - } - }, - }), - withComponentInputBinding(), - ), + routerProviders, provideExperimentalZonelessChangeDetection(), provideClientHydration(), provideHttpClient(withFetch()), - provideEnvironmentInitializer(() => inject(AppScroller)), provideEnvironmentInitializer(() => inject(AnalyticsService)), provideAlgoliaSearchClient(environment), { @@ -87,15 +52,10 @@ export const appConfig: ApplicationConfig = { {provide: PREVIEWS_COMPONENTS, useValue: PREVIEWS_COMPONENTS_MAP}, {provide: DOCS_CONTENT_LOADER, useClass: ContentLoader}, {provide: EXAMPLE_VIEWER_CONTENT_LOADER, useClass: ExampleContentLoader}, - { - provide: RouteReuseStrategy, - useClass: ReuseTutorialsRouteStrategy, - }, { provide: WINDOW, useFactory: (document: Document) => windowProvider(document), deps: [DOCUMENT], }, - {provide: TitleStrategy, useClass: ADevTitleStrategy}, ], }; diff --git a/adev/src/app/router_providers.ts b/adev/src/app/router_providers.ts new file mode 100644 index 00000000000..fb748becfab --- /dev/null +++ b/adev/src/app/router_providers.ts @@ -0,0 +1,54 @@ +/*! + * @license + * Copyright Google LLC All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.dev/license + */ + +import {inject, provideEnvironmentInitializer} from '@angular/core'; +import { + provideRouter, + withInMemoryScrolling, + Router, + withViewTransitions, + createUrlTreeFromSnapshot, + withComponentInputBinding, + RouteReuseStrategy, + TitleStrategy, +} from '@angular/router'; +import {routes} from './routes'; +import {ADevTitleStrategy} from './core/services/a-dev-title-strategy'; +import {ReuseTutorialsRouteStrategy} from './features/tutorial/tutorials-route-reuse-strategy'; +import {AppScroller} from './app-scroller'; + +export const routerProviders = [ + provideRouter( + routes, + withInMemoryScrolling(), + withViewTransitions({ + onViewTransitionCreated: ({transition, to}) => { + const router = inject(Router); + const toTree = createUrlTreeFromSnapshot(to, []); + // Skip the transition if the only thing changing is the fragment and queryParams + if ( + router.isActive(toTree, { + paths: 'exact', + matrixParams: 'exact', + fragment: 'ignored', + queryParams: 'ignored', + }) + ) { + transition.skipTransition(); + } + }, + }), + withComponentInputBinding(), + ), + { + provide: RouteReuseStrategy, + useClass: ReuseTutorialsRouteStrategy, + }, + {provide: TitleStrategy, useClass: ADevTitleStrategy}, + provideEnvironmentInitializer(() => inject(AppScroller)), +];