T15: Dynamic Site - Polish

Performance is a feature. Users abandon sites that load slowly. Polishing your site means optimizing what loads, when it loads, and how it loads. Like a restaurant that prepares popular dishes in advance and only cooks rare orders on demand.

Lazy Loading

Load images and content only when they enter the viewport. The loading="lazy" attribute handles images natively.

<img src="photo.jpg" loading="lazy" alt="Lazy loaded">

// For custom lazy loading with Intersection Observer
const observer = new IntersectionObserver((entries) => {
    entries.forEach(entry => {
        if (entry.isIntersecting) {
            const img = entry.target;
            img.src = img.dataset.src;
            observer.unobserve(img);
        }
    });
});

Performance Techniques

Minimize render-blocking resources. Defer non-critical JavaScript. Use efficient selectors and reduce DOM size.

<!-- Defer non-critical JS -->
<script src="app.js" defer></script>

<!-- Preload critical resources -->
<link rel="preload" href="font.woff2" as="font" crossorigin>

Code Splitting

Load only the JavaScript needed for the current view. Import additional modules when the user navigates to them.

async function loadModule(name) {
    const module = await import(`./modules/${name}.js`);
    module.init();
}
sequenceDiagram participant B as Browser participant S as Server B->>S: Initial HTML + critical CSS S-->>B: Minimal payload B->>B: Render above-the-fold B->>S: Lazy load images (on scroll) B->>S: Deferred JS modules S-->>B: Additional resources B->>B: Progressive enhancement

Key Takeaways

  • Lazy loading defers non-visible content until it enters the viewport
  • Use defer attribute on script tags to avoid render-blocking
  • Preload critical resources like fonts to speed up first paint
  • Code splitting loads only the JavaScript needed for the current view