π€ Vue Lazy Loaded Routes β Boost Performance with Route-Level Code Splitting (2025 Guide)
π§² Introduction β What Is Lazy Loading in Vue Router?
Lazy loading in Vue refers to deferring the loading of components until they are actually needed. This is especially powerful in large Vue applications where loading everything upfront can slow down performance. Vue Router supports lazy loading of components via dynamic import()
statements in your route definitions.
π― In this guide, youβll learn:
- What lazy loaded routes are
- How to configure lazy loading in Vue Router
- Best practices for performance
- Real-world examples and structure
π¦ What Are Lazy Loaded Routes?
Lazy loaded routes load a component only when the route is visited. Instead of bundling everything into one large JavaScript file, Vue splits the app into smaller chunks.
π§ This results in:
- Faster initial load times
- On-demand loading of route components
π Basic Lazy Loading Syntax
Instead of importing components upfront:
import About from './views/About.vue';
You use a function to dynamically import:
const About = () => import('./views/About.vue');
Then define the route like this:
{
path: '/about',
component: () => import('./views/About.vue')
}
π§± Example β Lazy Load Multiple Routes
β
router/index.js
import { createRouter, createWebHistory } from 'vue-router';
const routes = [
{
path: '/',
name: 'Home',
component: () => import('../views/Home.vue')
},
{
path: '/about',
name: 'About',
component: () => import('../views/About.vue')
},
{
path: '/contact',
name: 'Contact',
component: () => import('../views/Contact.vue')
}
];
const router = createRouter({
history: createWebHistory(),
routes
});
export default router;
π§ Each route will now load its component only when the user navigates to it.
π¦ Real-World Use Case β Admin Panel
In large apps like dashboards or admin panels:
- Public routes (login, signup) can be loaded eagerly.
- Admin sections (settings, analytics, reports) can be lazy loaded to reduce the initial bundle size.
{
path: '/admin',
component: () => import('../views/admin/AdminLayout.vue'),
children: [
{ path: 'dashboard', component: () => import('../views/admin/Dashboard.vue') },
{ path: 'settings', component: () => import('../views/admin/Settings.vue') }
]
}
π§ Custom Chunk Naming (Optional)
You can give webpack chunk names to make debugging easier:
component: () => import(/* webpackChunkName: "about" */ './views/About.vue')
Webpack will name the chunk about.[hash].js
.
π Benefits of Lazy Loading Routes
Benefit | Description |
---|---|
π Faster Load Time | Loads only whatβs needed for initial route |
π§ Efficient Code Splitting | Helps break down a large app into logical chunks |
π Improved Performance | Smaller bundle sizes = better TTI (Time to Interactive) |
π Organized Structure | Encourages modular route-based development |
β οΈ Best Practices
Tip | Why It Matters |
---|---|
Group related views in folders | Helps structure large apps |
Avoid lazy loading critical routes | Keep home/login eager to avoid delays |
Use webpackChunkName comments | Easier debugging and bundle analysis |
Combine with route-level guards | Ensure auth before loading protected chunks |
π Summary β Recap & Next Steps
Vue’s lazy loaded routes make large applications faster and more maintainable. With just a small tweak to the route config, you can boost performance and create a smoother user experience.
π Key Takeaways:
- Use
() => import(...)
to enable lazy loading - Break your app into route-level chunks
- Combine with route guards and modular folder structures
- Use chunk names for debugging
βοΈ Real-World Relevance:
Essential for SPAs with dashboards, portals, and admin panels where performance and structure matter.
β FAQ Section
β What is a lazy loaded route in Vue?
β
A route where the component is loaded only when the route is visited using dynamic import()
.
β How do I enable lazy loading in Vue?
β Use this route format:
component: () => import('./views/About.vue')
β Can I lazy load nested routes?
β Yes. Nested or child routes can be lazy loaded the same way using dynamic imports.
β Should I lazy load all routes?
β No. Keep essential routes (like home/login) eagerly loaded for better UX.
β How do I name chunks when lazy loading?
β
Use the webpackChunkName
comment:
() => import(/* webpackChunkName: "about" */ './About.vue')
Share Now :