Home / Blog / Lazy Load Routes
Lazy Load Routes

Lazy Load Routes

Alex Jover Morales
Alex Jover Morales
Updated: May 15th 2022
This entry is part 3 of 4 in the series Creating Your Own Router

Lazy Loading and Code Splitting are two of the main points of the PRPL Pattern, a pattern named by Google which intent is to to provide some structure to make the initial load of a Progressive Web App (PWA) as fast as possible, boosting the performance.

The PRPL pattern stands for:

  • Push critical resources for the initial URL route.
  • Render initial route.
  • Pre-cache remaining routes.
  • Lazy-load and create remaining routes on demand.

User engagement is dependant on how satisfied the user is using your app, and performance takes a great part. We can separate web performance in:

  • Actual performance: describes how long take the functional actions and states of an app to be in a ready state. Ajax calls durations or app loading time are some examples.
  • Perceptual performance: describes how fast and smooth those actions and states of an app are perceived by the user. It sums up to the actual performance. In other words, if the actual performance increases, the perceived performance increases as well, but when actual performance cannot be increased then the user experience can be improved by increasing the perceptual performance. Time to Interactive, Perceptual Speed Index and animations are some examples.

Lazy loading on routes increases the actual performance, being an important tool to improve the user experience of a web app.

Use Lazy Loading on the Router

You might think this can be a pain to implement, but... What if I tell you that this router already works with lazy loaded routes out-of-the-box?

Yes, that's right. Vue.js template’s compile to render functions, which get a createElement function as a parameter that allows to pass a function returning a promise. An example would be:

render(createElement) {
  return createElement(() => Promise.resolve('<div>Hey boy</div>'))
}

That way, we can use the dynamic import to load and render a component asynchronously because it returns a promise with the module as the payload.

Given that the <component> element gets compiled to a render function, in AppRouter.vue we can remove the static imports and instead use dynamic imports:

// AppRouter.vue
const routes = {
  "/": () => import("./Home"),
  "/articles": () => import("./Articles")
};

If you try it out and open the network tab on the browser's devtools, you'll see it's working and creating separate chunks with a name like 0.chunk.js which are loaded as you press the Next button.

Note: you must try it locally, you won’t see any chunks in the network tab if you try it from the CodeSandbox example.

Wrapping up

If you've reached this point, that means you've built a router in Vue.js yourself! It all started creating a basic router from scratch, but hey, the router is component based, uses the HTML5 History API, and it can be easily used for lazy load code-split routes! It wasn't hard, right? Vue.js indeed helps making it that easy.

For now, the router is tied to our app, but we’ll see how to make it a Vue.js plugin so it’s 100% reusable and exportable to an npm package.

Start learning Vue.js for free

Alex Jover Morales
Alex Jover Morales
Passionate web developer. Author of Test Vue.js components with Jest on Leanpub. I co-organize Alicante Frontend. Interested in web performance, PWA, the human side of code and wellness. Cat lover, sports practitioner and good friend of his friends. His bike goes with him.

Comments

Latest Vue School Articles

From Vue.js Options API to Composition API: Is it Worth it?

From Vue.js Options API to Composition API: Is it Worth it?

Explore the technicalities of transitioning from Options API to Composition API in Vue.js. Discover if migrating your app is worth the effort in our detailed guide
Mostafa Said
Mostafa Said
What’s New in Nuxt 4

What’s New in Nuxt 4

Have anxiety about a new major version of Nuxt coming out? Worried about a big migration project? Don’t worry about it, a peaceful and easy upgrade is literally one of the features of Nuxt version 4.
Daniel Kelly
Daniel Kelly

Our goal is to be the number one source of Vue.js knowledge for all skill levels. We offer the knowledge of our industry leaders through awesome video courses for a ridiculously low price.

More than 200.000 users have already joined us. You are welcome too!

Follow us on Social

© All rights reserved. Made with ❤️ by BitterBrains, Inc.