1. Code
  2. Coding Fundamentals
  3. Performance

Dramatically Speed Up Your React Front-End App Using Lazy Loading

Scroll to top

A constant challenge faced by front-end developers is the performance of our applications. How can we deliver a robust and full-featured application to our users without forcing them to wait an eternity for the page to load? The techniques used to speed up a website are so numerous that it can often be confusing to decide where to focus our energy when optimising for performance and speed.

Thankfully, the solution isn't as complicated as it sometimes might seem. In this post, I'll break down one of the most effective techniques used by large web apps to speed up their user experience. I'll go over a package to facilitate this and ensure that we can deliver our app to users faster without them noticing that anything has changed.

What Does It Mean for a Website to Be Fast?

The question of web performance is as deep as it is broad. For the sake of this post, I'm going to try and define performance in the simplest terms: send as little as you can as fast as you can. Of course, this might be an oversimplification of the problem, but practically speaking, we can achieve dramatic speed improvements by simply sending less data for the user to download and sending that data fast.

For the purpose of this post, I'm going to focus on the first part of this definition—sending the least possible amount of information to the user's browser. 

Invariably, the biggest offenders when it comes to slowing down our applications are images and JavaScript. In this post, I'm going to show you how to deal with the problem of large application bundles and speed up our website in the process.

React Loadable

React Loadable is a package that allows us to lazy load our JavaScript only when it's required by the application. Of course, not all websites use React, but for the sake of brevity I'm going to focus on implementing React Loadable in a server-side rendered app built with Webpack. The final result will be multiple JavaScript files delivered to the user's browser automatically when that code is needed. If you want to try out the completed code, you can clone the example source code from our GitHub repo.

Using our definition from before, this simply means we send less to the user up front so that data can be downloaded faster and our user will experience a more performant site.

1. Add React Loadable to Your Component

I'll take an example React component, MyComponent. I'll assume this component is made up of two files, MyComponent/MyComponent.jsx and MyComponent/index.js.

In these two files, I define the React component exactly as I normally would in MyComponent.jsx. In index.js, I import the React component and re-export it—this time wrapped in the Loadable function. Using the ECMAScript import feature, I can indicate to Webpack that I expect this file to be dynamically loaded. This pattern allows me to easily lazy load any component I've already written. It also allows me to separate the logic between lazy loading and rendering. That might sound complicated, but here's what this would look like in practice:

1
// MyComponent/MyComponent.jsx

2
3
export default () => (
4
  <div>
5
    This component will be lazy-loaded!
6
  </div>

7
)
1
// MyComponent/index.js

2
3
import Loadable from 'react-loadable'
4
5
export default Loadable({
6
  // The import below tells webpack to 

7
  // separate this code into another bundle

8
  loader: import('./MyComponent')
9
})

I can then import my component exactly as I normally would:

1
// anotherComponent/index.js

2
3
import MyComponent from './MyComponent'
4
5
export default () => <MyComponent />

I've now introduced React Loadable into MyComponent. I can add more logic to this component later if I choose—this might include introducing a loading state or an error handler to the component. Thanks to Webpack, when we run our build, I'll now be provided with two separate JavaScript bundles: app.min.js is our regular application bundle, and myComponent.min.js contains the code we've just written. I'll discuss how to deliver these bundles to the browser a little later.

2. Simplify the Setup With Babel

Ordinarily, I'd have to include two extra options when passing an object to the Loadable function, modules and webpack. These help Webpack identify which modules we should be including. Thankfully, we can obviate the need to include these two options with every component by using the react-loadable/babel plugin. This automatically includes these options for us:

1
// input file

2
3
import Loadable from 'react-loadable'
4
5
export default Loadable({
6
  loader: () => import('./MyComponent')
7
})
1
// output file 

2
3
import Loadable from 'react-loadable'
4
import path from 'path'
5
6
export default Loadable({
7
  loader: () => import('./MyComponent'),
8
  webpack: () => [require.resolveWeak('./MyComponent')],
9
  modules: [path.join(__dirname, './MyComponent')]
10
})

I can include this plugin by adding it to my list of plugins in my .babelrc file, like so:

1
{
2
  "plugins": ["react-loadable/babel"]
3
}

I'm now one step closer to lazy loading our component. However, in my case, I'm dealing with server-side rendering. Currently, the server will not be able to render our lazy-loaded components.

3. Rendering Components on the Server

In my server application, I have a standard configuration that looks something like this:

1
// server/index.js

2
3
app.get('/', (req, res) => {
4
  const markup = ReactDOMServer.renderToString(
5
    <MyApp/>
6
  )
7
8
  res.send(`

9
    <html>

10
      <body>

11
        <div id="root">${markup}</div>

12
        <script src="/build/app.min.js"></script>

13
      </body>

14
    </html>

15
  `)
16
})
17
18
app.listen(8080, () => {
19
  console.log('Running...')
20
})

The first step is going to be to instruct React Loadable that I want all modules to be preloaded. This allows me to decide which ones should be loaded immediately on the client. I do this by modifying my server/index.js file like so:

1
// server/index.js 

