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:
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:
Lazy loading on routes increases the actual performance, being an important tool to improve the user experience of a web app.
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.
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.
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!
© All rights reserved. Made with ❤️ by BitterBrains, Inc.