Accessibility
Code-splitting
- bundling - the process of
following imported files
and merging
them into a single file
- create react app / Next.js / Gatsby uses
Webpack
- create multiple bundles that can be
dynamically loaded at runtime
(improve performance)
- lazy-load only the things needed to improve performance
Dynamic import()
- when Webpack comes across dynamic import, it automatically starts
code-splitting
th app
import("./math").then(math => {
console.log(math.add(16,26));
})
React.lazy
- render a dynamic import as a regular component
- takes a function that must call a dynamic
import()
and return a Promise
which resolves to a module with a default
export containing a React component
- a lazy component should render inside a
Suspense
component (with fallback content, ex: loading)
- wrap the lazy component with an error boundary to show a error message when modules fails to load
import React, { Suspense } from 'react';
import MyErrorBoundary from './MyErrorBoundary';
const OtherComponent = React.lazy(() => import('./OtherComponent'));
const AnotherComponent = React.lazy(() => import('./AnotherComponent'));
const MyComponent = () => (
<div>
<MyErrorBoundary>
<Suspense fallback={<div>Loading...</div>}>
<section>
<OtherComponent />
<AnotherComponent />
</section>
</Suspense>
</MyErrorBoundary>
</div>
);
Reference