Lazy Loading Routes: Faster Initial Loads
Lazy loading routes splits your app into smaller chunks, loading code only when a user visits a specific page. This drastically cuts initial load times for large SPAs. Just be sure not to use Vue's async components directly as route components.
Why it exists
When building a single-page application (SPA) with a bundler, all your code can end up in one large JavaScript file. This bundle must be downloaded and parsed before the user sees anything, which can be very slow. Lazy loading was created to solve this by splitting the code and loading it on demand.
The mental model
Think of your app not as one big book, but as a library. When a user wants to read about 'User Profiles', you don't hand them the entire library at the entrance. You fetch just the 'User Profiles' section from the shelf when they ask for it. The first fetch takes a moment, but subsequent reads are instant because the app keeps it handy (cached).
How it works
Vue Router supports lazy loading out of the box. Instead of statically importing a component at the top of your file, you provide the route's component option with a function that returns a dynamic import. A bundler like Vite or webpack sees the import() function and automatically splits that component into a separate file, or 'chunk'. Vue Router only executes this function and fetches the chunk when a user navigates to the route for the first time. On subsequent visits, it uses the cached version.
When to use it
It's a best practice to use lazy loading for all routes in any bundled Vue application. The performance benefit is significant for medium to large apps, as it directly improves the initial page load time, a critical user experience metric. Modern bundlers make this process seamless with code splitting.
When not to use it
You might group several related routes into a single chunk if they are often visited together, like a user settings area with multiple sub-pages. This avoids multiple small network requests. This is done using bundler-specific options, like webpack's / webpackChunkName: "group-name" / comment. The main footgun is confusing this with Vue's async components; the route component itself must be a dynamic import function, not an async component definition.
One canonical example
Instead of a static import that bloats the main bundle:
import UserDetails from './views/UserDetails.vue';
const routes = [{ path: '/users/:id', component: UserDetails }];You use a dynamic import function to enable lazy loading:
const routes = [{ path: '/users/:id', component: () => import('./views/UserDetails.vue') }];This simple change tells the bundler to create a separate chunk for UserDetails.vue, which Vue Router will then fetch on demand.
Interview question
How does lazy loading routes primarily improve performance in a large Vue Single-Page Application (SPA)?
- a.By reducing the total number of network requests made by the application.
- b.By allowing the browser to cache all component definitions more efficiently from the start.
- c.By enabling server-side rendering for all routes, improving SEO and perceived performance.
- d.By decreasing the size of the initial JavaScript bundle, leading to faster download and parsing times.Correct
Why? this is the answer
Lazy loading's core benefit is to reduce the initial JavaScript bundle size, which directly leads to faster download and parsing times for the user's first interaction. While it splits code into multiple chunks, potentially increasing the total number of network requests over time, these requests are smaller and made on demand, improving the critical initial load.
Just read this? Test yourself on what you have been reading.
Read the original → router.vuejs.org
- #vue
- #performance
- #routing
- #code-splitting
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on vue — each one lists the topics its interview covers.
See open roles