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

vbharath picture vbharath · Apr 28, 2016 · Viewed 7.8k times · Source

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?

Answer

Daniel Herr picture Daniel Herr · Apr 28, 2016

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 browser 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