Reputation: 4649
So far as I know, one can even cache a web page. According to this documentation: https://www.mnot.net/cache_docs/#BROWSER
, representation can be cached in a browser cache. I see that even a service worker does the exact same thing (HTML, CSS and JavaScript files). What is the purpose of service workers?
Upvotes: 11
Views: 6714
Reputation: 468
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
Upvotes: 7
Reputation: 601
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.
Upvotes: 8