6
6
* found in the LICENSE file at https://angular.dev/license
7
7
*/
8
8
9
- import { EnvironmentProviders , InjectionToken , makeEnvironmentProviders } from '@angular/core' ;
9
+ import {
10
+ EnvironmentProviders ,
11
+ InjectionToken ,
12
+ Provider ,
13
+ Type ,
14
+ makeEnvironmentProviders ,
15
+ } from '@angular/core' ;
16
+ import { type DefaultExport , ROUTES , type Route } from '@angular/router' ;
17
+
18
+ /**
19
+ * The internal path used for the app shell route.
20
+ * @internal
21
+ */
22
+ const APP_SHELL_ROUTE = 'ng-app-shell' ;
23
+
24
+ /**
25
+ * Identifies a particular kind of `ServerRoutesFeatureKind`.
26
+ * @see {@link ServerRoutesFeature }
27
+ * @developerPreview
28
+ */
29
+ enum ServerRoutesFeatureKind {
30
+ AppShell ,
31
+ }
32
+
33
+ /**
34
+ * Helper type to represent a server routes feature.
35
+ * @see {@link ServerRoutesFeatureKind }
36
+ * @developerPreview
37
+ */
38
+ interface ServerRoutesFeature < FeatureKind extends ServerRoutesFeatureKind > {
39
+ ɵkind : FeatureKind ;
40
+ ɵproviders : Provider [ ] ;
41
+ }
10
42
11
43
/**
12
44
* Different rendering modes for server routes.
13
- * @see {@link provideServerRoutesConfig }
45
+ * @see {@link provideServerRouting }
14
46
* @see {@link ServerRoute }
15
47
* @developerPreview
16
48
*/
@@ -148,7 +180,7 @@ export interface ServerRouteServer extends ServerRouteCommon {
148
180
149
181
/**
150
182
* Server route configuration.
151
- * @see {@link provideServerRoutesConfig }
183
+ * @see {@link provideServerRouting }
152
184
* @developerPreview
153
185
*/
154
186
export type ServerRoute =
@@ -163,17 +195,16 @@ export type ServerRoute =
163
195
* This interface defines the optional settings available for configuring server routes
164
196
* in the server-side environment, such as specifying a path to the app shell route.
165
197
*
166
- * @see {@link provideServerRoutesConfig }
167
- * @developerPreview
198
+ *
199
+ * @see {@link provideServerRouting }
200
+ * @deprecated use `provideServerRouting`. This will be removed in version 20.
168
201
*/
169
202
170
203
export interface ServerRoutesConfigOptions {
171
204
/**
172
205
* Defines the route to be used as the app shell, which serves as the main entry
173
206
* point for the application. This route is often used to enable server-side rendering
174
207
* of the application shell for requests that do not match any specific server route.
175
- *
176
- * @see {@link https://angular.dev/ecosystem/service-workers/app-shell | App shell pattern on Angular.dev }
177
208
*/
178
209
appShellRoute ?: string ;
179
210
}
@@ -182,7 +213,13 @@ export interface ServerRoutesConfigOptions {
182
213
* Configuration value for server routes configuration.
183
214
* @internal
184
215
*/
185
- export interface ServerRoutesConfig extends ServerRoutesConfigOptions {
216
+ export interface ServerRoutesConfig {
217
+ /**
218
+ * Defines the route to be used as the app shell.
219
+ */
220
+ appShellRoute ?: string ;
221
+
222
+ /** List of server routes for the application. */
186
223
routes : ServerRoute [ ] ;
187
224
}
188
225
@@ -204,6 +241,8 @@ export const SERVER_ROUTES_CONFIG = new InjectionToken<ServerRoutesConfig>('SERV
204
241
*
205
242
* @see {@link ServerRoute }
206
243
* @see {@link ServerRoutesConfigOptions }
244
+ * @see {@link provideServerRouting }
245
+ * @deprecated use `provideServerRouting`. This will be removed in version 20.
207
246
* @developerPreview
208
247
*/
209
248
export function provideServerRoutesConfig (
@@ -223,3 +262,83 @@ export function provideServerRoutesConfig(
223
262
} ,
224
263
] ) ;
225
264
}
265
+
266
+ /**
267
+ * Sets up the necessary providers for configuring server routes.
268
+ * This function accepts an array of server routes and optional configuration
269
+ * options, returning an `EnvironmentProviders` object that encapsulates
270
+ * the server routes and configuration settings.
271
+ *
272
+ * @param routes - An array of server routes to be provided.
273
+ * @param features - (Optional) server routes features.
274
+ * @returns An `EnvironmentProviders` instance with the server routes configuration.
275
+ *
276
+ * @see {@link ServerRoute }
277
+ * @see {@link withAppShell }
278
+ * @developerPreview
279
+ */
280
+ export function provideServerRouting (
281
+ routes : ServerRoute [ ] ,
282
+ ...features : ServerRoutesFeature < ServerRoutesFeatureKind > [ ]
283
+ ) : EnvironmentProviders {
284
+ const config : ServerRoutesConfig = { routes } ;
285
+ const hasAppShell = features . some ( ( f ) => f . ɵkind === ServerRoutesFeatureKind . AppShell ) ;
286
+ if ( hasAppShell ) {
287
+ config . appShellRoute = APP_SHELL_ROUTE ;
288
+ }
289
+
290
+ const providers : Provider [ ] = [
291
+ {
292
+ provide : SERVER_ROUTES_CONFIG ,
293
+ useValue : config ,
294
+ } ,
295
+ ] ;
296
+
297
+ for ( const feature of features ) {
298
+ providers . push ( ...feature . ɵproviders ) ;
299
+ }
300
+
301
+ return makeEnvironmentProviders ( providers ) ;
302
+ }
303
+
304
+ /**
305
+ * Configures the app shell route with the provided component.
306
+ *
307
+ * The app shell serves as the main entry point for the application and is commonly used
308
+ * to enable server-side rendering (SSR) of the application shell. It handles requests
309
+ * that do not match any specific server route, providing a fallback mechanism and improving
310
+ * perceived performance during navigation.
311
+ *
312
+ * This configuration is particularly useful in applications leveraging Progressive Web App (PWA)
313
+ * patterns, such as service workers, to deliver a seamless user experience.
314
+ *
315
+ * @param component The Angular component to render for the app shell route.
316
+ * @returns A server routes feature configuration for the app shell.
317
+ *
318
+ * @see {@link provideServerRouting }
319
+ * @see {@link https://angular.dev/ecosystem/service-workers/app-shell | App shell pattern on Angular.dev }
320
+ */
321
+ export function withAppShell (
322
+ component : Type < unknown > | ( ( ) => Promise < Type < unknown > | DefaultExport < Type < unknown > > > ) ,
323
+ ) : ServerRoutesFeature < ServerRoutesFeatureKind . AppShell > {
324
+ const routeConfig : Route = {
325
+ path : APP_SHELL_ROUTE ,
326
+ } ;
327
+
328
+ if ( 'ɵcmp' in component ) {
329
+ routeConfig . component = component as Type < unknown > ;
330
+ } else {
331
+ routeConfig . loadComponent = component as ( ) => Promise < Type < unknown > > ;
332
+ }
333
+
334
+ return {
335
+ ɵkind : ServerRoutesFeatureKind . AppShell ,
336
+ ɵproviders : [
337
+ {
338
+ provide : ROUTES ,
339
+ useValue : routeConfig ,
340
+ multi : true ,
341
+ } ,
342
+ ] ,
343
+ } ;
344
+ }
0 commit comments