6
6
* found in the LICENSE file at https://angular.dev/license
7
7
*/
8
8
9
- import { destroyAngularAppEngine , getOrCreateAngularAppEngine } from '@angular/ssr' ;
9
+ import { AngularAppEngine } from '@angular/ssr' ;
10
10
import type { IncomingMessage } from 'node:http' ;
11
11
import { createWebRequestFromNodeRequest } from './request' ;
12
12
13
13
/**
14
14
* 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
+ *
17
21
* @developerPreview
18
22
*/
19
- export interface AngularNodeServerAppManager {
23
+ export class AngularNodeAppEngine {
24
+ private readonly angularAppEngine = new AngularAppEngine ( ) ;
25
+
20
26
/**
21
27
* Renders an HTTP response based on the incoming request using the Angular server application.
22
28
*
@@ -32,7 +38,9 @@ export interface AngularNodeServerAppManager {
32
38
* @returns A promise that resolves to a `Response` object, or `null` if the request URL is for a static file
33
39
* (e.g., `./logo.png`) rather than an application route.
34
40
*/
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
+ }
36
44
37
45
/**
38
46
* Retrieves HTTP headers for a request associated with statically generated (SSG) pages,
@@ -43,7 +51,7 @@ export interface AngularNodeServerAppManager {
43
51
* @note This function should be used exclusively for retrieving headers of SSG pages.
44
52
* @example
45
53
* ```typescript
46
- * const angularAppEngine = getOrCreateAngularNodeAppEngine ();
54
+ * const angularAppEngine = new AngularNodeAppEngine ();
47
55
*
48
56
* app.use(express.static('dist/browser', {
49
57
* setHeaders: (res, path) => {
@@ -58,51 +66,7 @@ export interface AngularNodeServerAppManager {
58
66
}));
59
67
* ```
60
68
*/
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
-
76
69
getHeaders ( request : IncomingMessage ) : Readonly < Map < string , string > > {
77
70
return this . angularAppEngine . getHeaders ( createWebRequestFromNodeRequest ( request ) ) ;
78
71
}
79
72
}
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
- }
0 commit comments