Skip to content

Commit 7d9ce24

Browse files
committed
refactor(@angular/ssr): remove singleton helpers
This commit removes the singleton helpers and instead exposes the classes directly.
1 parent 41fb2ed commit 7d9ce24

File tree

7 files changed

+23
-141
lines changed

7 files changed

+23
-141
lines changed

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,12 @@
55
```ts
66

77
// @public
8-
export interface AngularServerAppManager {
8+
export class AngularAppEngine {
99
getHeaders(request: Request): Readonly<Map<string, string>>;
1010
render(request: Request, requestContext?: unknown): Promise<Response | null>;
11+
static ɵhooks: Hooks;
1112
}
1213

13-
// @public
14-
export function destroyAngularAppEngine(): void;
15-
16-
// @public
17-
export function getOrCreateAngularAppEngine(): AngularServerAppManager;
18-
1914
// (No @packageDocumentation comment for this package)
2015

2116
```

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { StaticProvider } from '@angular/core';
1111
import { Type } from '@angular/core';
1212

1313
// @public
14-
export interface AngularNodeServerAppManager {
14+
export class AngularNodeAppEngine {
1515
getHeaders(request: IncomingMessage): Readonly<Map<string, string>>;
1616
render(request: IncomingMessage, requestContext?: unknown): Promise<Response | null>;
1717
}
@@ -46,12 +46,6 @@ export interface CommonEngineRenderOptions {
4646
// @public
4747
export function createWebRequestFromNodeRequest(nodeRequest: IncomingMessage): Request;
4848

49-
// @public
50-
export function destroyAngularNodeAppEngine(): void;
51-
52-
// @public
53-
export function getOrCreateAngularNodeAppEngine(): AngularNodeServerAppManager;
54-
5549
// @public
5650
export function writeResponseToNodeResponse(source: Response, destination: ServerResponse): Promise<void>;
5751

packages/angular/ssr/node/public_api.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ export {
1212
type CommonEngineOptions,
1313
} from './src/common-engine/common-engine';
1414

15-
export {
16-
type AngularNodeServerAppManager,
17-
destroyAngularNodeAppEngine,
18-
getOrCreateAngularNodeAppEngine,
19-
} from './src/app-engine';
15+
export { AngularNodeAppEngine } from './src/app-engine';
2016

2117
export { writeResponseToNodeResponse } from './src/response';
2218
export { createWebRequestFromNodeRequest } from './src/request';

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

Lines changed: 14 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,23 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import { destroyAngularAppEngine, getOrCreateAngularAppEngine } from '@angular/ssr';
9+
import { AngularAppEngine } from '@angular/ssr';
1010
import type { IncomingMessage } from 'node:http';
1111
import { createWebRequestFromNodeRequest } from './request';
1212

1313
/**
1414
* Angular server application engine.
15-
* Manages Angular server applications (including localized ones) and handles rendering requests.
16-
15+
* Manages Angular server applications (including localized ones), handles rendering requests,
16+
* and optionally transforms index HTML before rendering.
17+
*
18+
* @note This class should be instantiated once and used as a singleton across the server-side
19+
* application to ensure consistent handling of rendering requests and resource management.
20+
*
1721
* @developerPreview
1822
*/
19-
export interface AngularNodeServerAppManager {
23+
export class AngularNodeAppEngine {
24+
private readonly angularAppEngine = new AngularAppEngine();
25+
2026
/**
2127
* Renders an HTTP response based on the incoming request using the Angular server application.
2228
*
@@ -32,7 +38,9 @@ export interface AngularNodeServerAppManager {
3238
* @returns A promise that resolves to a `Response` object, or `null` if the request URL is for a static file
3339
* (e.g., `./logo.png`) rather than an application route.
3440
*/
35-
render(request: IncomingMessage, requestContext?: unknown): Promise<Response | null>;
41+
render(request: IncomingMessage, requestContext?: unknown): Promise<Response | null> {
42+
return this.angularAppEngine.render(createWebRequestFromNodeRequest(request), requestContext);
43+
}
3644

3745
/**
3846
* Retrieves HTTP headers for a request associated with statically generated (SSG) pages,
@@ -43,7 +51,7 @@ export interface AngularNodeServerAppManager {
4351
* @note This function should be used exclusively for retrieving headers of SSG pages.
4452
* @example
4553
* ```typescript
46-
* const angularAppEngine = getOrCreateAngularNodeAppEngine();
54+
* const angularAppEngine = new AngularNodeAppEngine();
4755
*
4856
* app.use(express.static('dist/browser', {
4957
* setHeaders: (res, path) => {
@@ -58,51 +66,7 @@ export interface AngularNodeServerAppManager {
5866
}));
5967
* ```
6068
*/
61-
getHeaders(request: IncomingMessage): Readonly<Map<string, string>>;
62-
}
63-
64-
/**
65-
* Angular server application engine.
66-
* Manages Angular server applications (including localized ones), handles rendering requests,
67-
* and optionally transforms index HTML before rendering.
68-
*/
69-
class AngularNodeAppEngine implements AngularNodeServerAppManager {
70-
private readonly angularAppEngine = getOrCreateAngularAppEngine();
71-
72-
render(request: IncomingMessage, requestContext?: unknown): Promise<Response | null> {
73-
return this.angularAppEngine.render(createWebRequestFromNodeRequest(request), requestContext);
74-
}
75-
7669
getHeaders(request: IncomingMessage): Readonly<Map<string, string>> {
7770
return this.angularAppEngine.getHeaders(createWebRequestFromNodeRequest(request));
7871
}
7972
}
80-
81-
let angularNodeAppEngine: AngularNodeAppEngine | undefined;
82-
83-
/**
84-
* Retrieves the existing `AngularNodeAppEngine` instance or creates a new one if it doesn't exist.
85-
*
86-
* This method ensures that a single instance of `AngularNodeAppEngine` is used throughout the application's lifecycle,
87-
* promoting efficient resource management. If an instance does not exist, it will be instantiated upon the first call.
88-
*
89-
* @developerPreview
90-
* @returns The existing or newly created instance of `AngularNodeAppEngine`.
91-
*/
92-
export function getOrCreateAngularNodeAppEngine(): AngularNodeServerAppManager {
93-
return (angularNodeAppEngine ??= new AngularNodeAppEngine());
94-
}
95-
96-
/**
97-
* Destroys the current `AngularNodeAppEngine` instance and releases any associated resources.
98-
*
99-
* This method resets the reference to the `AngularNodeAppEngine` instance to `undefined`, allowing for the creation
100-
* of a new instance on subsequent calls to `getOrCreateAngularNodeAppEngine()`. This is typically used when
101-
* reinitializing the server environment or refreshing the application state.
102-
*
103-
* @developerPreview
104-
*/
105-
export function destroyAngularNodeAppEngine(): void {
106-
angularNodeAppEngine = undefined;
107-
destroyAngularAppEngine();
108-
}

packages/angular/ssr/private_export.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,4 @@ export {
2121
setAngularAppEngineManifest as ɵsetAngularAppEngineManifest,
2222
} from './src/manifest';
2323

24-
export { AngularAppEngine as ɵAngularAppEngine } from './src/app-engine';
25-
2624
export { InlineCriticalCssProcessor as ɵInlineCriticalCssProcessor } from './src/utils/inline-critical-css';

packages/angular/ssr/public_api.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,4 @@
88

99
export * from './private_export';
1010

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

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

Lines changed: 4 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,17 @@ import { getPotentialLocaleIdFromUrl } from './i18n';
1212
import { EntryPointExports, getAngularAppEngineManifest } from './manifest';
1313
import { stripIndexHtmlFromURL } from './utils/url';
1414

15-
/**
16-
* Angular server application engine.
17-
* Manages Angular server applications (including localized ones) and handles rendering requests.
18-
19-
* @developerPreview
20-
*/
21-
export interface AngularServerAppManager {
22-
/**
23-
* Renders a response for the given HTTP request using the server application.
24-
*
25-
* This method processes the request, determines the appropriate route and rendering context,
26-
* and returns an HTTP response.
27-
*
28-
* If the request URL appears to be for a file (excluding `/index.html`), the method returns `null`.
29-
* A request to `https://www.example.com/page/index.html` will render the Angular route
30-
* corresponding to `https://www.example.com/page`.
31-
*
32-
* @param request - The incoming HTTP request object to be rendered.
33-
* @param requestContext - Optional additional context for the request, such as metadata.
34-
* @returns A promise that resolves to a Response object, or `null` if the request URL represents a file (e.g., `./logo.png`)
35-
* rather than an application route.
36-
*/
37-
render(request: Request, requestContext?: unknown): Promise<Response | null>;
38-
39-
/**
40-
* Retrieves HTTP headers for a request associated with statically generated (SSG) pages,
41-
* based on the URL pathname.
42-
*
43-
* @param request - The incoming request object.
44-
* @returns A `Map` containing the HTTP headers as key-value pairs.
45-
* @note This function should be used exclusively for retrieving headers of SSG pages.
46-
*/
47-
getHeaders(request: Request): Readonly<Map<string, string>>;
48-
}
49-
5015
/**
5116
* Angular server application engine.
5217
* Manages Angular server applications (including localized ones), handles rendering requests,
5318
* and optionally transforms index HTML before rendering.
5419
*
20+
* @note This class should be instantiated once and used as a singleton across the server-side
21+
* application to ensure consistent handling of rendering requests and resource management.
22+
*
5523
* @developerPreview
5624
*/
57-
export class AngularAppEngine implements AngularServerAppManager {
25+
export class AngularAppEngine {
5826
/**
5927
* Hooks for extending or modifying the behavior of the server application.
6028
* These hooks are used by the Angular CLI when running the development server and
@@ -153,32 +121,3 @@ export class AngularAppEngine implements AngularServerAppManager {
153121
return new Map(headers);
154122
}
155123
}
156-
157-
let angularAppEngine: AngularAppEngine | undefined;
158-
159-
/**
160-
* Retrieves an existing `AngularAppEngine` instance or creates a new one if none exists.
161-
*
162-
* This method ensures that only a single instance of `AngularAppEngine` is created and reused across
163-
* the application lifecycle, providing efficient resource management. If the instance does not exist,
164-
* it will be instantiated upon the first call.
165-
*
166-
* @developerPreview
167-
* @returns The existing or newly created instance of `AngularAppEngine`.
168-
*/
169-
export function getOrCreateAngularAppEngine(): AngularServerAppManager {
170-
return (angularAppEngine ??= new AngularAppEngine());
171-
}
172-
173-
/**
174-
* Destroys the current `AngularAppEngine` instance, releasing any associated resources.
175-
*
176-
* This method resets the reference to the `AngularAppEngine` instance to `undefined`, allowing
177-
* a new instance to be created on the next call to `getOrCreateAngularAppEngine()`. It is typically
178-
* used when reinitializing the server environment or refreshing the application state is necessary.
179-
*
180-
* @developerPreview
181-
*/
182-
export function destroyAngularAppEngine(): void {
183-
angularAppEngine = undefined;
184-
}

0 commit comments

Comments
 (0)