Web Performance Optimization & Lighthouse · Lektion

Code-Splitting und Lazy Loading

Teilen Sie große JavaScript-Bundles mit dynamischen Imports und routenbasiertem Splitting in bedarfsgerechte Chunks auf, damit Benutzer nur den tatsächlich benötigten Code herunterladen.

Lektion 4 von 413 Schritte

Code-Splitting und Lazy Loading ist eine kostenlose Web Performance Optimization & Lighthouse-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Web Performance Optimization & Lighthouse-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Web Performance Optimization & Lighthouse-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

The Monolithic Bundle Problem

Shipping all JavaScript in one bundle forces users to download code for pages they may never visit. Code splitting breaks the bundle into smaller chunks loaded only when needed.

Dynamic import()

The dynamic import() expression returns a promise and tells bundlers to create a separate chunk. The module loads on demand at runtime.

button.addEventListener('click', async () => {
  const { renderChart } = await import('./chart.js');
  renderChart();
});

Route-Based Splitting

The most impactful split is per route: each page becomes its own chunk, so the initial load contains only the landing route.

const Dashboard = React.lazy(() => import('./pages/Dashboard'));

Suspense Fallbacks

While a lazy chunk loads, show a fallback so the UI stays responsive. In React this is Suspense; other frameworks have equivalents.

<Suspense fallback={<Spinner />}>
  <Dashboard />
</Suspense>

Component-Level Splitting

Heavy widgets like rich text editors, maps, or charts can be split too, loading only when the user opens that feature rather than on first paint.

Vendor Splitting

Separating third-party libraries into a vendor chunk lets it cache independently. App code changes frequently; vendor code rarely does, so users re-download less.

Prefetching Chunks

Lazy loading can add a delay when the user navigates. Prefetch the chunk during idle time, for example on link hover, so it is ready before the click.

link.addEventListener('mouseenter', () => import('./pages/Settings'));

Avoiding Over-Splitting

Too many tiny chunks create request overhead and waterfalls. Aim for meaningful boundaries (routes, big features), not splitting every small function.

Tree Shaking Synergy

Code splitting pairs with tree shaking: use ES module imports and import only what you need so dead code never enters any chunk.

import { debounce } from 'lodash-es'; // only debounce is bundled

Measuring the Result

Use a bundle analyzer to inspect chunk sizes and the Coverage tab in DevTools to find unused code shipped on initial load. Lighthouse flags large unused JS.

Strategy Summary

  • Split by route first.
  • Lazy-load heavy components.
  • Separate vendor chunks for caching.
  • Prefetch likely-next chunks during idle.

Quick Check

Which JavaScript feature tells bundlers to create a separate chunk loaded on demand?

Recap

You learned to shrink initial payload with code splitting: dynamic import(), route- and component-level lazy loading, vendor chunking, idle prefetching, and tree shaking. Split at meaningful boundaries and measure with a bundle analyzer.

Kostenlos starten

Lerne Web Performance Optimization & Lighthouse mit einem KI-Tutor — kostenlos

Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.

Kurse
12
Lektionen
48

Häufig gestellte Fragen

Ist die Lektion „Code-Splitting und Lazy Loading“ kostenlos?

Ja — der vollständige Text von „Code-Splitting und Lazy Loading“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Web Performance Optimization & Lighthouse-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Web Performance Optimization & Lighthouse-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Code-Splitting und Lazy Loading“?

Teilen Sie große JavaScript-Bundles mit dynamischen Imports und routenbasiertem Splitting in bedarfsgerechte Chunks auf, damit Benutzer nur den tatsächlich benötigten Code herunterladen. Du übst Web Performance Optimization & Lighthouse mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Web Performance Optimization & Lighthouse zu starten?

Keine Vorkenntnisse erforderlich. Web Performance Optimization & Lighthouse auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „Code-Splitting und Lazy Loading“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Web Performance Optimization & Lighthouse-Lektion Code schreiben und ausführen?

Ja. Jede Web Performance Optimization & Lighthouse-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. JavaScript-Payload minimieren
  2. Strategien zum effizienten Laden von Skripten
  3. Web Workers und der Off-Main-Thread
  4. Code-Splitting und Lazy Loading
← Zurück zu Web Performance Optimization & Lighthouse