Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of service worker?

From what I'm seeing you can even cache a web page. According to this documentation: https://www.mnot.net/cache_docs/#BROWSER, representation can be cached in browser cache. I see the that even serviceworker does the exact same thing(html, css and javascript files). What is the purpose of service worker?

like image 957
Deke Avatar asked Aug 28 '16 00:08

Deke


People also ask

What is a service worker used for?

Service workers are specialized JavaScript assets that act as proxies between web browsers and web servers. They aim to improve reliability by providing offline access, as well as boost page performance.

How do you define a service worker?

Related Definitions Service workers means individuals in positions that include food service, cleaning service, personal service, and protective service activities. Skill may be acquired through formal training, job-related training or direct experi- ence.

What is service worker scope?

The scope is defined during the registration of the Service Worker - the scope specifies from which directory the requests of the Progressive Web App are controlled. The scope of the Services Worker is defined during registration and can only be below the respective path.

How do service workers improve performance?

One of the biggest benefits of service workers is their ability to support offline experiences. In the past, the AppCache API could be used to provide limited offline support, but service workers have now made AppCache obsolete. Loading locally cached data always takes less time than retrieving data from the web.


2 Answers

A service worker is not just a script to cache a web page or scripts, it is actually a script that handles your background operations separately from the webpage, functionalities include but not limited to : caching resources, background sync, push notifications... Their main advantage is that they can be woke up, even if the browser is closed and the website isnt open.

it would be helpful to check here for basics.

like image 160
jaafar Nasrallah Avatar answered Sep 21 '22 17:09

jaafar Nasrallah


A service worker is a specific type of JS Script, which runs in the background of the user’s browser. It acts like a proxy server exists between your app, the browser and the network. Among other things, service workers allow apps to continue functioning offline in case the user loses internet connection.

Improve performance of the website: Service worker helps the website to load offline. Unlike AppCache API, it is having more control over the browser cache. Returning visitors can fetch the website instantly from the browser cache and get a smoother experience so that they’ll keep coming back.

How Do Service Workers Work?

Service worker scripts is independent and have their own life cycle: The script first registered and then installed by the browser. After that it start caching static assets right away. If any errors occur, then installation will fail, and you must try again. For successful installation, the service worker will activate and gain control over all pages under its scope. Active service worker alternate between two states: either it will handle fetch and message events, or it will terminate to save memory.

Service Worker Caching: Service worker caching is much more manageable. It always checks network status whether it is online or offline and fetch or terminate the resource files accordingly. This means the site can work when there is no network, offline, or poor network connectivity, low bars or false cellular connectivity. Supported Browsers Browser options are growing. Service workers are supported by Chrome, Firefox and Opera. Microsoft Edge is now showing public support. Even Safari has dropped hints of future development.

Restrictions and Security:

• Service Worker can’t access the window object. That’s why it does not have any threat for spreading viruses.

• Require HTTPS. This ensures a level of security to allow the service worker to do the things it is designed to do.

• Only Asynchronous. This means synchronous APIs like XHR and local Storage are not available to a service worker.

Conclusions: Service workers transform your website to an instant loading powerhouse. Apart from improving web performance, they can be used to implement push notifications to keep users engaged with mobile apps. Future applications for service workers might include things like Geofencing and Periodic sync. Visit my video blog for more details: https://www.youtube.com/watch?v=nKHDUUvrkeg

like image 37
Tricks By Sam Avatar answered Sep 18 '22 17:09

Tricks By Sam