Skip to content

Commit 19c8f5e

Browse files
committed
fix(@angular/ssr): rename provideServerRoutesConfig to provideServerRouting
This commit renames `provideServerRoutesConfig` to `provideServerRouting` and updates the second parameter to use the `ServerRoutes` features. This change improves alignment with the framework's API conventions and the way features are integrated. ### Example Usage: Before: ```typescript provideServerRoutesConfig(serverRoutes, { appShellRoute: 'shell' }) ``` After: ```typescript provideServerRouting(serverRoutes, withAppShell(AppShellComponent)) ```
1 parent 4df97d1 commit 19c8f5e

File tree

8 files changed

+198
-196
lines changed

8 files changed

+198
-196
lines changed

goldens/public-api/angular/ssr/index.api.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
```ts
66

77
import { EnvironmentProviders } from '@angular/core';
8+
import { Provider } from '@angular/core';
9+
import { Type } from '@angular/core';
810

911
// @public
1012
export class AngularAppEngine {
@@ -23,9 +25,12 @@ export enum PrerenderFallback {
2325
Server = 0
2426
}
2527

26-
// @public
28+
// @public @deprecated
2729
export function provideServerRoutesConfig(routes: ServerRoute[], options?: ServerRoutesConfigOptions): EnvironmentProviders;
2830

31+
// @public
32+
export function provideServerRouting(routes: ServerRoute[], ...features: ServerRoutesFeature<ServerRoutesFeatureKind>[]): EnvironmentProviders;
33+
2934
// @public
3035
export enum RenderMode {
3136
Client = 1,
@@ -63,7 +68,7 @@ export interface ServerRoutePrerenderWithParams extends Omit<ServerRoutePrerende
6368
getPrerenderParams: () => Promise<Record<string, string>[]>;
6469
}
6570

66-
// @public
71+
// @public @deprecated
6772
export interface ServerRoutesConfigOptions {
6873
appShellRoute?: string;
6974
}
@@ -73,6 +78,9 @@ export interface ServerRouteServer extends ServerRouteCommon {
7378
renderMode: RenderMode.Server;
7479
}
7580

81+
// @public
82+
export function withAppShell(component: Type<unknown>): ServerRoutesFeature<ServerRoutesFeatureKind.AppShell>;
83+
7684
// (No @packageDocumentation comment for this package)
7785

7886
```

packages/angular/ssr/public_api.ts

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export {
1616
type ServerRoute,
1717
type ServerRoutesConfigOptions,
1818
provideServerRoutesConfig,
19+
provideServerRouting,
20+
withAppShell,
1921
RenderMode,
2022
type ServerRouteClient,
2123
type ServerRoutePrerender,

packages/angular/ssr/src/routes/route-config.ts

+122-8
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,43 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

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 { ROUTES } 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+
}
1042

1143
/**
1244
* Different rendering modes for server routes.
13-
* @see {@link provideServerRoutesConfig}
45+
* @see {@link provideServerRouting}
1446
* @see {@link ServerRoute}
1547
* @developerPreview
1648
*/
@@ -148,7 +180,7 @@ export interface ServerRouteServer extends ServerRouteCommon {
148180

149181
/**
150182
* Server route configuration.
151-
* @see {@link provideServerRoutesConfig}
183+
* @see {@link provideServerRouting}
152184
* @developerPreview
153185
*/
154186
export type ServerRoute =
@@ -163,17 +195,16 @@ export type ServerRoute =
163195
* This interface defines the optional settings available for configuring server routes
164196
* in the server-side environment, such as specifying a path to the app shell route.
165197
*
166-
* @see {@link provideServerRoutesConfig}
167-
* @developerPreview
198+
*
199+
* @see {@link provideServerRouting}
200+
* @deprecated use `provideServerRouting`. This will be removed in version 20.
168201
*/
169202

170203
export interface ServerRoutesConfigOptions {
171204
/**
172205
* Defines the route to be used as the app shell, which serves as the main entry
173206
* point for the application. This route is often used to enable server-side rendering
174207
* 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}
177208
*/
178209
appShellRoute?: string;
179210
}
@@ -182,7 +213,13 @@ export interface ServerRoutesConfigOptions {
182213
* Configuration value for server routes configuration.
183214
* @internal
184215
*/
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. */
186223
routes: ServerRoute[];
187224
}
188225

@@ -204,6 +241,8 @@ export const SERVER_ROUTES_CONFIG = new InjectionToken<ServerRoutesConfig>('SERV
204241
*
205242
* @see {@link ServerRoute}
206243
* @see {@link ServerRoutesConfigOptions}
244+
* @see {@link provideServerRouting}
245+
* @deprecated use `provideServerRouting`. This will be removed in version 20.
207246
* @developerPreview
208247
*/
209248
export function provideServerRoutesConfig(
@@ -223,3 +262,78 @@ export function provideServerRoutesConfig(
223262
},
224263
]);
225264
}
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>,
323+
): ServerRoutesFeature<ServerRoutesFeatureKind.AppShell> {
324+
return {
325+
ɵkind: ServerRoutesFeatureKind.AppShell,
326+
ɵproviders: [
327+
{
328+
provide: ROUTES,
329+
useValue: [
330+
{
331+
path: APP_SHELL_ROUTE,
332+
component,
333+
},
334+
],
335+
multi: true,
336+
},
337+
],
338+
};
339+
}

packages/angular/ssr/test/testing-utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { provideServerRendering } from '@angular/platform-server';
1818
import { RouterOutlet, Routes, provideRouter } from '@angular/router';
1919
import { destroyAngularServerApp } from '../src/app';
2020
import { ServerAsset, setAngularAppManifest } from '../src/manifest';
21-
import { ServerRoute, provideServerRoutesConfig } from '../src/routes/route-config';
21+
import { ServerRoute, provideServerRouting } from '../src/routes/route-config';
2222

2323
@Component({
2424
standalone: true,
@@ -97,7 +97,7 @@ export function setAngularAppTestingManifest(
9797
provideServerRendering(),
9898
provideExperimentalZonelessChangeDetection(),
9999
provideRouter(routes),
100-
provideServerRoutesConfig(serverRoutes),
100+
provideServerRouting(serverRoutes),
101101
...extraProviders,
102102
],
103103
});

0 commit comments

Comments
 (0)