perf(@angular/ssr): pre-compile critical CSS plans at build time - #33992
Conversation
31f8133 to
4eb1d31
Compare
5c6aa18 to
bf1c206
Compare
Previously, critical CSS inlining in `@angular/ssr` was executed dynamically at runtime by parsing HTML and stylesheets on each request (or caching processed HTML in an in-memory LRU cache keyed by SHA-256 content hashes). This incurred significant CPU and latency overhead during request processing and precluded efficient stream processing. This commit updates the critical CSS workflow to pre-compile stylesheet plans during the build step via Beasties' compiler (`compileSheet` and `encodePlan`). These compact plans are stored in the server application manifest (`criticalCssPlans`) along with any CSP nonce. At runtime, `AngularServerApp` initializes Beasties' runtime processor with the pre-compiled plans, allowing fast single-pass string and stream processing without re-parsing stylesheets, calculating content hashes, or maintaining a runtime LRU cache. Consequently, `InlineCriticalCssProcessor`, `LRUCache`, and `crypto.ts` utility classes have been removed from `@angular/ssr`.
bf1c206 to
9b3a98b
Compare
There was a problem hiding this comment.
Code Review
This pull request replaces the internal bundled version of the beasties library with the official npm package dependency, removing custom implementations of the critical CSS processor and LRU cache in favor of beasties/runtime. It also introduces build-time pre-compilation of critical CSS plans and nonce extraction into the server application manifest. The review feedback highlights a high-severity issue where an error during CSS inlining could abort streaming responses mid-flight, a bug in the regex used for extracting unquoted nonces, and an opportunity to simplify a dynamic import now that the module resolution is set to bundler.
|
This PR was merged into the repository. The changes were merged into the following branches:
|
Pre-compiles stylesheet critical CSS plans during build time and embeds them into the server application manifest instead of performing full CSS and HTML parsing dynamically at runtime on every SSR request.
Changes
@angular/build, uses Beasties compiler (compileSheetandencodePlan) to pre-compile stylesheet plans and embedscriticalCssPlansand any templatenonceinto the server application manifest.@angular/ssr, initializes Beasties runtime processor withcriticalCssPlans, enabling fast, single-pass critical CSS inlining for response handling and streaming without runtime LRU cache and SHA-256 hashing.@angular/ssrinternalInlineCriticalCssProcessor,LRUCache, andcrypto.tsutilities, as well as the bundled third-party Beasties distribution in favor of the upstream package.setAngularAppTestingManifestto accept stylesheet CSS records and compile plans internally.Depends on #34014.