Dynamic Import

patternsdev/skills/javascript/dynamic-import

作者 patternsdev48bf58a488cdMIT253 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫5 個月前更新

Teaches dynamic import() for on-demand code loading. Use when you need to reduce initial bundle size by lazily loading modules that aren't required at startup.

AI 產生的概覽

說明如何用動態 import() 依需求載入 JavaScript 模組,以縮小初始打包體積。

功能
這個技能說明如何使用動態 import(),讓模組依需求載入,而不是在啟動時全部載入。它以 React 為例,說明如何把表情符號選擇器等元件拆分成獨立打包檔,並介紹 React.lazy 搭配 Suspense、載入期間的備援介面,以及用於伺服器端渲染的 loadable-components。產出的是說明性指引與打包體積分析,而不是可執行程式碼或檔案。
適用情境
當某些模組只在使用者互動後或特定條件下才需要時使用。適合模態視窗、選擇器或大型函式庫在首次渲染時並不需要,且希望縮小初始打包體積的情境。也適用於不支援 React Suspense 的伺服器端渲染應用程式。
執行需求
不需要指令稿或特殊工具,僅為說明性內容。內容假設使用 JavaScript 或 TypeScript 專案,並提到 React、React.lazy、Suspense 以及 loadable-components 函式庫作為可選方案。

Dynamic Import

In a 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!

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

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 Namesemoji-picker.bundle.js           1.48 KiB      1    [emitted]    emoji-pickermain.bundle.js                   1.33 MiB      main [emitted]    mainvendors~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.

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.

Source

來源與署名

來源:patternsdev/skills位於javascript/dynamic-import提交48bf58a

授權條款: MIT

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架