Written by Muriel Santoni on
How does a PWA work?
- The App Shell
- Service Worker
- JSON manifest
What is a service worker?
1) Definition and how it works
From a technical standpoint, a service worker is a script a browser runs in the background. It is capable of executing actions in response to events such as network requests, or information transmitted by the operating system (connectivity changes for example). It allows developers to build advanced features, such as push notification or cache management. Once the service worker is installed in the browser, it independently performs the tasks it's been assigned, even if the user is not browsing the site. The way a service worker functions is similar to a proxy server; it serves as an intermediary between the browser and the Internet in order to improve user experience, particularly by caching a web page's data.
There are 2 pre-requisites for a service worker to be installed in a browser :
- The first is the usage of https protocol to serve web pages. Service workers can perform very powerful operations from the user's browser. Using https protocol guarantees that there is no risk that the service worker installed on the browser has been tampered with. If the page is served in http, the service worker won't be installed.
- The second is the usage of a browser that supports service workers. Currently, service workers are supported by the following browsers : Google Chrome, Mozilla Firefox, Opera, and Microsoft Edge, depending on the operating system of the machine they're installed on.
Service worker developments are also in progress for the Edge browser%20worker , but in IE there are no developments to speak of.
What does a service worker bring to a PWA, exactly?
1) Background synchronisation
2) Offline accessibility
Before service workers appeared, web app offline functionality was managed by the Appcache interface, which allowed application files to be stored in a cache and to be used in the case of a lost connection.
Offline accessibility was therefore already possible through Appcache, but service workers largely simplified the implementation of this functionality. Appcache does have certain inconveniences; its lack of flexibility in particular.
Service workers were originally created like an improved version of Appcache, in order to allow web developers to provide their apps with optimal offline functionality.
In terms of functionality, service workers actually intercept network requests, then carry out the appropriate actions based on whether or not the network is available and if the updated resources are available on the server.
Here is an example of a caching strategy that developers can implement via service workers : Cache then Network .
3) Sending push notifications
The ability to use push notifications via web apps is a true revolution which bridges the gap between native and web apps even more.
This functionality is made possible by service workers' ability to work in the background. Two technologies contribute to this possibility : the Push API and the Notification API, which are different yet complementary. The Push API notifies the service worker of a notification's presence. The Notification API triggers the display of the notification in the browser. These two API's must therefore be used inside the service worker.
Push notifications aren't supported by Safari on mobile yet, but they do work on Safari desktop, not because of service workers but because of Apple's push notification service (APNs) which is a service created by Apple, allowing an external service provider to send messages to any Apple device using OS X. Note that notifications for websites do not appear on devices using iOS (mobile).
To sum it up, you can see the importance of service workers in the functionality of Progressive Web Apps, as a number of functionalities rely on their usage, leaving developers with a lot of freedom in building their apps. Recent news is leaving us hopeful and giving us reason to believe that service workers have a long life ahead of them, since Apple seems to be moving towards making Safari compatible with them, thus allowing its users to take full advantage of all that Progressive Web Apps have to offer.
As you can see, service workers and Progressive Web Apps are about to revolutionize the way we see the web, for both developers and users who are little by little discovering the potential of web apps, which turn out to be closer to native apps than you would think.