Tutorial

Lazy-loading Routes in Your Vue.js App

Published on March 19, 2017
Default avatar

By Joshua Bemenderfer

Lazy-loading Routes in Your Vue.js App

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking 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.

Usage

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([], () => {
    resolve(require('./routes/OtherMassivePage.vue'))
  })
}

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.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Joshua Bemenderfer

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel