CodePark/ questions / Web workers & service workers
START PRACTICINGPRACTICE
Frontend question · Browser & web performance › Web workers & service workers

In a service worker, which caching strategy best fits your hashed, versioned JS and CSS files?

Cache-first

A hashed file never changes, so the cached copy is always correct. Cache-first serves it from the cache and avoids the network completely.

Stale-while-revalidate is the tempting wrong answer. It also serves from the cache, but then it fetches the file from the network in the background every time. For a hashed file, that request is always wasted, because the file can't change. Network-first is wrong too. It suits content that must be fresh, like HTML or API data, and here it would waste a request before every use. Network-only would lose the cache completely, so the files wouldn't work offline.