GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing developer.mozilla.org — Caching - Progressive web apps | MDN CachingWhen a user opens and interacts with a website, all the resources that the website needs, including the HTML, JavaScript, CSS, images, fonts, as well as any data explicitly requested by the app, are retrieved by making HTTP(S) requests. One of the most fundamental features of a PWA is the ability to explicitly cache some of the app's resources on the device, meaning that they can be retrieved without needing to send a request to the network. There are two main benefits to caching resources locally: offline operation and responsiveness.
The main drawback, of course, is freshness: caching is less appropriate for resources which need to be up to date. Also, for some types of requests, such as POST requests, caching is never appropriate. This means that whether and when you should cache a resource is very dependent on the resource in question, and a PWA will typically adopt different strategies for different resources. In this guide we'll look at some common caching strategies for PWAs, and see which strategies make sense for which resources. Caching technology overviewThe main technologies on which a PWA can build a caching strategy are the Fetch API, the Service Worker API, and the Cache API. Fetch APIThe Fetch API defines a global function The Service Worker APIA service worker is a part of a PWA: it's a separate script that runs in its own thread, separate from the app's main thread. Once the service worker is active, then whenever the app requests a network resource controlled by the service worker, the browser fires an event called By listening for the Cache APIThe Most commonly, the service worker will add resources to the cache in its When to cache resourcesA PWA can cache resources at any time, but in practice there are a few times when most PWAs will choose to cache them:
Caching strategiesA caching strategy is an algorithm for when to cache a resource, when to serve a cached resource, and when to get the resource from the network. In this section we'll summarize some common strategies. This isn't an exhaustive list: it's just intended to illustrate the kinds of approaches a PWA can take. A caching strategy balances offline operation, responsiveness, and freshness. Different resources have different requirements here: for example, the app's basic UI is likely to be relatively static, while it may be essential to have fresh data when displaying a product listing. This means that a PWA will typically adopt different strategies for different resources, and a single PWA might use all the strategies described here. Cache firstIn this strategy we will precache some resources, and then implement a "cache first" strategy only for those resources. That is:
Precaching is an appropriate strategy for resources that the PWA is certain to need, that will not change for this version of the app, and that need to be fetched as quickly as possible. That includes, for example, the basic user interface of the app. If this is precached, then the app's UI can be rendered on launch without needing any network requests. First, the service worker precaches static resources in its js In the The js We return the resource by calling the event's When we add You might wonder why we fall back to the network for precached resources. If they are precached, can't we be sure they will be in the cache? The reason is that it is possible for the cache to be cleared, either by the browser or by the user. Although this is unlikely, it would make the PWA unusable unless it can fall back to the network. See Deleting cached data. Cache first with cache refreshThe drawback of "cache first" is that once a response is in the cache, it is never refreshed until a new version of the service worker is installed. The "cache first with cache refresh" strategy, also known as "stale while revalidate", is similar to "cache first", except that we always send the request to the network, even after a cache hit, and use the response to refresh the cache. This means we get the responsiveness of "cache first", but get a fairly fresh response (as long as the request is made reasonably often). This is a good choice when responsiveness is important, and freshness is somewhat important but not essential. In this version we implement "cache first with cache refresh" for all resources except JSON. js Note that we update the cache asynchronously (in a Network firstThe last strategy we'll look at, "network first", is the inverse of cache first: we try to retrieve the resource from the network. If the network request succeeds, we return the response and update the cache. If it fails, we try the cache. This is useful for requests for which it is important to get the most fresh response possible, but for which a cached resource is better than nothing. A messaging app's list of recent messages might fall into this category. In the example below we use "network first" for requests to fetch all resources located under the app's "inbox" path. js There are still requests for which no response is better than a possibly-outdated response, and for which only a "network only" strategy is appropriate. For example, if an app is showing the list of available products, it will be frustrating to users if the list is out of date. Deleting cached dataCaches have a limited amount of storage space, and the browser may evict an app's cached data if the limit is exceeded. The specific limits and behavior are browser-specific: see Storage quotas and eviction criteria for details. In practice, eviction of cached data is a very rare event. The user can also clear an app's cache at any time. A PWA should clean up any old versions of its cache in the service worker's See also
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |