관리 메뉴

Dev Blog

Service worker 본문

Web Development/General Tech Knowledges

Service worker

Nomad Kim 2021. 9. 3. 08:17

What is Service Worker:
A service worker is a script that runs independently in the browser background. On the user side, it can intercept its network requests and decide what to load (fetch).
Service workers mainly serve features like background sync, push notifications and they are commonly used for’offline first’ applications, giving the developers the opportunity to take complete control over the user experience.

 

The service worker life cycle:
The service worker lifecycle is completely separate from the web page. It’s a programmable network proxy, which is terminated when it’s not used and restarted when it’s next needed. Service Workers heavily rely on the use of Javascript Promises.

 

During installation, the service worker can cache some static assets like web pages. If the browser cache the files successfully, the service worker gets installed.

Afterward, the worker needs to be activated. During activation the service worker can manage and decide what to happen to the old caches, typically they are being deleted and replaced with the new versions.

 

Lastly, after activation, the service worker takes control over all pages in his scope, without the page which initially registered the service worker, which needs to be refreshed. The service worker is smart in terms of memory usage and will be terminated if there is nothing to fetch and there are no message events occurring.

 

Service Worker can’t:

  • Access the Parent Object
  • Access the Window Object
  • Access the Document Object
  • Access the DOM

However, the Service Worker can:

  • Cache Assets & API calls
  • Manage Push Notifications
  • Control the Network Traffic
  • Store the Application Cache

Common use cases:

  • Offline-optimized experience
  • Sending Push Notifications
  • Background sync

 

Source from: https://www.geeksforgeeks.org/service-workers-in-javascript/

'Web Development > General Tech Knowledges' 카테고리의 다른 글

dependencies version in package.json  (0) 2021.10.16
What is JSON File ?  (0) 2021.09.09
CDN  (0) 2021.08.01
PWA  (0) 2021.08.01
SPA - CSR/SSR/SSG  (0) 2021.08.01
Comments