dynamic-import

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Dynamic Import

动态导入

In our chat application, we have four key components:
UserInfo
,
ChatList
,
ChatInput
and
EmojiPicker
. However, only three of these components are used instantly on the initial page load:
UserInfo
,
ChatList
and
ChatInput
. The
EmojiPicker
isn't directly visible, and may not even be rendered at all if the user won't even click on the
Emoji
in order to toggle the
EmojiPicker
. This would mean that we unnecessarily added the
EmojiPicker
module to our initial bundle, which potentially increased the loading time!
In order to solve this, we can dynamically import the
EmojiPicker
component. Instead of statically importing it, we'll only import it when we want to show the
EmojiPicker
. An easy way to dynamically import components in React is by using React Suspense. The
React.Suspense
component receives the component that should be dynamically loaded, which makes it possible for the
App
component to render its contents faster by suspending the import of the
EmojiPicker
module!
在我们的聊天应用中,有四个核心组件:
UserInfo
ChatList
ChatInput
EmojiPicker
。不过,初始页面加载时仅会立即用到其中三个组件:
UserInfo
ChatList
ChatInput
EmojiPicker
并非直接可见,而且如果用户根本不会点击「表情」按钮来唤起它,这个组件甚至可能完全不会渲染。这就意味着我们毫无必要地将
EmojiPicker
模块加入了初始打包文件中,这可能会拖慢页面加载速度!
为了解决这个问题,我们可以动态导入
EmojiPicker
组件。不再采用静态导入的方式,而是仅在需要显示
EmojiPicker
时才导入它。在 React 中实现组件动态导入的简便方式是使用 React Suspense
React.Suspense
组件可以包裹需要动态加载的组件,这样
App
组件就能通过延迟导入
EmojiPicker
模块,更快地渲染内容!

When to Use

使用场景

  • Use this when certain modules are only needed based on user interaction or conditions
  • This is helpful when you want to reduce the initial bundle size for faster page loads
  • Use this when components like modals, pickers, or heavy libraries aren't needed on initial render
  • 当某些模块仅在用户交互或特定条件下才需要时使用
  • 适用于希望减小初始打包体积以加快页面加载速度的场景
  • 当初始渲染不需要模态框、选择器或大型库等组件时使用

Instructions

操作说明

  • Use
    React.lazy
    with
    Suspense
    for dynamic component imports in React
  • Provide meaningful fallback UI while dynamically imported modules are loading
  • Consider using
    loadable-components
    for SSR applications where React Suspense isn't supported
  • Only dynamically import modules that aren't critical to the initial render
  • 在 React 中结合
    React.lazy
    Suspense
    实现组件动态导入
  • 在动态导入模块加载过程中,提供有意义的备用 UI
  • 如果是不支持 React Suspense 的 SSR 应用,考虑使用
    loadable-components
  • 仅对非初始渲染关键模块进行动态导入

Details

细节说明

Instead of unnecessarily adding
EmojiPicker
to the initial bundle, we can split it up into its own bundle and reduce the size of the initial bundle!
A smaller initial bundle size means a faster initial load: the user doesn't have to stare at a blank loading screen for as long. The
fallback
component lets the user know that our application hasn't frozen: they simply need to wait a little while for the module to be processed and executed.
Asset                             Size         Chunks            Chunk Names
emoji-picker.bundle.js           1.48 KiB      1    [emitted]    emoji-picker
main.bundle.js                   1.33 MiB      main [emitted]    main
vendors~emoji-picker.bundle.js   171 KiB       2    [emitted]    vendors~emoji-picker
Whereas previously the initial bundle was
1.5MiB
, we've been able to reduce it to
1.33 MiB
by suspending the import of the
EmojiPicker
!
By dynamically importing the
EmojiPicker
component, we managed to reduce the initial bundle size from
1.5MiB
to
1.33 MiB
! Although the user may still have to wait a while until the
EmojiPicker
has been fully loaded, we have improved the user experience by making sure the application is rendered and interactive while the user waits for the component to load.
我们无需将
EmojiPicker
加入初始打包文件,而是可以将它拆分到独立的打包文件中,从而减小初始打包体积!
更小的初始打包体积意味着更快的初始加载速度:用户不需要长时间盯着空白的加载页面。备用(fallback)组件可以让用户知道应用并未卡死,他们只需稍等片刻,模块就会处理完成并执行。
Asset                             Size         Chunks            Chunk Names
emoji-picker.bundle.js           1.48 KiB      1    [emitted]    emoji-picker
main.bundle.js                   1.33 MiB      main [emitted]    main
vendors~emoji-picker.bundle.js   171 KiB       2    [emitted]    vendors~emoji-picker
之前的初始打包体积是
1.5MiB
,通过延迟导入
EmojiPicker
,我们将其减小到了
1.33 MiB
通过动态导入
EmojiPicker
组件,我们成功将初始打包体积从
1.5MiB
降至
1.33 MiB
!虽然用户可能仍需等待片刻才能完全加载
EmojiPicker
,但我们通过确保应用在组件加载过程中保持可渲染和交互状态,优化了用户体验。

Loadable Components

Loadable Components

Server-side rendering doesn't support React Suspense (yet). A good alternative to React Suspense is the
loadable-components
library, which can be used in SSR applications.
Similar to React Suspense, we can pass the lazily imported module to the
loadable
, which will only import the module once the
EmojiPicker
module is being requested! While the module is being loaded, we can render a
fallback
component.
Although loadable components are a great alternative to React Suspense for SSR applications, they're also useful in CSR applications in order to suspend the import of modules.
服务端渲染(SSR)目前暂不支持 React Suspense。一个不错的替代方案是
loadable-components
库,它可用于 SSR 应用。
与 React Suspense 类似,我们可以将延迟导入的模块传入
loadable
,这样只有当
EmojiPicker
模块被请求时才会导入它!在模块加载过程中,我们可以渲染一个备用(fallback)组件。
尽管 Loadable Components 是 SSR 应用中 React Suspense 的优秀替代方案,但它在客户端渲染(CSR)应用中同样实用,可用于延迟导入模块。

Source

来源