liveBook · Manning

Lazy initialization

As explained in Vanilla Web

A technique that defers loading or initialization until content or functionality is needed.

Lazy initialization delays loading or setup until a feature is likely to be needed. It moves noncritical work off the initial render and interaction path; it does not mean delaying everything. section 14.6

Choose what to defer by user-visible priority:

  • Load the main document, critical CSS, and core navigation early; defer below-the-fold media and optional features. section 14.6.2
  • Use native media attributes or content-visibility for off-screen content, IntersectionObserver for custom viewport logic, and dynamic imports at route, dialog, or widget boundaries. section 14.6 section 14.6.3
  • Keep loading, failure, and retry states visible; set image dimensions to prevent layout shifts, and preload only assets users are likely to need soon. section 14.6.2
async function openReportDialog() {
 reportButton.disabled = true;

  try {
    const { showReportDialog } = await import("./report-dialog.js");
    await showReportDialog();
  } catch (error) {
    showToast("The report tools could not be loaded.");
    console.error(error);
  } finally {
    reportButton.disabled = false;
  }
}

A report dialog loads when requested; the button is disabled during loading, errors are surfaced, and the button is restored afterward. section 14.6.2

How Vanilla Web covers it

How deep it goes
Explains native loading options, priority choices, boundaries, and failure handling, while deferring dynamic-import syntax.
Examples
Uses a dimensioned lazy image and a dynamically imported report dialog with visible loading and error handling.
What you can do after
Apply native loading attributes and dynamic imports at user-visible boundaries while handling failures.
How it connects
Connects lazy loading with dynamic import, IntersectionObserver, content-visibility, and modulepreload.

Also explained in

Compare how 12 books explain lazy initialization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Vanilla Web. The section links point to the parts of the book it draws on.