Module/Nomodule Pattern for Critical JS
You'll learn how to serve modern JavaScript to modern browsers while providing a fallback for older ones, optimizing performance for everyone.
The Core Idea
Modern browsers support ES modules (type="module"), which means they understand modern JavaScript syntax (arrow functions, classes, async/await, etc.). Older browsers don't. The module/nomodule pattern lets you ship two JavaScript bundles: a smaller, modern one for capable browsers and a larger, transpiled one for legacy browsers—and each browser automatically picks the right version.
How It Works
<!-- Modern browsers load this (and ignore nomodule) -->
<script type="module" src="app.modern.js"></script>
<!-- Legacy browsers load this (they ignore type="module") -->
<script nomodule src="app.legacy.js"></script>
Modern browsers see type="module" and load that script, ignoring the nomodule attribute entirely. Legacy browsers don't understand type="module", so they skip it—but they do recognize and load the nomodule script.
Why This Matters for Performance
Your modern bundle (app.modern.js) can be 20-40% smaller because it doesn't need Babel transformations or polyfills. Most of your users (90%+) on modern browsers get faster downloads, parsing, and execution. Legacy users still get a working experience—they just don't penalize everyone else.
Think of it like a restaurant menu: one for locals (in the native language, concise) and one for tourists (translated, more verbose). Everyone gets fed, but locals get faster service.
Key Takeaway: The module/nomodule pattern delivers optimal JavaScript bundles based on browser capability—modern browsers get lean, fast code while legacy browsers receive compatible fallbacks, maximizing performance without breaking support.