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

Browser CacheOffline CachingService Worker

Browser Cache Problem Overview


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?

Browser Cache Solutions


Solution 1 - Browser Cache

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

Solution 2 - Browser Cache

I wanted to share the points that I observed while going through service worker documentation and implemented it.

  1. Browser cache is different, as the service worker supports offline cache, the webapp will access the content that is cached, even though the network is not available.
  2. Service worker will give native experience.
  3. Service worker cannot modify DOM content but still it can serve the pages within its scope. With the help of events like postMessage, the page can be accessed and DOM can be changed.
  4. Service worker do not require user interaction or webpage . It runs in the background.

Solution 3 - Browser Cache

Actually, it's slower to response the request when you use sw instead of http cache... Because sw use cache api to store the cache content, it's really slower than the browser cache--memory cache and disk cache.

It's not designed for faster than http cache, howerver, when you use sw, you can Fully customizable the response, I think the Fully customizable is the reason why you should use it.

If your situation is not complicated enough, you should not use it

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbvakitiView Question on Stackoverflow
Solution 1 - Browser CacheDaniel HerrView Answer on Stackoverflow
Solution 2 - Browser CachebvakitiView Answer on Stackoverflow
Solution 3 - Browser Cacheuser2248202View Answer on Stackoverflow