MediumBundle Optimization📖 Theory Question

What is the module/nomodule pattern for differential serving?

💡

Hint

Ship modern ESM to capable browsers and legacy transpiled bundle to IE/old browsers — reduces bundle size for majority of users

Full Answer

Differential serving ships two builds: a modern one (ES2020+ with minimal transpilation) and a legacy one (ES5 for old browsers).

<!-- Modern browsers load this (smaller, faster) -->
<script type="module" src="app.modern.js"></script>

<!-- Legacy browsers (IE11) load this; modern browsers ignore it -->
<script nomodule src="app.legacy.js"></script>

Why it matters:

  • Babel transpilation of async/await, optional chaining, nullish coalescing adds ~10–20% to bundle size in polyfills.
  • 95%+ of users are on modern browsers — they get a leaner bundle.
  • IE11 and very old browsers get the bloated legacy bundle — acceptable since they're a tiny minority.

In practice: Vite handles this automatically via @vitejs/plugin-legacy. Next.js's browserslist config controls transpilation targets.

More Bundle Optimization Questions

EasyWhat is tree shaking and what conditions must be met for it to work?→EasyWhat is code splitting and how does dynamic import() enable it?→EasyHow do you analyze and diagnose large bundle sizes?→EasyWhat is vendor chunk splitting and why is it important for caching?→

Practice this in a timed sprint →

5 free questions, no signup required

⚡ Start Sprint