0Pricing
Vue Academy · Lesson

Lazy Loading and Code Splitting

Split bundles and lazy-load components and routes.

Lazy Loading and Code Splitting is a free Vue Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Bundle Size Matters

When everything ships in one big JavaScript file, users wait longer for the first screen to appear. Code splitting breaks your app into smaller chunks loaded only when needed, shrinking the initial download and speeding up startup.

Dynamic import()

The key tool is the dynamic import() function. Unlike a static import at the top of a file, it loads a module on demand and returns a promise. Bundlers split each dynamic import into its own chunk.

const module = await import("./heavyModule.js")
module.doSomething()

Route-Level Code Splitting

The biggest win is splitting by route, so each page loads its own code. In Vue Router, pass a function returning a dynamic import instead of importing the component directly.

const routes = [
  {
    path: "/dashboard",
    component: () => import("./views/Dashboard.vue")
  }
]

Static vs Lazy Routes

Compare the two styles. The static import bundles the component into the main chunk; the lazy version creates a separate chunk fetched only when the route is visited.

// eager (always loaded)
import Dashboard from "./views/Dashboard.vue"

// lazy (loaded on demand)
const Dashboard = () => import("./views/Dashboard.vue")

defineAsyncComponent

To lazy-load a component outside the router, wrap a dynamic import with defineAsyncComponent. Vue loads it only when it first renders.

import { defineAsyncComponent } from "vue"

const Chart = defineAsyncComponent(() =>
  import("./components/HeavyChart.vue")
)

Registering Async Components

Register an async component like any other and use it in your template. Vue handles the loading transparently.

export default {
  components: {
    HeavyChart: defineAsyncComponent(() =>
      import("./components/HeavyChart.vue")
    )
  }
}

Loading and Error States

The advanced form of defineAsyncComponent accepts options to show a placeholder while loading and a fallback if it fails.

const Chart = defineAsyncComponent({
  loader: () => import("./HeavyChart.vue"),
  loadingComponent: Spinner,
  errorComponent: LoadError,
  delay: 200,
  timeout: 5000
})

Lazy-Loading Heavy Components

Good candidates for lazy loading are heavy or rarely used pieces: charts, rich text editors, maps, or modal dialogs that only some users open.

const RichEditor = defineAsyncComponent(() =>
  import("./components/RichTextEditor.vue")
)
// only downloaded when the editor is actually shown

Conditional Loading with v-if

Pair an async component with v-if so its chunk is fetched only when the user triggers it.

<template>
  <button @click="showEditor = true">Edit</button>
  <RichEditor v-if="showEditor" />
</template>

Named Chunks for Debugging

Webpack-style magic comments give chunks readable names, making your network tab and build output easier to inspect.

const Reports = () => import(
  /* webpackChunkName: "reports" */ "./views/Reports.vue"
)

Measuring the Impact

After splitting, check your build output or browser network tab. You should see multiple smaller chunk files instead of one large bundle, and the initial load should download less code.

Quick Check

Test your knowledge of lazy loading.

Recap

Code splitting with dynamic import() reduces the initial bundle. Use a function-returning-import for lazy routes and defineAsyncComponent for on-demand components, optionally with loading and error states. Lazy-load heavy or rarely used pieces to keep startup fast. Next you will cache and skip unnecessary work with memoization.

Frequently asked questions

Is the “Lazy Loading and Code Splitting” lesson free?

Yes — the full text of “Lazy Loading and Code Splitting” is free to read here on the web, and the Vue Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Vue Academy course, upgrade to CoddyKit PRO.

What will I learn in “Lazy Loading and Code Splitting”?

Split bundles and lazy-load components and routes. You practise Vue Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Vue Academy?

No prior experience is required. Vue Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Lazy Loading and Code Splitting” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Vue Academy lesson?

Yes. Every Vue Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Lazy Loading and Code Splitting
  2. Memoization Techniques
  3. Identifying and Fixing Bottlenecks
← Back to Vue Academy