Skip to content

Commit 30c25bf

Browse files
committed
feat(@angular/ssr): export AngularAppEngine as public API
Added `AngularAppEngine` to the public API of `@angular/ssr`, allowing users to access it directly for enhanced server-side rendering functionality.
1 parent a539da3 commit 30c25bf

File tree

4 files changed

+77
-4
lines changed

4 files changed

+77
-4
lines changed

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

+11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44
55
```ts
66

7+
// @public
8+
export interface AngularServerAppManager {
9+
render(request: Request, requestContext?: unknown): Promise<Response | null>;
10+
}
11+
12+
// @public
13+
export function destroyAngularAppEngine(): void;
14+
15+
// @public
16+
export function getOrCreateAngularAppEngine(): AngularServerAppManager;
17+
718
// (No @packageDocumentation comment for this package)
819

920
```

packages/angular/ssr/private_export.ts

+2
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ export {
1717
setAngularAppEngineManifest as ɵsetAngularAppEngineManifest,
1818
} from './src/manifest';
1919

20+
export { AngularAppEngine as ɵAngularAppEngine } from './src/app-engine';
21+
2022
export { InlineCriticalCssProcessor as ɵInlineCriticalCssProcessor } from './src/utils/inline-critical-css';

packages/angular/ssr/public_api.ts

+6
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@
77
*/
88

99
export * from './private_export';
10+
11+
export {
12+
type AngularServerAppManager,
13+
getOrCreateAngularAppEngine,
14+
destroyAngularAppEngine,
15+
} from './src/app-engine';

packages/angular/ssr/src/app-engine.ts

+58-4
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,45 @@ import { Hooks } from './hooks';
1010
import { getPotentialLocaleIdFromUrl } from './i18n';
1111
import { EntryPointExports, getAngularAppEngineManifest } from './manifest';
1212

13+
/**
14+
* Angular server application engine.
15+
* Manages Angular server applications (including localized ones) and handles rendering requests.
16+
17+
* @developerPreview
18+
*/
19+
export interface AngularServerAppManager {
20+
/**
21+
* Renders a response for the given HTTP request using the server application.
22+
*
23+
* This method processes the request, determines the appropriate route and rendering context,
24+
* and returns an HTTP response.
25+
*
26+
* If the request URL appears to be for a file (excluding `/index.html`), the method returns `null`.
27+
* A request to `https://www.example.com/page/index.html` will render the Angular route
28+
* corresponding to `https://www.example.com/page`.
29+
*
30+
* @param request - The incoming HTTP request object to be rendered.
31+
* @param requestContext - Optional additional context for the request, such as metadata.
32+
* @returns A promise that resolves to a Response object, or `null` if the request URL represents a file (e.g., `./logo.png`)
33+
* rather than an application route.
34+
*/
35+
render(request: Request, requestContext?: unknown): Promise<Response | null>;
36+
}
37+
1338
/**
1439
* Angular server application engine.
1540
* Manages Angular server applications (including localized ones), handles rendering requests,
1641
* and optionally transforms index HTML before rendering.
1742
*/
18-
export class AngularAppEngine {
43+
export class AngularAppEngine implements AngularServerAppManager {
1944
/**
2045
* Hooks for extending or modifying the behavior of the server application.
2146
* These hooks are used by the Angular CLI when running the development server and
2247
* provide extensibility points for the application lifecycle.
2348
*
24-
* @internal
49+
* @private
2550
*/
26-
static hooks = new Hooks();
51+
static ɵhooks = new Hooks();
2752

2853
/**
2954
* Provides access to the hooks for extending or modifying the server application's behavior.
@@ -32,7 +57,7 @@ export class AngularAppEngine {
3257
* @internal
3358
*/
3459
get hooks(): Hooks {
35-
return AngularAppEngine.hooks;
60+
return AngularAppEngine.ɵhooks;
3661
}
3762

3863
/**
@@ -92,3 +117,32 @@ export class AngularAppEngine {
92117
return entryPoints.get(potentialLocale);
93118
}
94119
}
120+
121+
let angularAppEngine: AngularAppEngine | undefined;
122+
123+
/**
124+
* Retrieves an existing `AngularAppEngine` instance or creates a new one if none exists.
125+
*
126+
* This method ensures that only a single instance of `AngularAppEngine` is created and reused across
127+
* the application lifecycle, providing efficient resource management. If the instance does not exist,
128+
* it will be instantiated upon the first call.
129+
*
130+
* @developerPreview
131+
* @returns The existing or newly created instance of `AngularAppEngine`.
132+
*/
133+
export function getOrCreateAngularAppEngine(): AngularServerAppManager {
134+
return (angularAppEngine ??= new AngularAppEngine());
135+
}
136+
137+
/**
138+
* Destroys the current `AngularAppEngine` instance, releasing any associated resources.
139+
*
140+
* This method resets the reference to the `AngularAppEngine` instance to `undefined`, allowing
141+
* a new instance to be created on the next call to `getOrCreateAngularAppEngine()`. It is typically
142+
* used when reinitializing the server environment or refreshing the application state is necessary.
143+
*
144+
* @developerPreview
145+
*/
146+
export function destroyAngularAppEngine(): void {
147+
angularAppEngine = undefined;
148+
}

0 commit comments

Comments
 (0)