home / skills / develite98 / angular-best-practices / angular-best-practices-legacy

angular-best-practices-legacy skill

/skills/angular-best-practices-legacy

This skill helps you optimize legacy Angular 12-16 apps by applying NgModule, RxJS, and template best practices for performance.

npx playbooks add skill develite98/angular-best-practices --skill angular-best-practices-legacy

Review the files below or copy the command above to add this skill to your agents.

Files (21)
SKILL.md
4.5 KB
---
name: angular-best-practices-legacy
description: Angular 12-16 performance optimization guidelines with NgModules, RxJS patterns, and classic template syntax (*ngIf, *ngFor). Use when maintaining or working with pre-v17 Angular codebases. Triggers on tasks involving Angular components, services, modules, or performance improvements in legacy projects.
license: MIT
metadata:
  author: community
  version: "1.0.0"
---

# Angular Best Practices (Legacy: v12-16)

Comprehensive performance optimization guide for Angular 12-16 applications using NgModule-based architecture and RxJS-centric patterns. Contains 30+ rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.

## When to Apply

Reference these guidelines when:
- Maintaining Angular 12-16 codebases
- Using NgModule-based architecture
- Working with *ngIf, *ngFor directives
- Using RxJS for state management
- Using class-based HTTP interceptors
- Reviewing code for performance issues

## Key Patterns (v12-16)

- **NgModules** - Feature modules for organization
- **BehaviorSubject** - Reactive state management
- **Subject + takeUntil** - Subscription cleanup pattern
- **Class-based interceptors** - HTTP handling with implements HttpInterceptor
- ***ngFor / *ngIf** - Structural directives with trackBy
- **loadChildren** - Module-based lazy loading

## Rule Categories by Priority

| Priority | Category | Impact | Prefix |
|----------|----------|--------|--------|
| 1 | Change Detection | CRITICAL | `change-` |
| 2 | Bundle & Lazy Loading | CRITICAL | `bundle-` |
| 3 | RxJS Optimization | HIGH | `rxjs-` |
| 4 | Template Performance | HIGH | `template-` |
| 5 | Dependency Injection | MEDIUM-HIGH | `di-` |
| 6 | HTTP & Caching | MEDIUM | `http-` |
| 7 | Forms Optimization | MEDIUM | `forms-` |
| 8 | General Performance | LOW-MEDIUM | `perf-` |

## Quick Reference

### 1. Change Detection (CRITICAL)

- `change-rxjs-state` - Use BehaviorSubject with OnPush for reactive state
- `change-onpush` - Use OnPush change detection strategy
- `change-detach-reattach` - Detach change detector for heavy operations
- `change-run-outside-zone` - Run non-UI code outside NgZone

### 2. Bundle & Lazy Loading (CRITICAL)

- `bundle-ngmodule` - Organize code into feature NgModules
- `bundle-scam` - Use Single Component Angular Modules pattern
- `bundle-lazy-routes` - Lazy load routes with loadChildren
- `bundle-preload` - Preload routes for perceived speed
- `bundle-no-barrel-imports` - Avoid barrel files, use direct imports

### 3. RxJS Optimization (HIGH)

- `rxjs-async-pipe` - Use async pipe instead of manual subscriptions
- `rxjs-takeuntil` - Use takeUntil with destroy$ Subject for cleanup
- `rxjs-share-replay` - Share observables to avoid duplicate requests
- `rxjs-operators` - Use efficient RxJS operators
- `rxjs-mapping-operators` - Use correct mapping operators (switchMap vs exhaustMap)
- `rxjs-no-nested-subscribe` - Avoid nested subscriptions

### 4. Template Performance (HIGH)

- `template-trackby` - Use trackBy function with *ngFor
- `template-pure-pipes` - Use pure pipes for expensive transformations
- `template-ng-optimized-image` - Use NgOptimizedImage for image optimization
- `template-no-function-calls` - Avoid function calls in templates
- `template-virtual-scroll` - Use virtual scrolling for large lists

### 5. Dependency Injection (MEDIUM-HIGH)

- `di-provided-in-root` - Use providedIn: 'root' for singleton services
- `di-injection-token` - Use InjectionToken for non-class dependencies
- `di-factory-providers` - Use factory providers for complex initialization

### 6. HTTP & Caching (MEDIUM)

- `http-interceptors` - Use class-based interceptors for cross-cutting concerns
- `http-transfer-state` - Use TransferState for SSR hydration

### 7. Forms Optimization (MEDIUM)

- `forms-reactive` - Use reactive forms instead of template-driven
- `forms-typed` - Use typed FormGroup for type safety

### 8. General Performance (LOW-MEDIUM)

- `perf-memory-leaks` - Prevent memory leaks (timers, listeners, subscriptions)
- `perf-web-workers` - Offload heavy computation to Web Workers
- `arch-smart-dumb-components` - Use Smart/Dumb component pattern

## How to Use

Read individual rule files for detailed explanations and code examples:

```
rules/change-rxjs-state.md
rules/bundle-ngmodule.md
rules/rxjs-takeuntil.md
```

Each rule file contains:
- Brief explanation of why it matters
- Incorrect code example with explanation
- Correct code example with explanation
- Additional context and references

## Full Compiled Document

For the complete guide with all rules expanded: `AGENTS.md`

Overview

This skill provides targeted performance optimization guidelines for Angular 12–16 applications that use NgModule-based architecture, classic template syntax, and RxJS patterns. It focuses on high-impact changes—change detection, lazy loading, RxJS best practices, and template optimizations—useful when maintaining or modernizing legacy Angular codebases. The guidance is practical, prioritized, and ready to guide automated refactoring or manual reviews.

How this skill works

The skill inspects component, service, module, and template code to identify legacy anti-patterns and performance hotspots. It recommends concrete fixes such as switching to OnPush, replacing manual subscriptions with async pipe or takeUntil, applying trackBy to *ngFor, and converting heavy work to run outside NgZone. Priority tags indicate impact to help you triage changes for automated fixes or developer action.

When to use it

  • Maintaining or refactoring Angular 12–16 codebases
  • Adding or reviewing performance fixes in components, services, or modules
  • Auditing templates that use *ngIf and *ngFor for render hotspots
  • Improving RxJS usage and subscription cleanup in legacy projects
  • Planning bundle size reductions and lazy loading strategies

Best practices

  • Adopt OnPush change detection and use BehaviorSubject for reactive local state
  • Prefer async pipe and shared observables (shareReplay) over manual subscriptions
  • Apply trackBy for all *ngFor lists and avoid function calls inside templates
  • Organize features into NgModules and lazy-load with loadChildren where appropriate
  • Use takeUntil(destroy$) for subscription cleanup or use takeUntil-equivalents and avoid nested subscriptions
  • Run CPU-bound or non-UI code outside NgZone and detach change detector for heavy updates

Example use cases

  • Convert component subscriptions to async pipe and BehaviorSubject to reduce change detection churn
  • Refactor a monolithic module into feature NgModules and add lazy-loaded routes to shrink initial bundle
  • Add trackBy to large list renders and introduce virtual scrolling for long collections
  • Replace repeated HTTP calls with shared observables using shareReplay and add class-based interceptors for caching headers
  • Use takeUntil in services/components to prevent memory leaks during navigation

FAQ

Will these guidelines break existing behavior?

Most recommendations are non-breaking if applied carefully; OnPush and reactive state changes may require adjusting how inputs and state are updated. Test behavior after each change.

Which changes give the biggest wins first?

Start with change detection (OnPush, async pipe), lazy loading (split bundles), and eliminating duplicate HTTP requests (shareReplay). These typically yield the largest perf improvements.