Skip to content

Commit d009ec9

Browse files
committed
fixup! perf(@angular/ssr): pre-compile critical CSS plans at build time
1 parent 9b3a98b commit d009ec9

3 files changed

Lines changed: 23 additions & 24 deletions

File tree

packages/angular/build/src/utils/index-file/nonce.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export async function addNonce(html: string): Promise<string> {
4242
}
4343

4444
/** Finds the Angular nonce in an HTML string. */
45-
async function findNonce(html: string): Promise<string | null> {
45+
export async function findNonce(html: string): Promise<string | null> {
4646
// Inexpensive check to avoid parsing the HTML when we're sure there's no nonce.
4747
if (!NONCE_ATTR_PATTERN.test(html)) {
4848
return null;

packages/angular/build/src/utils/server-rendering/manifest.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
BuildOutputFileType,
1919
createOutputFile,
2020
} from '../../tools/esbuild/bundler-files';
21+
import { findNonce } from '../index-file/nonce';
2122
import { joinUrlParts } from '../url';
2223

2324
export const SERVER_APP_MANIFEST_FILENAME = 'angular-app-manifest.mjs';
@@ -201,7 +202,7 @@ export async function generateAngularServerAppManifest(
201202

202203
const indexHtml = additionalHtmlOutputFiles.get(INDEX_HTML_SERVER)?.text;
203204
if (indexHtml) {
204-
nonce = findNonce(indexHtml);
205+
nonce = (await findNonce(indexHtml)) ?? undefined;
205206
}
206207

207208
// When routes have been extracted, mappings are no longer needed, as preloads will be included in the metadata.
@@ -263,12 +264,3 @@ function generateLazyLoadedFilesMappings(
263264

264265
return entryPointToBundles;
265266
}
266-
267-
/**
268-
* Finds the Angular nonce attribute value in an HTML string.
269-
*/
270-
function findNonce(html: string): string | undefined {
271-
const match = /<[a-zA-Z0-9-]+[^>]*?\sngcspnonce=(?:"([^"]*)"|'([^']*)'|(\S+))/i.exec(html);
272-
273-
return match ? (match[1] ?? match[2] ?? match[3]) : undefined;
274-
}

packages/angular/ssr/src/app.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -369,20 +369,27 @@ export class AngularServerApp {
369369
return html;
370370
}
371371

372-
this.inlineCriticalCssProcessor ??= createProcessor([...criticalCssPlans], {
373-
preload: 'media-script',
374-
nonce,
375-
preloadFonts: true,
376-
inlineFonts: true,
377-
noscriptFallback: true,
378-
cache: true,
379-
logger: {
380-
// eslint-disable-next-line no-console
381-
warn: console.warn,
382-
},
383-
}).process;
372+
try {
373+
this.inlineCriticalCssProcessor ??= createProcessor([...criticalCssPlans], {
374+
preload: 'media-script',
375+
nonce,
376+
preloadFonts: true,
377+
inlineFonts: true,
378+
noscriptFallback: true,
379+
cache: true,
380+
logger: {
381+
// eslint-disable-next-line no-console
382+
warn: console.warn,
383+
},
384+
}).process;
384385

385-
return this.inlineCriticalCssProcessor(html);
386+
return this.inlineCriticalCssProcessor(html);
387+
} catch (error) {
388+
// eslint-disable-next-line no-console
389+
console.error('An error occurred while inlining critical CSS.', error);
390+
391+
return html;
392+
}
386393
}
387394

388395
/**

0 commit comments

Comments
 (0)