When I first heard about these terms I thought, okay they are doing around the same things with their separate thread. Then Why we need these 2 terms?
But to tell you the truth there are huge differences between these 2 terms and how they behave.
Will try to explain in detail.
Common ground Between these 2 is
Web Worker
Service Worker
Life Cycle of Service Worker
1. Registration
if ('serviceWorker' in navigator) { // wrap it in try/catch / promisses await navigator.serviceWorker.register('/service-worker.js') }
2. Installation
the below code we need to write it in service-worker.js
self.addEventListener('install', (event) => { // do your operations })
3. Activation
self.addEventListener('activate', (event) => { // Do your Operation })
4. Idle
5. fetch/Message
self.addEventListener('fetch', (event) => { // Do your Opeation })
6. Termination
It will keep the service workers for very long time.
Example:-
in chrome Open this link there you will see lots of service worker hanging, and you can do lot of stuff like, Inspecting/starting and sending a message.
chrome://serviceworker-internals/
How we can Wake Up Service workers even the Browser is closesed.
Note:-
For this specific we can use push to wake up, but for this use must give Notification permission to the Browser, else there is no way.
Other ways are relevant when the browser is still open
1. fetch Event
self.addEventListener('fetch', event => { // Handle fetch event });
2. Message
self.addEventListener('message', (event) => { // Handle message Event })
3. Push
self.addEventListener('push', (event) => { // Handle Push Event })
4. Sync Event
self.addEventListener('sync', (event) => { // handle background Sync Event })
Reference
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3