Service worker

from Wikipedia, the free encyclopedia

Service worker is a modern browser technology that uses JavaScript to provide a proxy between the web browser and the server that can be used in a variety of ways. The best-known use is to make certain pages available offline . Service workers also provide the basis for push notifications , progressive web apps, and other uses.

Basics

From a technical point of view, a service worker is a special web worker , so it offers a way to run JavaScript independently of the main thread . The special feature of the service worker is that he can take over certain services for the pages assigned to him and can also be active if the user has not accessed any of these pages.

To prevent misuse as man-in-the-middle , service workers are only allowed within a secure context, i.e. usually only via HTTPS .

For use, the service worker is first registered via the JavaScript function navigator.serviceWorker.register. You can specify whether the service worker should apply to all pages of the current domain or only to those that are located in a certain path . The browser will then download and initialize the specified script file. As soon as this has happened, the service worker will be available for all pages assigned to him in the future. Pages that are already open in the browser must first be reloaded before they can be checked by the newly set up service worker.

Regardless of the specified cache settings , the browser will download the service worker again once every 24 hours, provided there is an Internet connection. This is to avoid that a faulty worker remains permanently in use, even if the information about the cache was chosen unfavorably. If there is a new version, it is initialized and is available for all newly opened pages.

Purposes

Offline use

The best-known purpose is the offline use of websites. Here the service worker replaces the application cache .

To make a website usable offline with the help of a service worker, proceed as follows in the simplest case:

When the worker is initialized (the only time an internet connection is required), all required resources are downloaded and saved. In principle, any available memory can be used for this, such as the Indexed Database API , but usually a special cache memory is used that is independent of the normal browser cache , but how it saves resources based on their URL .

Then must the service worker only fetch- Events react. This special event is triggered whenever a page monitored by the worker requests a new resource. This can occur through the navigation of the user, through the fact that a called up page includes additional elements such as images or style sheets, or because data is to be loaded dynamically using AJAX . If the requested resource is available in the cache of the service worker, he can simply hand it over; an internet connection is not necessary.

Push notifications

Another area of ​​application for service workers are push notifications , i.e. notifications sent by the server that should reach the user even if the user has not currently opened a corresponding page in the browser.

To do this, the push notification is first activated via the push manager belonging to the service worker. The server that is to send the push notifications is given a URL to which it should send the notifications. The server is operated by the respective browser manufacturer and acts as a proxy between the service and the user.

The service worker is then pushinformed via events when there is a new message, even if no page is currently open in the browser that it is monitoring. In response to this event, the worker should generate a visible notification and, depending on the user's wishes, load a page with further information in a new browser tab or bring an already open page to the foreground.

Other possible uses

A service worker can be used in a variety of ways, especially in his function as a proxy. In addition to more complex cache strategies than pure offline storage, load balancers can be implemented, for example .

Browser support

Apart from Internet Explorer , all common browsers support service workers (as of June 2018); Firefox (from version 44), Google Chrome (from version 40), Microsoft Edge (from version 17), Apple Safari (from version 11.1) and Opera (from version 27). In addition to the scope of support, the main differences between the browsers are the debugging tools for service workers.

Web links

Individual evidence

  1. ^ Service Workers. Motivation. W3C Draft June 25, 2015.
  2. ^ Salva: Beyond Offline. In: Mozilla Hacks. December 21, 2015, accessed March 30, 2017 .
  3. Can I use: Service Workers. Retrieved March 30, 2017 (English).