Web performance optimization

From kumaran-is/claude-code-onboarding · View interactive version

Web performance optimization is an AI agent skill from the GitHub repository kumaran-is/claude-code-onboarding. Optimize Angular 21.x web application performance including Core Web Vitals, bundle size, lazy loading with @defer, NgOptimizedImage, OnPush+Signals, SSR TransferState, and Angular CLI build optimization. Use when diagnosing slow Angular apps, preparing for Lighthouse audits, or reducing bundle size

How to use this skill

Skill file

Iron Law

NO PERFORMANCE OPTIMIZATION WITHOUT MEASURING FIRST — run Lighthouse and ng build --stats-json before writing a single optimization

Profile before optimizing. Every change must have a before/after metric. Do not optimize based on guesswork.

Web Performance Optimization — Angular 21.x

Overview

Help developers optimize Angular 21.x SPA performance to improve user experience, SEO rankings, and Core Web Vitals scores. This skill provides systematic approaches to measure, analyze, and improve loading speed, runtime performance, and bundle size — all grounded in Angular-native patterns (signals, @defer, NgOptimizedImage, lazy routes, SSR TransferState).

When to Use This Skill

How It Works

Step 1: Measure Current Performance

Establish baseline metrics before touching any code:

Step 2: Identify Issues

Angular-specific performance bottlenecks to look for:

Step 3: Prioritize Optimizations

Focus on highest-impact Angular improvements first:

  1. Critical rendering path — NgOptimizedImage with priority on LCP image, <link rel="preload"> for fonts
  2. Bundle size — lazy routes (loadComponent/loadChildren), @defer for below-fold components
  3. Runtime rendering — OnPush + signals on all components, eliminate Default change detection
  4. Image pipeline — convert to AVIF/WebP, serve responsive srcsets via NgOptimizedImage
  5. SSR double-fetch — TransferState to hydrate state from server render without re-fetching

Step 4: Implement Optimizations

Apply improvements in priority order — measure after each step.

Step 5: Verify Improvements


Examples

See references/optimization-checklists.md for full worked examples with before/after code and measured results.


Best Practices

Do This

Do Not Do This


Common Pitfalls

Problem: Good Desktop Score, Poor Mobile Score

Symptoms: Lighthouse passes on desktop, fails on mobile throttling

Solution:

lighthouse https://yoursite.com --throttling.cpuSlowdownMultiplier=4 --preset=desktop

Problem: Bundle Size Exceeds angular.json Budget

Symptoms: ng build exits with "ERROR: bundle initial exceeded maximum budget"

Solution:

ng build --configuration=production --stats-json
npx webpack-bundle-analyzer dist/browser/stats.json

Problem: Images Cause Layout Shifts (High CLS)

Symptoms: CLS > 0.1 in Lighthouse, content jumps on load

Solution:

/* Prevent CLS on any image not using NgOptimizedImage */
img {
  aspect-ratio: attr(width) / attr(height);
  width: 100%;
  height: auto;
}

Problem: Slow TTFB with Angular SSR

Symptoms: Time to First Byte > 600ms, server HTML arrives late

Solution:

// app.config.ts
import { provideClientHydration, withIncrementalHydration } from '@angular/platform-browser';

export const appConfig: ApplicationConfig = {
  providers: [
    provideClientHydration(withIncrementalHydration())
  ]
};

Problem: SSR Double HTTP Fetch

Symptoms: Network tab shows the same API call twice (once server, once client)

Solution: Use TransferState — serialize data on the server, read it on the client without re-fetching

// product.service.ts
import { Injectable, inject, PLATFORM_ID } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { TransferState, makeStateKey } from '@angular/core';
import { isPlatformServer } from '@angular/common';
import { Observable, of } from 'rxjs';
import { tap } from 'rxjs/operators';

const PRODUCTS_KEY = makeStateKey<Product[]>('products');

@Injectable({ providedIn: 'root' })
export class ProductService {
  private http = inject(HttpClient);
  private transferState = inject(TransferState);
  private platformId = inject(PLATFORM_ID);

  getProducts(): Observable<Product[]> {
    if (this.transferState.hasKey(PRODUCTS_KEY)) {
      const products = this.transferState.get(PRODUCTS_KEY, []);
      this.transferState.remove(PRODUCTS_KEY);
      return of(products);
    }

    return this.http.get<Product[]>('/api/products').pipe(
      tap(products => {
        if (isPlatformServer(this.platformId)) {
          this.transferState.set(PRODUCTS_KEY, products);
        }
      })
    );
  }
}

Performance Checklist

See references/optimization-checklists.md for the full checklist (Angular Architecture, Bundle Size, Images, CSS, Core Web Vitals targets, SSR).

Critical items:


Performance Tools

See references/optimization-checklists.md for full tool list (Measurement, Bundle Analysis, Image Optimization, Monitoring).


Related Skills


Additional Resources


Key principle: Measure before optimizing. ng build --stats-json + webpack-bundle-analyzer takes 2 minutes and reveals exactly where your bundle weight is — every other optimization is guesswork without it.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.