Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If you are using Service Workers do you still need cache-control headers?

Is there any use-case where the use of cache-control headers should be preferred over Service Workers? Is there any benefit using both of them (apart from the fact the SW are not cross-browser supported) ?

like image 661
Avraam Mavridis Avatar asked Dec 08 '22 19:12

Avraam Mavridis


1 Answers

Absolutely. HTTP cache, which is controlled by HTTP cache headers sits between the network and the service worker. All fetch requests initiated from the service worker still use HTTP cache.

Let's say you have a script with long max-age. Most Service Workers repopulate their caches every time they are installed, which is to say, every time something/anything changes in the service worker script. However, if HTTP cache headers are properly configured many resources to be included in Service Worker cache will be still present in HTTP cache and can be fetched without involving the network.

Properly configured HTTP headers are also essential for intermediary caching proxies (CDNs) to work properly. Proxies do not know anything about Service Worker and its caches.

More on this in Caching best practices & max-age gotchas by Jake Archibald.

like image 108
pirxpilot Avatar answered May 16 '23 06:05

pirxpilot