Mandela Winnie Product Associate , AHA
- Debugging the myth around application perfomance in Angular . Covering from SSR , Partial Hydration to Web Core Vitals
-
Time to render the largest visible content element.
-
Examples of LCP Elements:
-
<img>
elements -
<img>
elements inside an<svg>
element -
<video>
elements -
Elements with a
url()
loaded background image -
Block-level elements with text nodes
-
-
**What affects this : ** Image handling, placeholder content for deferrable views, hydration configuration.
-
Quantifies unexpected changes to the page layout.
-
Calculation:
Impact Fraction
(how much of the viewport) *Distance Fraction
(how far from the start position). -
Mitigation Strategies (Preview): Image handling, placeholder content for deferrable views, hydration configuration.
-
Measures the latency of all interactions from the user.
-
Examples :
- Mouse clicks
- key presses
- touchscreen taps.
-
What affects them : Change detection cycles, code efficiency, bundle size.
-
Removing "dead code"
-
Doesn't handle private class properties, methods, dependencies, styles
-
Goal: Only ship the code your users need, including general code cleanup.
-
Breaking down the large bundle into smaller, on-demand chunks.
-
Angular Builder & ESBuild: How Angular handles code splitting (especially when ESBuild becomes the default build system).
-
When to AVOID Code Splitting:
- Large Enums,
- Catch-all classes,
- Large barrel files,
- Angular Modules (can prevent effective splitting).
-
Key Technique: Lazy Loading (e.g., routes, defer blocks).
-
Making the HTML dynamic by adding JavaScript to portions of static HTML.
-
Introducing
@defer
(Angular v17+ Preview, v18 Stable):-
Tells ESBuild to split code inside the block into a lazy-loaded bundle.
-
Before v17: Lazy loading was done mainly in the Router Module (misuse of auxiliary routes).
-
-
@defer
@defer { }
-
@placeholder
(Optional, but Recommended):Code snippet
@defer { /* ... */ } @placeholder (minimum 500ms) { }
- Default Trigger:
on idle
(wrapsrequestIdleCallbackAPI
to queue requests; no guarantee to run in the next cycle).
- Default Trigger:
-
@loading
(Optional):Code snippet
@defer { /* ... */ } @loading { }
- Can use
after
andminimum
options.
- Can use
-
@error
(Optional):Code snippet
@defer { /* ... */ } @error { }
- (Not sure if there's a retry feature.)
-
on
Options (Specify a trigger, can be combined as OR conditions):-
on viewport
(element): Wraps theIntersectionObserver API
to watch the trigger element. Watches the placeholder by default, or can accept a template reference variable. -
on interaction
(element): Triggered by click or keydown event (same as above). -
on hover
(element): Triggered on mouse hover. -
on immediate
: Triggered when rendering is finished. -
on timer(duration)
: Loads after a specified duration. -
on idle
: Default, when the browser is idle. -
when
(truthy expression): Useful for feature flags.
-
-
prefetch
Option:Code snippet
@defer (on interaction; prefetch on idle) { /* ... */ }
- Accepts fine-tuning by preloading chunks. Fetches and stores the code until the trigger is met.
-
Limitations:
-
Content projections don't work as expected.
-
Loading/injection of environment variables within
defer
blocks can be challenging.
-
-
Requirements:
-
The deferred component must be Standalone.
-
The parent component must also be Standalone.
-
The deferred component must not require an import in the parent's
imports
array.
-
-
Client-Side Rendering (CSR) means an empty HTML skeleton initially, leading to slow LCP.
-
Send an HTML skeleton with content on the first load, reducing the amount of time for the user to see content (improving LCP).
-
Angular Universal (Angular 4, 2017): Introduced to handle performance and SEO.
-
Destructive Hydration (Angular v16): Initially, Angular destroyed the server-rendered content to show a new one.
-
Non-Destructive Hydration (Angular v17, 2023): Welcome! Reuses server-rendered DOM instead of killing it.
-
Enhanced SSR (Angular v18, 2024): Further improvements and debugging tools.
-
SSR Application Builds: Requires two builds – one for the server (e.g., Node.js Express) and one for the browser.
-
Introduced in v16, refined in v17: Reuses server-rendered DOM, significantly improving Web Vitals and SEO.
-
How it Works: Transfers data fetched on the server and persists the application state by attaching client-side Angular to the existing DOM.
-
Addresses the time gap between when the view is usable versus when the view is seen.
-
Uses the event dispatch primitive to record user interactions during this gap and replays them when the client-side application is fully connected.
-
Can be added using the Angular CLI (
ng add @angular/ssr
) to new or existing projects. -
Manual configuration involves
provideClientHydration()
. -
DOM Manipulation Concerns: Direct DOM manipulation (accessing the document, querying specific DOM elements,
appendChild
,innerHTML
/outerHTML
, detaching/moving DOM nodes) can conflict with hydration.
-
Browser-Only Hooks:
-
afterNextRender()
: Runs one time, useful for initializing third-party APIs or browser-specific functionalities. -
afterRender()
: Runs after every change detection cycle.
-
-
ngSkipHydration
: A directive to opt out components that cannot be refactored to be hydration-compatible.
-
Angular Prerendering: Prerenders HTML files at build time, which can then be cached and served.
-
Benefits: Lowers Time to First Byte (TTFB Web Vital). Good for static views.
-
Limitations: Not good for highly dynamic views.
-
Hybrid SSG: You can configure a hybrid approach, combining SSG for static parts and SSR/CSR for dynamic parts.
-
server.ts
: Node.js Express server to redirect static assets and API routes. -
main.server.ts
: The main application bootstrap file for the server. -
provideServerRendering()
: For server-side rendering setup. -
provideClientHydration()
: For client-side hydration setup.
-
1. Server-Side Rendering (SSR): Renders the application for each request made (client receives HTML).
-
2. Client-Side Rendering (CSR): The default Angular behavior (client receives an HTML skeleton, then JavaScript renders content).
-
3. Prerender (SSG): Pre-renders the HTML at build time for static views.
-
Why Track? What gets measured, gets improved!
-
Key Metrics to Monitor: LCP, CLS, INP, Bundle Size, TTFB.
-
Tools & Techniques:
-
Lighthouse audits.
-
Web Vitals Chrome extension.
-
Google Search Console (for real-user data).
-
Real User Monitoring (RUM) solutions.
-
Browser DevTools Performance tab.
-
Custom application-specific metrics.
-
Questions?
Thank You!
This Markdown output provides the full content of your presentation, organized with headings and bullet points, ready to be used as a script or pasted into a document. Let me know if you'd like any specific sections rephrased or expanded!