Member-only story
Javascript — Service Workers
4 min readAug 24, 2022
What is a Service Worker?
A service worker is an event-driven worker registered against an origin and a path. It takes the form of a JavaScript file that can control the web-page/site that it is associated with.
A service worker is a JavaScript file that functions a bit like a proxy server. It catches outgoing network requests from your application, letting you create custom responses.
It can then decide if it should serve the resource from the cache via the Cache Storage API, from the network as normally would happen without a service worker, or create it from a local algorithm.
Points to note :-
- A service worker is run in a worker context: it therefore has no DOM access,
- runs on a different thread to the main JavaScript that powers your app, so it is non-blocking.
- It is designed to be fully async; as a consequence, APIs such as synchronous XHR and Web Storage can’t be used inside a service worker.
- Unlike previous attempts in this area such as AppCache, service workers don’t make assumptions about what you are trying to do, but then break when those assumptions are not exactly right. Instead, service workers give you much more granular control.
- Not all browsers support service workers. Even when present your service worker won’t be available on first load or while it’s waiting to activate. Therefore, treat it as optional and do not require…