Browser - Service Worker (Offline / Cache enhanced capability)
About
A service worker is a type of worker (JavaScript application) installed in the browser that can act as a forward proxy.
Two Ways
Browser > Server
They can:
- modify and create HTTP response
- before the requests reach the Internet
Server > Browser
They can receive http requests (and associated resources) created by the server
Usage
They make the following feature possible in the browser:
- cache (useful when the network is not available)
- push notifications,
- background syncing,
- offline functionality
Cache
This section explains the http cache feature.
The first time a user visits your page using a browser that supports service worker, all of the resources needed to use the page offline can be automatically cached locally, and each subsequent visit to any page on the site will be:
- a) fast (since there's no network dependency)
- b) work offline (for the same reason).
Management
The service worker needs to be registered (associated with) against:
Tool
Workbox
sw-toolbox and sw-precache are deprecated in favor of Workbox.
Test
- https://github.com/puppeteer/examples/blob/master/verify_sw_caching.js - Verify all the resources you expect are being cached by a service worker for offline.
Documentation / Reference
- Doc: Service Workers
- or Service Workers,