Dynamic module importing is one of the latest JavaScript features to hit the major browsers. The main use case for this feature is lazy-loading modules to allow content to be delivered when it is needed, rather than all at once.

In this article, I’ll demonstrate how you can build a lazy-load router with Vue.js in just a few lines of code. This will work natively in browsers that have implemented dynamic module imports, but I’ll also include a fallback for older browsers.

Lazy loading components

Grab the finished code here on GitHub.

You may also like:
Lazy Loading ES2015 Modules in the Browser.

Static JavaScript Module Imports

If you’re using the latest version of any major browser, you can perform static import/export natively now. For Vue.js, this means you can export a component definition from a file like this:


And import it into your app like this:


Making component JavaScript modules allows you to organize your app such that every “page” is in a different file. This is nothing new if you use Vue.js single-file components, but with native support now, this architecture can be achieved without Webpack or Babel.

I cover importing Vue.js components as JavaScript modules more thoroughly in a recent article Vue.js Single-File JavaScript Components In The Browser.

Dynamic JavaScript Module Imports

If your components represent pages, it’d be better to fetch the module files on demand so that a user doesn’t have to download pages they don’t visit. Static imports are resolved at compile time, however. This means you can’t put import BooksPage from './BooksPage'; inside an if statement to conditionally load it. Instead, your static imports will start downloading as soon as the script that loads them runs.

That’s where dynamic imports come in. These can be determined at runtime, meaning you can conditionally load a JavaScript module, and therefore pages, on demand. Note that dynamic imports return a Promise which resolves the module content.

Note: component is a built-in “meta” component that accepts a component definition through the prop is. Like all Vue components, this can either be a component definition, or a Promise that resolves the component definition.

This code will work natively in the browser if you’re using the latest Safari or Chrome Canary, with other browsers soon to follow.

Creating a Tiny Lazy-Load Router

With that theory in mind, let’s make our lazy-load router. In your app template, create some anchor tags where the href is the path to the component module for that page. Listen to the click event on these elements, prevent the redirect, and instead trigger a method, navigate.


We’ll define the navigate method in the Vue instance and it will accept the click event as an argument. Use the value of the link’s href, i.e. event.target.pathname, to dynamically import the required page component module and assign it to the local page state property. This is dynamically bound to the component component.


Note that versions of Vue.js before 2.5.0 need to include a then callback to correctly resolve the module defintion.

That’s it! If you run this in a browser that supports dynamic imports you’ll see this:

Example output


What about users who don’t have the latest version of Safari or Chrome? They’ll need a fallback. Let’s create one with Webpack.

Firstly, we’ll have to modify the navigate method a bit. Webpack’s implementation of import() requires it to know ahead of time which files it may need to dynamically load. You don’t need to specify the exact module name, just make sure the value you provide to import() is a resolvable file or directory.

To do this, we’ll change the dynamic file name so it specifies the pages directory and extracts the filename from the href i.e. ./pages/${event.target.pathname.split("https://dzone.com/").pop()}. At compile-time, Webpack is smart enough to know this means “some file in the pages directory” and will process any JavaScript file in this directory.

Secondly, we need to put the comment /* webpackChunkName: "pages/[request]" */ in the function so that Webpack knows to extract this chunk to a separate file. Otherwise, Webpack will bundle all the page component modules in one file and the benefits of lazy-loading will be forfeit.


Webpack Config

You can use this simple Webpack config which has two notable features:

  1. Specifies a chunkFilename output property. This ensures the page component modules are named correctly in the Webpack output.
  2. Transpiles the JavaScript with Babel. You’ll need the plugin syntax-dynamic-import for Babel to recognize the dynamic import statement.

Run Webpack with that config and you’ll get build files including:

  • All the JavaScript module files transpiled to CommonJS modules.
  • The main bundle will include Webpack’s implementation of dynamic import.

Webpack dynamic import

Checking for Dynamic Import Support

How do you tell the browser to use the fallback? As far as I know, there’s no specific way to check for browser support for import(). My strategy is to use an inline script in the body of the document which creates a new script tag and will conditionally change the source (either the main script or the fallback) depending on support for import().

Note that the fallback script is treated as a normal JavaScript script, whereas the main script is treated as a module.


It’s great if we can use the native implementations of JavaScript module imports as optimizations like lazy-loading can be done with a smaller file size and much simpler implementation. For example, Webpack’s implementation of import() requires it to know ahead of time which files it may need to dynamically load, the native implementation does not.

In practice, features like this will need to be progressive enhancements and will, therefore, require Webpack as a fallback, which reintroduces the same complications. Oh well, that’s web dev for you.

Further Reading

Source link

Write A Comment