@@ -68,6 +68,60 @@ export class AngularAppEngine {
68
68
* rather than an application route.
69
69
*/
70
70
async render ( request : Request , requestContext ?: unknown ) : Promise < Response | null > {
71
+ const serverApp = await this . getAngularServerAppForRequest ( request ) ;
72
+
73
+ return serverApp ? serverApp . render ( request , requestContext ) : null ;
74
+ }
75
+
76
+ /**
77
+ * Serves a prerendered page for a given request.
78
+ *
79
+ * This method examines the incoming request to determine if it corresponds to a known prerendered page.
80
+ * If a matching page is found, it returns a `Response` object containing the page content; otherwise, it returns `null`.
81
+ *
82
+ * @param request - The incoming HTTP request object to be rendered.
83
+ * @returns A promise that resolves to a `Response` object containing the prerendered page content, or `null`
84
+ * if no matching page is found.
85
+ **/
86
+ async serve ( request : Request ) : Promise < Response | null > {
87
+ const serverApp = await this . getAngularServerAppForRequest ( request ) ;
88
+
89
+ return serverApp ? serverApp . serve ( request ) : null ;
90
+ }
91
+
92
+ /**
93
+ * Handles incoming HTTP requests by serving prerendered content or rendering the page.
94
+ *
95
+ * This method first attempts to serve a prerendered page. If the prerendered page is not available,
96
+ * it falls back to rendering the requested page using server-side rendering. The function returns
97
+ * a promise that resolves to the appropriate HTTP response.
98
+ *
99
+ * @param request - The incoming HTTP request to be processed.
100
+ * @param requestContext - Optional additional context for rendering, such as request metadata.
101
+ * @returns A promise that resolves to the HTTP response object resulting from the request handling,
102
+ * or null if no matching content is found.
103
+ */
104
+ async process ( request : Request , requestContext ?: unknown ) : Promise < Response | null > {
105
+ const serverApp = await this . getAngularServerAppForRequest ( request ) ;
106
+ if ( ! serverApp ) {
107
+ return null ;
108
+ }
109
+
110
+ return ( await serverApp . serve ( request ) ) ?? ( await serverApp . render ( request , requestContext ) ) ;
111
+ }
112
+
113
+ /**
114
+ * Retrieves the Angular server application instance for a given request.
115
+ *
116
+ * This method checks if the request URL corresponds to an Angular application entry point.
117
+ * If so, it initializes or retrieves an instance of the Angular server application for that entry point.
118
+ * Requests that resemble file requests (except for `/index.html`) are skipped.
119
+ *
120
+ * @param request - The incoming HTTP request object.
121
+ * @returns A promise that resolves to an `AngularServerApp` instance if a valid entry point is found,
122
+ * or `null` if no entry point matches the request URL.
123
+ */
124
+ private async getAngularServerAppForRequest ( request : Request ) : Promise < AngularServerApp | null > {
71
125
// Skip if the request looks like a file but not `/index.html`.
72
126
const url = new URL ( request . url ) ;
73
127
const entryPoint = await this . getEntryPointExportsForUrl ( url ) ;
@@ -82,26 +136,7 @@ export class AngularAppEngine {
82
136
const serverApp = getOrCreateAngularServerApp ( ) as AngularServerApp ;
83
137
serverApp . hooks = this . hooks ;
84
138
85
- return serverApp . render ( request , requestContext ) ;
86
- }
87
-
88
- /**
89
- * Retrieves HTTP headers for a request associated with statically generated (SSG) pages,
90
- * based on the URL pathname.
91
- *
92
- * @param request - The incoming request object.
93
- * @returns A `Map` containing the HTTP headers as key-value pairs.
94
- * @note This function should be used exclusively for retrieving headers of SSG pages.
95
- */
96
- getPrerenderHeaders ( request : Request ) : ReadonlyMap < string , string > {
97
- if ( this . manifest . staticPathsHeaders . size === 0 ) {
98
- return new Map ( ) ;
99
- }
100
-
101
- const { pathname } = stripIndexHtmlFromURL ( new URL ( request . url ) ) ;
102
- const headers = this . manifest . staticPathsHeaders . get ( stripTrailingSlash ( pathname ) ) ;
103
-
104
- return new Map ( headers ) ;
139
+ return serverApp ;
105
140
}
106
141
107
142
/**
0 commit comments