Web Fundamentals

Import on Interaction: Load When User Interacts

Web Fundamentals Map

Rendering & Browser Architecture

Critical Rendering PathScript Loading Patterns (async/defer)Event Loop Deep DiveJavaScript Module Systems (CJS, ESM, UMD)Dynamic Module Loading (import())Import on InteractionImport on VisibilityBrowser Rendering Pipeline & Layout ThrashingRendering Strategies (CSR, SSR, SSG, ISR)Streaming SSR & Progressive HTMLIslands ArchitectureReact Server ComponentsFramework Reactivity (React, Vue, Svelte, Solid)HTTP/1.1 vs HTTP/2 vs HTTP/3 (QUIC)DNS Resolution & TTL Caching

Performance

Core Web Vitals: LCP, INP, CLSPerformance Optimization Trade-offsCritical Resource PrioritizationCode Splitting & Dynamic ImportsTree Shaking & Dead Code EliminationLazy LoadingResource Hints: Preload, Prefetch & PreconnectText Compression: Gzip & BrotliImage & Video OptimizationAdaptive LoadingList VirtualizationWeb Workers vs Main ThreadMemory Leaks: Detection & PreventionManaging Third-Party ScriptsHow CDNs WorkHTTP Caching Deep DiveService Workers & Offline StrategyPWA Fundamentals

Security

Cross-Site Scripting (XSS)Cross-Site Request Forgery (CSRF)CORS ExplainedCORS Preflight, Credentials & MisconfigurationsContent Security Policy (CSP)Why is HTTPS Secure? (TLS/SSL)Authorization Best PracticesCookie Security & Session Hardening

State & Data Architecture

State Management Guide (Context vs Zustand vs Redux)React Query & Server State CachingData Fetching PatternsCaching StrategiesPagination: Offset vs Cursor-BasedReal-time Communication (WebSocket, SSE, Polling)
mediumWeb Fundamentals

Import on Interaction: Load When User Interacts

Learn the interview-ready mental model, practical trade-offs, and production patterns for this web fundamentals topic.

Topic content

TL;DRTrigger dynamic import() on user actions (click/hover/focus) to load heavy optional features only when intent is shown
High Signal
Google
Meta
Agoda
Meesho
30-Second Answerstart every interview with this

Import on Interaction is a performance pattern that uses dynamic import() to load non-critical code only when the user explicitly interacts with a UI element. It reduces initial JavaScript bundle size while deferring cost to the moment of actual need. Combine with prefetching on hover/focus and Promise caching for smooth UX.

Static imports = pre-cooking everything before the restaurant opens. Import on Interaction = cooking expensive dishes only after the customer orders them. You save kitchen resources upfront, but the first order might take slightly longer unless you start prep on early signals like seeing the menu (hover/focus).

User Intent (Click / Hover / Focus)
dynamic import()
Fetch + Execute Chunk
Feature Ready (with loading UI)

1Basic Click Pattern

Load the module only when the user clicks. Cache the result so subsequent clicks are instant.

SettingsButton.jsxjsx
const handleOpen = async () => {
  if (!Modal) {
    const mod = await import('./SettingsModal');
    setModal(() => mod.default);
  }
  setOpen(true);
};

2Prefetch on Hover / Focus

Start loading on early intent signals (mouseenter, focus) to hide network latency before the actual click.

ChatTrigger.jsxjsx
<button 
  onMouseEnter={prefetch} 
  onFocus={prefetch} 
  onClick={handleClick}
>
  Open Chat
</button>

3Promise Caching

Cache the import Promise to avoid duplicate requests and simplify loading state management.

useLoadOnInteraction.jsjs
let modalPromise;
function loadModal() {
  if (!modalPromise) modalPromise = import('./Modal');
  return modalPromise;
}

4React Hook Pattern

Encapsulate loading, caching, states, and prefetching into a reusable hook for clean component code.

Key Takeaways
  • ✓Import on interaction defers heavy optional code until explicit user intent
  • ✓Combine click loading with hover/focus prefetching for smoother UX
  • ✓Always cache the import Promise to avoid redundant work
  • ✓Provide clear loading and error states
  • ✓Best for modals, editors, charts, admin tools — not core navigation
  • ✓Measure both initial bundle reduction and interaction-to-ready latency
PreviousNext

Topic Guide

On this page