To install a service worker, first create a file for it (which is served over HTTPS). In the following example, this file is called service-worker.js. Then, inside a <script> tag on your HTML page (also served over HTTPS), add the following JavaScript code:
if ('serviceWorker' in navigator) { navigator.serviceWorker.register('service-worker.js', { scope: '/' }); }
The preceding code snippet first checks to see whether the service workers are supported, and if they are, it registers your worker. You can now fetch resources and add them to the cache. An interesting performance-enhancing use case for this is prefetching resources (that the user may need) ahead of time and putting them in the cache. Scope is an optional ...