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

Preface

The service worker feature of the browser will enable you to build highly available and performant native web applications that seamlessly integrate with third-party APIs. Whether you want to create an offline web app or a proxy, this book will show you how to do it.

What this book covers

Chapter 1, Learning Service Worker Basics, covers setting up the service worker in your environment, and how to get up and running using service worker development. This chapter includes registering a service worker and debugging.

Chapter 2, Working with Resource Files, provides several recipes on how to handle resource files with the service worker, including loading CSS and fonts.

Chapter 3, Accessing Offline Content, takes a look at how to cache resources and serve content offline.

Chapter 4, Accessing Offline Content with Advanced Techniques, explores advanced techniques, including templating and Google Analytics, when working with offline content.

Chapter 5, Reaching Beyond the Offline Cache, provides recipes beyond the offline cache and explores topics ranging from getting network responses offline to how to use the service worker as a load balancer.

Chapter 6, Working with Advanced Libraries, talks about Google Analytics, circuit breakers, and dead letter queues.

Chapter 7, Fetching Resources, covers various techniques on fetching resources from different sources.

Chapter 8, Experimenting with Web Push, talks about different ways of implementing push notifications.

Chapter 9, Looking at General Usage, provides various recipes on general usages of the service worker from slow responses to a live flowchart.

Chapter 10, Improving Performance, talks about how to optimize your service worker application to perform efficiently and in a performant manner.

What you need for this book

This book was written using a Mac with Google Chrome as the browser, running Node.js. However, Node.js can be run on a Windows or Linux machine as well, along with Google Chrome.

All the software used in this book is free and open source. You will definitely need to be running Node.js and Google Chrome for most of the recipes.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The skipWaiting() method is used inside the active event handler, which in turn uses Clients.claim()."

A block of code is set as follows:

self.oninstall = function(event) {
  event.waitUntil(
    fetch(zipURL)
      .then(function(res) {
        return res.arrayBuffer();
      })
      .then(getZipFileReader)
      .then(cacheFileContents)
      .then(self.skipWaiting.bind(self))
  );
};

Any command-line input or output is written as follows:

$ git add –all
$ git commit -m "initial commit"
$ git push -u origin master

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes, for example, appear in the text like this: "Finally, in the sidebar on the left-hand side, select Credentials."

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Service-Worker-Development-Cookbook. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.