2
3
Loadable.preloadAll().then(() => {
4
  app.listen(8080, () => {
5
    console.log('Running...')
6
  })
7
})

The next step is going to be to push all components I want to render to an array so we can later determine which components require immediate loading. This is so the HTML can be returned with the correct JavaScript bundles included via script tags (more on this later). For now, I'm going modify my server file like so:

1
// server/index.js

2
3
import Loadable from 'react-loadable'
4
5
app.get('/', (req, res) => {
6
  const modules = []
7
  const markup = ReactDOMServer.renderToString(
8
    <Loadable.Capture report={moduleName => modules.push(moduleName)}>
9
      <MyApp/>
10
    </Loadable>

11
  )
12
13
  res.send(`

14
    <html>

15
      <body>

16
        <div id="root">${markup}</div>

17
        <script src="/build/app.min.js"></script>

18
      </body>

19
    </html>

20
  `)
21
})
22
23
Loadable.preloadAll().then(() => {
24
  app.listen(8080, () => {
25
    console.log('Running...')
26
  })
27
})

Every time a component is used that requires React Loadable, it will be added to the modules array. This is an automatic process done by React Loadable, so this is all that's required on our part for this process.

Now we have a list of modules that we know will need to be rendered immediately. The problem we now face is mapping these modules to the bundles that Webpack has automatically produced for us.

4. Mapping Webpack Bundles to Modules

So now I've instructed Webpack to create myComponent.min.js, and I know that MyComponent is being used immediately, so I need to load this bundle in the initial HTML payload we deliver to the user. Thankfully, React Loadable provides a way for us to achieve this, as well. In my client Webpack configuration file, I need to include a new plugin:

1
// webpack.client.config.js

2
3
import { ReactLoadablePlugin } from 'react-loadable/webpack'
4
5
plugins: [
6
  new ReactLoadablePlugin({
7
    filename: './build/loadable-manifest.json'
8
  })
9
]

The loadable-manifest.json file will provide me a mapping between modules and bundles so that I can use the modules array I set up earlier to load the bundles I know I'll need. In my case, this file might look something like this:

1
// build/loadable-manifest.json

2
3
{
4
  "MyComponent": "/build/myComponent.min.js"
5
}

This will also require a common Webpack manifest file to include the mapping between modules and files for internal Webpack purposes. I can do this by including another Webpack plugin:

1
plugins: [
2
  new webpack.optimize.CommonsChunkPlugin({
3
    name: 'manifest',
4
    minChunks: Infinity
5
  })
6
]

5. Including Bundles in Your HTML

The final step in loading our dynamic bundles on the server is to include these in the HTML we deliver to the user. For this step, I'm going to combine the output of steps 3 and 4. I can start by modifying the server file I created above:

1
// server/index.js

2
3
import Loadable from 'react-loadable'
4
import { getBundles } from 'react-loadable/webpack'
5
import manifest from './build/loadable-manifest.json'
6
7
app.get('/', (req, res) => {
8
  const modules = []
9
  const markup = ReactDOMServer.renderToString(
10
    <Loadable.Capture report={moduleName => modules.push(moduleName)}>
11
      <MyApp/>
12
    </Loadable>

13
  )
14
  
15
  const bundles = getBundles(manifest, modules)
16
17
  // My rendering logic below ...

18
})
19
20
Loadable.preloadAll().then(() => {
21
  app.listen(8080, () => {
22
    console.log('Running...')
23
  })
24
})

In this, I've imported the manifest and asked React Loadable to create an array with module/bundle mappings. The only thing left for me to do is to render these bundles to an HTML string:

1
// server/index.js

2
3
app.get('/', (req, res) => {
4
  // My App & modules logic

5
6
  res.send(`

7
    <html>

8
      <body>

9
        <div id="root">${markup}</div>

10
        <script src="/build/manifest.min.js"></script>

11
        ${bundles.map(({ file }) =>
12
          `<script src="/build/${file}"></script>`
13
        }).join('\n')}

14
        <script src="/build/app.min.js"></script>

15
      </body>

16
    </html>

17
  `)
18
})
19
20
Loadable.preloadAll().then(() => {
21
  app.listen(8080, () => {
22
    console.log('Running...')
23
  })
24
})

6. Load the Server-Rendered Bundles on the Client

The final step to using the bundles that we've loaded on the server is to consume them on the client. Doing this is simple—I can just instruct React Loadable to preload any modules it's found to be immediately available:

1
// client/index.js

2
3
import React from 'react'
4
import { hydrate } from 'react-dom'
5
import Loadable from 'react-loadable'
6
7
import MyApplication from './MyApplication'
8
9
Loadable.preloadReady().then(() => {
10
  hydrate(
11
    <MyApplication />,
12
    document.getElementById('root')
13
  );
14
});

Conclusion

Following this process, I can split my application bundle into as many smaller bundles as I need. In this way, my app sends less to the user and only when they need it. I've reduced the amount of code that needs to be sent so that it can be sent faster. This can have significant performance gains for larger applications. It can also set smaller applications up for rapid growth should the need arise. 

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.