Dynamic Import Use Cases
Dynamic Import Use Cases
Section titled “Dynamic Import Use Cases”Introduction
Section titled “Introduction”Dynamic imports shine in scenarios where code doesn’t need to load upfront. Here are the most common and effective use cases.
Route-Based Splitting
Section titled “Route-Based Splitting”Load page components only when the user navigates to them:
// SPA Routerconst routes = { '/': () => import('./pages/Home.js'), '/dashboard': () => import('./pages/Dashboard.js'), '/settings': () => import('./pages/Settings.js'),};
async function navigate(path) { const module = await routes[path](); module.render();}Conditional Loading
Section titled “Conditional Loading”Load heavy libraries only when a feature is triggered:
async function showChart(data) { // Chart library is large — only load when user opens chart const { Chart } = await import('chart.js'); new Chart(ctx, { data });}
async function handlePDFExport() { const { jsPDF } = await import('jspdf'); const doc = new jsPDF(); doc.save('report.pdf');}Loading Polyfills on Demand
Section titled “Loading Polyfills on Demand”Serve polyfills only to browsers that need them:
if (!('IntersectionObserver' in window)) { const module = await import('intersection-observer'); module.polyfill();}
if (typeof ResizeObserver === 'undefined') { await import('@juggle/resize-observer');}Internationalization (i18n)
Section titled “Internationalization (i18n)”Load locale data lazily:
async function loadLocale(lang) { const messages = await import(`./locales/${lang}.js`); i18n.setMessages(messages);}
// Only loads 'en' by default; user can switch to othersloadLocale('en');Dynamic Theme Loading
Section titled “Dynamic Theme Loading”async function setTheme(theme) { if (theme === 'dark') { await import('./themes/dark.css'); } else { await import('./themes/light.css'); }}Summary
Section titled “Summary”- Route-based: load page components per route
- Conditional: load heavy libs only when triggered
- Polyfills: serve only to browsers that need them
- i18n: load locale data on demand
- Themes: switch styles without initial CSS burden