Preload modules before they're needed:
<link rel="modulepreload" href="/js/heavy-module.js">
<script type="module" src="/js/app.js"></script>
✅ Benefits: Faster load times, reduced waterfall effects
Remove unused code during bundling:
// utils.js - exports many functions
export function add(a, b) { return a + b; }
export function multiply(a, b) { return a * b; }
// main.js - only imports what's needed
import { add } from './utils.js';
// multiply() is removed from final bundle!
Requirements:
Load modules only when needed:
// Load heavy feature on user interaction
button.addEventListener('click', async () => {
const { init } = await import('./heavy-feature.js');
init();
});
ES Modules work great with HTTP/2's multiplexing:
Split code into vendor, app, and route chunks:
// vite.config.js
export default {
build: {
rollupOptions: {
output: {
manualChunks: {
vendor: ['react', 'react-dom'],
utils: ['lodash']
}
}
}
}
}
✅ Do
❌ Avoid