前端效能優化
Caching
Workbox
Strongly recommend to read all the references in the ariticle before you implement service worker caching in your project.
https://web.dev/articles/service-worker-caching-and-http-caching
Features | HTTP Caching | SW Cahcing |
---|---|---|
HTTP Caching 只有「有跟沒有」 | 只有 Cache Only, Network Only | 還有 Cache First, Network First, Stale While Revalidaiont |
HTTP Caching 只有在有真實的 request 發生時才會作用 | Offline feature NOT available | Offline feature available |
SW Caching 後才是 HTTP Caching | SW Caching 準行的 request 可能是 HTTP Caching 的結果,這也是為什麼一起設定 SW Caching、HTTP Caching,可以有更彈性的 Caching 機制 | |
SW Caching 是透過 JavaScript 運行的 | 無法解析 Opaque response |
Example
Strategy | Service worker cache TTL | HTTP cache max-age |
---|---|---|
Network First | 1 day | 10 mins |
When a cached resource is valid in the service worker cache (<= 1 day): The service worker goes to the network for the resource. The browser returns the resource from the HTTP cache if it's there (updated every 10 mins). If the network is down, the service worker returns the resource from the service worker cache
When a cached resource is expired in the service worker cache (> 1 day): The service worker goes to the network to fetch the resource. The browser fetches the resources over the network as the cached version in its HTTP cache is expired.
Pros and cons
Pro
Con
https://developer.chrome.com/docs/workbox/caching-strategies-overview
CacheStorage.open
to create a new Cache instance.Cache.add
and Cache.put
to store network responses in a service worker cache.Cache.match
to locate a cached response in a Cache instance.Cache.delete
to remove a cached response from a Cache instance.Notice the important API methods just mentioned.
https://developer.chrome.com/docs/workbox/caching-strategies-overview#cache_only
https://developer.chrome.com/docs/workbox/caching-strategies-overview#network_only
https://developer.chrome.com/docs/workbox/caching-strategies-overview#stale-while-revalidate
https://developer.chrome.com/docs/workbox/service-worker-overview#precaching_and_runtime_caching
https://gist.github.com/jeffposnick/fc761c06856fa10dbf93e62ce7c4bd57
https://developer.chrome.com/docs/workbox/modules/workbox-window
為什麼 workbox-window 沒有 unregister()
logic?
https://stackoverflow.com/questions/46424367/how-to-unregister-and-remove-old-service-worker
退一步想為什麼要 unregister()
?
workbox-window
deal with the updatation logic for you
當你要完全移除 SW,你需要在 main.js
中 explicitly write something like
More about Handling service worker updates
https://developer.chrome.com/docs/workbox/caching-resources-during-runtime
workbox-routing 提供 registerRoute()
包含兩個參數
Route
Route Handler
new CacheOnly()
new CacheFirst()
new NetworkOnly()
new NetworkFirst()
new StaleWhileRevalidate()
cacheName
:Separate Cache instances using the cacheName option
plugins
new ExpirationPlugin()
:Setting an expiry for cache entriesnew CacheableResponsePlugin()
:Deal with opaque responsesOpaque responses
https://developer.chrome.com/docs/workbox/caching-resources-during-runtime#opaque_responses
crossorigin
as anonymous
https://developer.chrome.com/docs/workbox/modules/workbox-precaching
workbox-precaching 將各種情境及會遇到的 edge cases 封裝成可複用的函式,降低 developer 的使用門檻。
Precaching 顧名思義需要
如何在資源更新時,更新 SW?
workbox-precaching 提供 precacheAndRoute()
目前大多專案會使用 Webpack 等 bundler,build 出來的 file name 是自動產生的,難道我每次 build 時都要手動更改 SW script 嗎?
大多數的使用情境很單純,是不是可以有自動創建 SW script 的工具呢?
workbox-build
提供 injectManifest()
及 generateSW()
https://developer.chrome.com/docs/workbox/modules/workbox-build
injectManifest()
generateSW()
runtimeCaching
來實踐 Running Cache
https://developer.chrome.com/docs/workbox/modules/workbox-recipes
A number of common patterns, especially around routing and caching, are common enough that they can be standardized into reusable recipes.
Example: Page cache
https://developer.chrome.com/docs/workbox/modules/workbox-recipes#page_cache