Skip to content

Dynamic Import Use Cases

Dynamic imports shine in scenarios where code doesn’t need to load upfront. Here are the most common and effective use cases.

Load page components only when the user navigates to them:

// SPA Router
const 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();
}

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');
}

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');
}

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 others
loadLocale('en');
async function setTheme(theme) {
if (theme === 'dark') {
await import('./themes/dark.css');
} else {
await import('./themes/light.css');
}
}
  • 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