How to speed up your app

Load what's absolutely needed at the beginning of the page load. The rest can be loaded on-demand.

import React from 'react'

// remove this import
import OnDemand from '../ondemand'

add React.lazy

import React from 'react'

const OnDemand = React.lazy(()=> import('../ondemand'))

and React.Suspense component higher up in the tree

 <React.Suspense fallback={<div>loading...</div>}>
          {showOnDemand ? <OnDemand /> : null}
 </React.Suspense>

circle-info

If you’re using webpackarrow-up-right to bundle your application, then you can use webpack magic commentsarrow-up-right to have webpack instruct the browser to prefetch dynamic imports.

const OnDemand = React.lazy(() => import(/* webpackPrefetch: true */ '../ondemand'))

We could have OnDemand start loading as soon as the user hovers over the element.

const loadOnDemand = () => import('../ondemand')
const OnDemand = React.lazy(loadOnDemand)

Next.js

Next.js supports ES2020 dynamic import()arrow-up-right for JavaScript. With it, you can import JavaScript modules dynamically and work with them. They also work with SSR.

Dynamically import the Hello component with loading component:

With no server-side rendering:

Lazy Loading components with React-Loadable

React-Loadablearrow-up-right - a higher-order component for dynamically loading a module before rendering it, a loading component is rendered while the module is unavailable.

Use only what you need

For example,

change to:

OR in this case, you can use the Babel plugin for lodash.

In your .babelrc :

Last updated

Was this helpful?