Route Based

patternsdev/skills/javascript/route-based

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

Teaches route-based code splitting for single-page applications. Use when different routes load distinct feature sets and you want to avoid loading all route code upfront.

AI 產生的概覽

說明單頁應用程式以路由為基礎的程式碼分割,依路由延遲載入元件以減少初始載入量。

功能
此技能提供單頁應用程式依路由進行程式碼分割的指引。它說明如何將 React Suspense 或 loadable-components 與 react-router 等路由函式庫搭配,依路由延遲載入頁面層級元件。它也指出 Next.js、Remix 與 React Router 等框架已內建支援,並說明在自訂設定中使用 React.lazy() 搭配 Suspense,或依路由設定打包進入點。
適用情境
當應用程式有多個路由,且並非每個頁面都需要全部程式碼時使用。其目的是只請求目前路由所需的程式碼包,以縮短初始載入時間。
執行需求
未包含任何指令碼或工具,僅為說明性指引。實踐此指引需要 JavaScript 或 TypeScript 單頁應用程式、路由函式庫,以及可選的 React Suspense、loadable-components 或依路由設定進入點的打包工具。

Route Based Splitting

We can request resources that are only needed for specific routes, by adding route-based splitting. By combining React Suspense or loadable-components with libraries such as react-router, we can dynamically load components based on the current route.

By lazily loading the components per route, we're only requesting the bundle that contains the code that's necessary for the current route. Since most people are used to the fact that there may be some loading time during a redirect, it's the perfect place to lazily load components!

When to Use

  • Use this when your application has multiple routes and not all code is needed on every page
  • This is helpful for reducing initial load time by only loading code for the current route

Instructions

  • Combine React Suspense or loadable-components with routing libraries like react-router
  • Lazily load page-level components per route for optimal code splitting
  • Take advantage of natural loading pauses during route transitions for a seamless experience

Details

By lazily loading the components per route, only the bundle containing code necessary for the current route is requested. Since users expect some loading time during navigation, route transitions are a natural place to introduce code splitting without degrading the experience.

Most modern frameworks (Next.js, Remix, React Router) support route-based splitting out of the box. In a custom setup, use React.lazy() with Suspense to wrap route-level components, or configure your bundler's entry points per route.

Source

來源與署名

來源:patternsdev/skills位於javascript/route-based提交48bf58a

授權條款: MIT

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

檢舉或申請下架