For optimizing front-end loading performance, many people use http-cache, asynchronous loading, 304 status codes, file compression, CDN and other methods to solve the problem. In fact, in addition to these methods, there is one more powerful than all of them, and that is Service Worker.
We can use Google Chrome team’s Workbox to implement Service Worker for rapid development.
Registering a Service Worker
Add the following to the page to register a Service Worker.
|
|
Of course, you need to have a Service Worker code /sw.js
before you can do that.
You can write the following code in this file to check if the Service Worker is successfully registered.
If you think Google’s CDN is not very reliable, you can use workbox-cli
to store the resources locally.
In this case, you need to replace the content written above at the beginning of sw.js
with the following
Workbox Policy
Stale While Revalidate
This policy will respond to network requests using a cache (if available) and update the cache in the background. If it is not cached, it will wait for a network response and use it. This is a fairly safe policy because it means that the user will update its cache periodically. The disadvantage of this policy is that it always requests resources from the network, which is more wasteful of the user’s bandwidth.
Network First
This policy will try to get a response from the network first. If a response is received, it will pass it to the browser and save it to the cache. If the network request fails, the last cached response will be used.
|
|
Cache First
This policy will first check to see if there is a response in the cache and if so, use the policy. If the request is not in the cache, the network will be used and any valid response will be added to the cache before being passed to the browser.
|
|
Network Only
|
|
Forced to let the response come from the network.
Cache Only
Force the response to come from the cache.
|
|
Policy Configuration
You can customize the behavior of the route by defining the plugins to be used.
Custom Policies in Workbox
In some cases, you may want to use your own alternative policy to respond to requests, or just generate requests in the Service Worker via a template.
To do this, you can provide a function handler
that returns a Response
object asynchronously.
Note that if a value is returned in a match
callback, it passes handler
as a params
parameter to the callback.
|
|
It may be useful for handler
if some information in the URL can be parsed once in the match callback and used in it.
Workbox Practices
For most projects that use Workbox, you will usually introduce the appropriate gulp or webpack plugin, register the Service Worker in the build process, Precache the specified URL, generate sw.js, and so on.
But for static site generators like Hexo, Jekyll, or CMSs like WordPress or Typecho, if you don’t install the corresponding plugins, you need to write a sw.js
from scratch.
Let’s write the general configuration first.
Google Fonts
Google Fonts uses two main domains: fonts.googleapis.com
and fonts.gstatic.com
, so only caching is required when these two domains are matched.
|
|
jsDelivr CDN
When using the jsDelivr CDN, if you specify the version of the library, the corresponding files can be said to be permanently unchanged, so use CacheFirst
for caching.
|
|
Google Analytics
Workbox has a Google Analytics Offline Statistics Plugin, but unfortunately I use the Sukka-written Unofficial Google Analytics Implementation, so I can only add a NetworkOnly
to drop offline stats.
|
|
Pictures
I am a LifeTime Premium VIP of SM.MS, so of course the pictures should be saved here~
MS has these image domains: i.loli.net
, vip1.loli.net
, vip2.loli.net
, s1.baoshuo.ren
, s1.baoshuo.ren
, just write a regular match.
Since the files corresponding to the image links, like jsDelivr, are also almost never changed, use CacheFirst
to cache them.
|
|
Links
These files are only updated occasionally, use StaleWhileRevalidate
to balance speed and version updates.
|
|
Disqus Comments
DisqusJS determines the availability of Disqus for visitors by checking shortname.disqus.com/favicon.
and disqus.com/favicon.
, which obviously cannot be cached.
The API can use NetworkFirst
when there is no network to achieve the effect of viewing comments even when there is no network.
Also, Disqus itself does not need to cache, so using NetworkOnly
for *.disqus.com
is fine.
However, the avatars, JS and CSS under *.disquscdn.com
can be cached for some time, so use CacheFirst
to cache them for 10 days.
|
|
Suffix Matching
For the rest of the static files that are not matched by the domain name, match by file suffix and use StaleWhileRevalidate
to balance speed and version updates.
Default behavior
Use Workbox’s defaultHandler to match the rest of the requests (including the page itself), always using NetworkFirst
to speed up and take offline with Workbox’s runtimeCache
.
Article cover image from: https://developers.google.com/web/tools/workbox
The image in the Workbox Strategies section is from: https://web.dev/offline-cookbook/