Route Optimization and Lazy Loading
🎯 Introduction
In this article, you will learn how route lazy loading and code splitting improve loading speed and performance in Vue 2 applications. Splitting code appropriately is especially useful in large SPAs: it reduces the initial download size and improves the experience for first-time visitors, making your application feel faster and smoother.
🚀 What Is Route Lazy Loading?
In Vue 2, route lazy loading means loading a page component when the user visits its route, rather than loading every page component at startup. This reduces the initial download size and speeds up the initial render.
The traditional approach imports components directly into the route configuration:
import Home from './views/Home.vue';
const routes = [{ path: '/home', component: Home }];
This approach bundles route components into the initial JavaScript bundle, which can become too large in bigger projects and hurt performance.
⚡ Lazy Loading with Dynamic import()
Vue 2 supports dynamically importing components with import(). Use it in the route configuration to load components on demand:
const routes = [
{
path: '/about',
component: () => import('./views/About.vue'),
},
];
Here, component is a function that returns a Promise. Vue Router calls it when the route is visited to load the corresponding component dynamically.
🎯 Naming Chunks with the webpackChunkName Magic Comment
When using dynamic imports, webpack's webpackChunkName magic comment lets you name chunks for easier debugging and cache management:
const routes = [
{
path: '/about',
component: () => import(/* webpackChunkName: "about" */ './views/About.vue'),
},
];
After bundling, About.vue is included in a separate file named about.[hash].js.
🧩 How Code Splitting Works and Why It Helps
Dynamic imports let webpack split code automatically. Route components are bundled into separate files that are requested only when the user visits the corresponding route.
Benefits:
- Reduce the initial bundle size and speed up the initial render
- Save bandwidth by avoiding loading all code at once
- Simplify cache management by updating only the chunks that have changed
💡 Practical Example: A Complete Lazy-Loaded Route Configuration
import Vue from 'vue';
import Router from 'vue-router';
Vue.use(Router);
// Define components with dynamic imports
const Home = () => import(/* webpackChunkName: "home" */ './views/Home.vue');
const About = () => import(/* webpackChunkName: "about" */ './views/About.vue');
export default new Router({
mode: 'history',
routes: [
{ path: '/', redirect: '/home' },
{ path: '/home', component: Home },
{ path: '/about', component: About },
],
});
The home and about pages are split into separate files and loaded only when visited.
❓ Questions and Answers
Q1: Why use route lazy loading?
Route lazy loading reduces the initial download size, speeds up the initial render, and improves application performance.
Q2: What is webpack's webpackChunkName magic comment?
It tells webpack how to name a dynamically loaded chunk, helping generate readable filenames that can be cached.
🧪 Exercises
- In an existing Vue 2 route configuration, lazy-load the
/contactroute and use a magic comment to name its chunkcontact.
const routes = [
{
path: '/contact',
component: '' /* Write the lazy-loading code here */,
},
];
🎉 You now understand route lazy loading, webpack magic comments, and code splitting in Vue 2! These techniques help improve frontend performance. Keep practicing to make your applications faster and smoother.
