Lazy-loading Routes in Your Vue.js App


While this tutorial has content that we believe is of great benefit to our community, we have not yet tested or edited it to ensure you have an error-free learning experience. It's on our list, and we're working on it! You can help us out by using the "report an issue" button at the bottom of the tutorial.

As your SPA (Single-Page Application, for the uninitiated) grows in complexity, so does the size of the application bundle. After a point, it becomes a significant hindrance to the load times of your page. Thankfully, vue-router supports webpack’s built in async module loading system. As a result, it’s now trivial to separate routed components for lesser-used routes into bundles that are loaded on-demand when the route is accessed.


Supposing your route configuration is something like this:

import MainPage from './routes/MainPage.vue'
import OtherMassivePage from './routes/OtherMassivePage.vue'

const routes = [
  { path: '/main', component: MainPage },
  { path: '/other', component: OtherMassivePage }

Literally all you need to do to split OtherMassivePage and all of its dependencies (that aren’t shared by anything else) into a separate chunk is to replace the import statement with a scary-looking require.ensure call.

If you reload your app, you should notice that nothing seems different, until you check the network developer tools and see that there’s a new file being loaded when you first load the /other route.

import MainPage from './routes/MainPage.vue'
const OtherMassivePage = r => require.ensure([], () => r(require('./routes/OtherMassivePage.vue')))

const routes = [
  { path: '/main', component: MainPage },
  { path: '/other', component: OtherMassivePage }

Yeah. I know it looks scary, but trust me, it’s not as bad as it first appears.

It’s sort of like a promise that eventually resolves to the loaded component. A not-shorthand version of that would look like this:

const OtherMassivePage = resolve => {
  // The empty array is for specifying other dependencies that need to be loaded.
  require.ensure([], () => {

Unfortunately, you can’t really use any abstractions or wrappers to make this shorter, as webpack uses static analysis to detect and split chunks. The best you can do is use those one-liners to take up less space.

Combining Routes

Sometimes you might have multiple routes or components that you want in the same chunk. To accomplish this, you can simply pass a third parameter to require.ensure that specifies the name of the group to group these components under.

// Both routes are output in the same chunk and bundle, causing that bundle to be lazy-loaded when either route is accessed.
const OtherMassivePage = r => require.ensure([], () => r(require('./routes/OtherMassivePage.vue')), 'big-pages')
const WeightLossPage = r => require.ensure([], () => r(require('./routes/WeightLossPage.vue')), 'big-pages')

Unlike many other tasks with webpack, this is an unexpectedly simple method to produce an amazingly useful result. I’d definitely recommend using this pattern if you’re working on large SPAs that are becoming bloated.

Creative Commons License