Book Image

Service Worker Development Cookbook

By : Sean Amarasinghe
Book Image

Service Worker Development Cookbook

By: Sean Amarasinghe

Overview of this book

It would be nice to have web apps that work offline and send push notifications. This is now possible with Service Workers, which can add native-like functionality to your web apps without requiring a download. This book will get your mobile and web apps functioning without Internet connectivity, improve performance and network interaction in order to increase the level of availability, and show you how to build performant applications that seamlessly integrate with third-party APIs. We’ll show you how to add Service Worker functionality to web apps and sites, access offline content through basic and advanced techniques, and build powerful interactive system notifications. We’ll also teach you about cache functionality and assets to provide immediate load even over narrow connections. We conclude by giving you various tips to improve app performance, including the background sync technique. By the end of this book, you’ll know build high performing and faster web and mobile applications with Service Workers.
Table of Contents (17 chapters)
Service Worker Development Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

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.