@@ -332,45 +332,70 @@ export class AngularServerApp {
332332 return createRedirectResponse ( result . redirectTo , status ) ;
333333 }
334334
335- const { inlineCriticalCssProcessor, criticalCssLRUCache, textDecoder } = this ;
335+ if ( renderMode === RenderMode . Prerender ) {
336+ const renderedHtml = await result . content ( ) ;
337+ const finalHtml = await this . inlineCriticalCss ( renderedHtml , url ) ;
338+
339+ return new Response ( finalHtml , responseInit ) ;
340+ }
336341
337342 // Use a stream to send the response before finishing rendering and inling critical CSS, improving performance via header flushing.
343+ const parentThis = this ;
338344 const stream = new ReadableStream ( {
339345 async start ( controller ) {
340346 const renderedHtml = await result . content ( ) ;
341-
342- if ( ! inlineCriticalCssProcessor ) {
343- controller . enqueue ( textDecoder . encode ( renderedHtml ) ) ;
344- controller . close ( ) ;
345-
346- return ;
347- }
348-
349- let htmlWithCriticalCss ;
350- try {
351- if ( renderMode === RenderMode . Server ) {
352- const cacheKey = await sha256 ( renderedHtml ) ;
353- htmlWithCriticalCss = criticalCssLRUCache . get ( cacheKey ) ;
354- if ( ! htmlWithCriticalCss ) {
355- htmlWithCriticalCss = await inlineCriticalCssProcessor . process ( renderedHtml ) ;
356- criticalCssLRUCache . put ( cacheKey , htmlWithCriticalCss ) ;
357- }
358- } else {
359- htmlWithCriticalCss = await inlineCriticalCssProcessor . process ( renderedHtml ) ;
360- }
361- } catch ( error ) {
362- // eslint-disable-next-line no-console
363- console . error ( `An error occurred while inlining critical CSS for: ${ url } .` , error ) ;
364- }
365-
366- controller . enqueue ( textDecoder . encode ( htmlWithCriticalCss ?? renderedHtml ) ) ;
347+ const finalHtml = await parentThis . inlineCriticalCss . call (
348+ parentThis ,
349+ renderedHtml ,
350+ url ,
351+ true ,
352+ ) ;
353+ controller . enqueue ( parentThis . textDecoder . encode ( finalHtml ) ) ;
367354 controller . close ( ) ;
368355 } ,
369356 } ) ;
370357
371358 return new Response ( stream , responseInit ) ;
372359 }
373360
361+ /**
362+ * Inlines critical CSS into the given HTML content.
363+ *
364+ * @param html - The HTML content to process.
365+ * @param url - The URL associated with the request, for logging purposes.
366+ * @param cache - A flag to indicate if the result should be cached.
367+ * @returns A promise that resolves to the HTML with inlined critical CSS.
368+ */
369+ private async inlineCriticalCss ( html : string , url : URL , cache = false ) : Promise < string > {
370+ const { inlineCriticalCssProcessor, criticalCssLRUCache } = this ;
371+
372+ if ( ! inlineCriticalCssProcessor ) {
373+ return html ;
374+ }
375+
376+ try {
377+ if ( ! cache ) {
378+ return await inlineCriticalCssProcessor . process ( html ) ;
379+ }
380+
381+ const cacheKey = await sha256 ( html ) ;
382+ const cachedHtml = criticalCssLRUCache . get ( cacheKey ) ;
383+ if ( cachedHtml ) {
384+ return cachedHtml ;
385+ }
386+
387+ const processedHtml = await inlineCriticalCssProcessor . process ( html ) ;
388+ criticalCssLRUCache . put ( cacheKey , processedHtml ) ;
389+
390+ return processedHtml ;
391+ } catch ( error ) {
392+ // eslint-disable-next-line no-console
393+ console . error ( `An error occurred while inlining critical CSS for: ${ url } .` , error ) ;
394+
395+ return html ;
396+ }
397+ }
398+
374399 /**
375400 * Constructs the asset path on the server based on the provided HTTP request.
376401 *
0 commit comments