Prefetch

patternsdev/skills/javascript/prefetch

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

Teaches resource prefetching strategies for faster navigation. Use when you can predict which resources the user will need next and want to load them during idle time.

AI 產生的概覽

說明瀏覽器資源預取策略,以縮短後續導覽的感知載入時間。

功能
這個技能說明如何預取使用者可能很快就會需要的資源,方式包括 link rel=prefetch、HTTP Link 標頭、Service Worker 或 Webpack 魔法註解。內容涵蓋預取適用的情境、應避免的情況,以及預取的 bundle 在建置輸出與文件 head 中的呈現方式。它產出的是指引說明,而不是檔案或程式碼成品。
適用情境
當你能預測使用者接下來會前往哪些路由或需要哪些資源,並希望在瀏覽器閒置時提前載入時使用。它也適合用來判斷在頻寬有限、計量連線或資源經常變動的情況下是否適合預取。
執行需求
不需要任何工具、套件或憑證;它僅為說明性內容,不附帶指令碼。參考其中的 Webpack 範例需要 Webpack 建置環境,瀏覽器範例則需要網頁環境。

Prefetch

Prefetch (<link rel="prefetch">) is a browser optimization which allows us to fetch resources that may be needed for subsequent routes or pages before they are needed. Prefetching can be achieved in a few ways. It can be done declaratively in HTML (such as in the example below), via a HTTP Header (Link: </js/chat-widget.js>; rel=prefetch), Service Workers or via more custom means such as through Webpack.

When to Use

  • Use this when you know users will likely navigate to certain routes or need certain resources soon
  • This is helpful for reducing perceived loading time on subsequent navigations

When NOT to Use

  • For resources unlikely to be needed — unnecessary prefetching wastes bandwidth and competes with critical requests
  • On low-bandwidth or metered connections where prefetching consumes the user's data budget
  • When the prefetched resources change frequently and would be stale by the time they're used

Instructions

  • Use <link rel="prefetch"> or Webpack magic comments (/* webpackPrefetch: true */) to prefetch resources
  • Only prefetch resources that are likely to be needed — don't overdo it as it consumes bandwidth
  • Prefetched resources are loaded at low priority when the browser is idle

Details

html
<link rel="prefetch" href="/pages/next-page.html" /><link rel="prefetch" href="/js/emoji-picker.js" />

Prefetch

In the examples showing how we can import modules based on visibility or interaction, we saw that there was often some delay between clicking on the button in order to toggle the component, and showing the actual component on the screen. This happened, since the module still had to get requested and loaded when the user clicked on the button!

In many cases, we know that users will request certain resources soon after the initial render of a page. Although they may not visible instantly, thus shouldn't be included in the initial bundle, it would be great to reduce the loading time as much as possible to give a better user experience!

Components or resources that we know are likely to be used at some point in the application can be prefetched. We can let Webpack know that certain bundles need to be prefetched, by adding a magic comment to the import statement: /* webpackPrefetch: true */.

js
const EmojiPicker = import(/* webpackPrefetch: true */ "./EmojiPicker");

After building the application, we can see that the EmojiPicker will be prefetched.

 Asset                             Size       Chunks                          Chunk Names    emoji-picker.bundle.js         1.49 KiB   emoji-picker [emitted]          emoji-picker    vendors~emoji-picker.bundle.js 171 KiB    vendors~emoji-picker [emitted]  vendors~emoji-picker    main.bundle.js                 1.34 MiB   main  [emitted]                 main
Entrypoint main = main.bundle.js(prefetch: vendors~emoji-picker.bundle.js emoji-picker.bundle.js)

The actual output is visible as a link tag with rel="prefetch" in the head of our document.

html
<link rel="prefetch" href="emoji-picker.bundle.js" as="script" /><link rel="prefetch" href="vendors~emoji-picker.bundle.js" as="script" />

Modules that are prefetched are requested and loaded by the browser even before the user requested the resource. When the browser is idle and calculates that it's got enough bandwidth, it will make a request in order to load the resource, and cache it. Having the resource cached can reduce the loading time significantly, as we don't have to wait for the request to finish after the user has clicked the button. It can simply get the loaded resource from cache.

Although prefetching is a great way to optimize the loading time, don't overdo it. If the user ended up never requesting the EmojiPicker component, we unnecessarily loaded the resource. This could potentially cost a user money, or slow down the application. Only prefetch the necessary resources.

Source

References

來源與署名

來源:patternsdev/skills位於javascript/prefetch提交48bf58a

授權條款: MIT

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

檢舉或申請下架