typescript 48 lines · 8 steps

Deferred loading with @defer in Angular

A standalone Angular component lazy-loads its recommendations panel on viewport with placeholder, loading, and error states.

Explained by highlit
1import { Component, signal } from '@angular/core';
2import { CommonModule } from '@angular/common';
3import { ProductRecommendationsComponent } from './product-recommendations.component';
4 
5@Component({
6 selector: 'app-product-detail',
7 standalone: true,
8 imports: [CommonModule, ProductRecommendationsComponent],
9 template: `
10 <section class="product-detail">
11 <h1>{{ product().name }}</h1>
12 <p class="price">{{ product().price | currency }}</p>
13 
14 @defer (on viewport; prefetch on idle) {
15 <app-product-recommendations [productId]="product().id" />
16 } @placeholder (minimum 500ms) {
17 <div class="recommendations-skeleton" aria-hidden="true">
18 @for (row of skeletonRows; track $index) {
19 <div class="skeleton-card"></div>
20 }
21 </div>
22 } @loading (after 100ms; minimum 300ms) {
23 <div class="recommendations-loading">
24 <span class="spinner"></span>
25 <span>Finding products you'll love…</span>
26 </div>
27 } @error {
28 <div class="recommendations-error" role="alert">
29 <p>We couldn't load recommendations right now.</p>
30 <button type="button" (click)="reload()">Try again</button>
31 </div>
32 }
33 </section>
34 `,
35})
36export class ProductDetailComponent {
37 protected readonly skeletonRows = Array.from({ length: 4 });
38 
39 protected readonly product = signal({
40 id: 'sku-4820',
41 name: 'Merino Wool Runner',
42 price: 118,
43 });
44 
45 protected reload(): void {
46 window.location.reload();
47 }
48}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1@defer with an `on viewport` trigger delays loading a component's code until it's actually needed on screen.
  2. 2The @placeholder, @loading, and @error blocks let you script every phase of a deferred view's lifecycle.
  3. 3Timing modifiers like `minimum` and `after` prevent flicker by controlling how long each state stays visible.

Related explainers

typescript
import { registerLocaleData } from '@angular/common';
import localeFr from '@angular/common/locales/fr';
import localeFrExtra from '@angular/common/locales/extra/fr';
import localeDe from '@angular/common/locales/de';

Locale-aware bootstrapping in Angular

i18n localization dependency-injection
Intermediate 8 steps
typescript
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import * as Joi from 'joi';
 

Validating env config at boot in NestJS

configuration schema-validation environment-variables
Intermediate 8 steps
typescript
import { Inject, Injectable, Logger } from '@nestjs/common';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Cache } from 'cache-manager';
import { InjectRepository } from '@nestjs/typeorm';

A cache-aside country lookup in NestJS

cache-aside dependency-injection batch-lookup
Intermediate 8 steps
typescript
import { Injectable, effect, signal, computed } from '@angular/core';
 
interface Preferences {
  theme: 'light' | 'dark';

A signal-based preferences store in Angular

signals state-management persistence
Intermediate 7 steps
typescript
import { useEffect, useState } from "react";
 
interface Section {
  id: string;

Building a scroll-spy hook in React

custom-hooks intersectionobserver dom-observation
Intermediate 8 steps
typescript
import { Injectable, Scope, Inject, NotFoundException } from '@nestjs/common';
import { REQUEST } from '@nestjs/core';
import { Request } from 'express';
import { DataSource } from 'typeorm';

Per-tenant database connections in NestJS

multi-tenancy connection-pooling dependency-injection
Advanced 8 steps

Share this explainer

Here's the card — post it anywhere.

Deferred loading with @defer in Angular — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code