Service Worker Development Cookbook
上QQ阅读APP看书,第一时间看更新

Setting up service workers

In order to get service workers running, we need to serve our code via Hyper Text Transfer Protocol Secure (HTTPS). Service workers are designed to run only across HTTPS for security reasons. Source code repositories such as GitHub support HTTPS, where you can host your files.

Getting ready

If you are using a newer version of a browser, the chances are that service workers are already enabled on it. But, if that is not the case, we will have to change few things in the browser settings. In the following section, we are going to cover how to enable service worker features in Chrome and Firefox.

How to do it...

Follow the steps below to enable service worker in Chrome and Firefox.

Chrome

To experiment in Chrome, you need to download Chrome Canary:

  1. Go to https://www.google.com/chrome/browser/canary.html and download the latest version.
  2. Open Chrome Canary and type chrome://flags.
  3. Turn on experimental-web-platform-features.
  4. Restart the browser.
  5. The following image shows experimental features on the Chrome browser, where you enable the Experimental Web Platform feature by clicking on the Enable link underneath:

Firefox

To experiment in Firefox, you need to download Firefox Nightly:

  1. Go to https://nightly.mozilla.org/ and download the latest version.
  2. Open Firefox Nightly, and go to about:config.
  3. Set experimental-web-platform-features to true.
  4. Restart the browser.

Note

At the time of writing, Opera offers partial support for service workers, including basic support, as well as installing and uninstalling events. Internet Explorer and Safari do not support service workers.

Service workers are currently an experimental technology, meaning the syntax and the behavior are subject to change in future versions as the specification changes.