Region: Global
Language: EN
Recent additions:
Discover pronunciations:
Service workers
Dec 9, 2018
Front end

Service workers

Listen to this post

Service workers allow developers to build web applications that can run offline and support push messaging and background data synchronization.

A service worker is a JavaScript execution context that acts as a target for the fetch, sync, push, and other functional events. It remains active even when the user is no longer browsing the website, the web browser is closed, or there is no network access.

Service workers allow web applications to intercept individual network requests, including those to the URLs that are used to open the app itself. This is mediated by the fetch events that are dispatched to the service worker and let the app generate custom network responses. The application can choose to return resources that have been previously cached and thereby allow itself to load in the absence of network connectivity.

The background sync technology allows web apps to register deferred background syncs when there is no network access. When the device is back online, a sync event is dispatched to the service worker for each pending background sync, allowing the application to use the network to finish its tasks.

Push communication enables application back ends to send messages to the clients that are subscribed to push endpoints. This form of messaging involves the push events getting dispatched to the service worker that can use the information in the push messages to display notifications to the user or perform other tasks.

There are a number of service worker libraries that abstract away common tasks and integrate with build processes. The most commonly used one is Google's workbox, the successor to sw-precache and sw-toolbox.

Have feedback on this post? Let @cofactor_io know.
Need help? Contact us.
Auckland · New Zealand
© 2019 Cofactor Ltd.
Cofactor