Yashveer Singh
Connect
<- All posts
Performance Optimization6 min read

Service Workers: When They Help and When They Hurt

Service workers are JavaScript files that run in a separate thread from the web page, intercepting network requests and enabling features like offline access, push notifications, background sync, and advanced caching strategies. They act as a programmable proxy between the browser and the network, giving developers control over how resources are fetched and cached. Service workers are the foundation of Progressive Web Apps (PWAs) but are equally applicable to standard web applications that want caching or offline capabilities.

Written by Yashveer Singh, founder of Yashveer Labs.

What you need to know

  • Service workers enable offline access, push notifications, and advanced caching strategies not possible with HTTP cache headers alone.
  • The most common service worker production problem is stale content after deployment. Use content-hashed asset URLs and stale-while-revalidate for HTML to mitigate it.
  • Cache strategies must be intentional per resource type. Network-only for payment flows; cache-first for content-hashed static assets; stale-while-revalidate for frequently-changing pages.
  • Service workers are hardest to debug because their behavior depends on installation and activation state that does not exist in a fresh development environment.
  • For most SaaS web applications, service workers are not necessary. Add them when offline capability or push notifications are product requirements, not for performance alone.

The core argument

Service workers occupy an unusual position in web development: they provide capabilities that are genuinely powerful and difficult to achieve otherwise, while simultaneously being the source of some of the most confusing production bugs in web development. The stale content problem, where a deployed update is not visible to users for hours or days because their service worker is serving the old files from cache, is a real and recurring issue that has caused confusion in many products I have seen.

The value proposition is strongest for applications where offline capability is a product requirement, not a nice-to-have. A field service application that workers use on job sites with unreliable connectivity, or a documentation reader that users want to access on a plane, genuinely benefits from service worker caching. An SaaS dashboard that is only used at a desk with a reliable internet connection gets minimal benefit from the complexity.

Push notifications are the other compelling case. Service workers enable push notifications to reach users even when the browser tab is not open, which is a meaningful engagement capability for certain product types. But push notifications are a feature decision, not a performance optimization, and should be evaluated on the product's notification strategy merit, not as a side benefit of service worker adoption.

Common mistakes

  1. Installing a service worker without a cache invalidation strategy. The moment a service worker is deployed, it begins caching resources. Without a plan for how those cached resources are invalidated when content updates, the service worker will serve stale content after every deployment. The plan must be in place before the service worker is deployed, not figured out after the first stale content incident.
  1. Caching API responses without considering personalization. An API response for user A's dashboard data must not be served to user B. Service workers that cache API responses without account for the user context can serve one user's data to another user, especially after a logout and login sequence. Exclude authenticated API responses from service worker caching or include the user identity in the cache key.
  1. Not providing an offline fallback page. A service worker that intercepts all navigation requests must handle the case where the requested page is not in the cache and the network is unavailable. Without an explicit offline fallback (a pre-cached offline.html page served when the navigation fails), the user sees a browser error rather than an application-specific offline message.
  1. Using service workers to cache third-party scripts without a clear update policy. Caching a third-party analytics, monitoring, or A/B testing script means the application may run an outdated version of that script after the vendor releases an update. Third-party scripts that are critical to the product experience (not just analytics) should use network-first strategies or be excluded from service worker caching.
  1. Not testing service worker behavior in the uninstalled, installing, and active states. Service workers behave differently depending on whether they are newly installed, waiting to activate, or active. Testing only the active state misses the bugs that occur during the update transition. Chrome DevTools' service worker panel supports forcing different states, which is essential for testing update behavior.

Where to start

  1. Define the specific capability that justifies adding a service worker. Is it offline access? Push notifications? Improved repeat-visit load times? The capability definition determines the caching strategy required and the scope of the service worker. A service worker added "because PWA" without a specific capability in mind adds complexity without benefit.
  1. Implement Workbox for the first service worker rather than writing it from scratch. Workbox is Google's service worker library that provides preconfigured caching strategies, cache versioning, and update handling. It reduces the chance of the common mistakes (no invalidation strategy, incorrect offline handling) by providing tested patterns out of the box. The first service worker in a Next.js project can be added with next-pwa or a custom Workbox configuration.
  1. Test the update flow specifically before shipping. In Chrome DevTools, use the service worker panel to force the new service worker to install and activate. Verify that users see the new content after the update, that they do not see stale content, and that the update notification (if implemented) works correctly. This test must happen before production deployment, not after the first stale content report from a user.

Related reading

FAQ

Frequently asked

Author

The reason I write these

I write these because the writing is the proof. Yashveer Singh, founder of Yashveer Labs. The systems I build are not theoretical. They are running right now, serving real users, generating real revenue. That is the bar I hold this writing to. If you want to hire someone who can match that bar, I am the call.

Related reading