Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use a service worker for caching when browser cache handles the caching?

I read that using a service worker for offline caching is similar to browser caching. If so, then why would you prefer a service worker for this caching? Browser caching will check if the file is modified or not and then serve from the cache, and with a service worker we are handling the same thing from our code. By default, the browser has that feature so why prefer a service worker?

like image 944
bvakiti Avatar asked Apr 28 '16 13:04

bvakiti


People also ask

Why do we need service worker?

Service workers are a fundamental part of a PWA. They enable fast loading (regardless of the network), offline access, push notifications, and other capabilities. Users expect apps to start on slow or flaky network connections, or even when offline.

Should you cache the service worker?

Caching unnecessary resources wastes bandwidth and storage space and could cause your app to serve unintended outdated resources. You don't need to cache all the assets at once, you can cache assets many times during the lifecycle of your PWA, such as: On installation of the service worker. After the first page load.

What is service worker cache?

A service worker intercepts network-type HTTP requests and uses a caching strategy to determine what resources should be returned to the browser.

Can service workers access cache?

Using a Service worker you can easily set an app up to use cached assets first, thus providing a default experience even when offline, before then getting more data from the network (commonly known as Offline First).


1 Answers

Service Workers give you complete control over network requests. You can return anything you want for the fetch event, it does not need to be the past or current contents of that particular file.

However, if the HTTP cache handles your needs, you are under no obligation to use Service Workers.

They are also used for things such as push notifications.

Documentation: https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API, https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers

like image 182
Daniel Herr Avatar answered Sep 21 '22 23:09

Daniel Herr