Service workers considered harmful

Service workers are an exciting new technology which allows to intercept network traffic. Cool stuff like saving content for offline usage and advanced caching is possible. They are as powerful as they are dangerous.

There are a lot of things that can go wrong. Users might not get the new version of a page anymore. The only way to recover from this is removing the local service worker, which persists over page reloads and browser sessions. Unfortunately this is not as easy as clearing the browser cache and common users are stuck with a bad version of it.

Hard to grasp

And this Problem is not limited to novices, even experienced developers happen to run into those problems. This technology is quite new and it’s not that easy to understand what’s going on (although not impossible). Therefore different abstractions come up such as Service Worker Precache from Google. Abstractions make complex code easy to use, but does not help understanding it’s implications. This is dangerous.

Recently the Create React App maintainers made the usage of service workers in v2 opt-in instead of enabled by default which it previously was. I think this is a step into the right direction as it should only be used if you considered and reviewed it carefully. Kent C. Dodds, who is a renowned person in the React ecosystem, had serious problems with a service worker which prevented users from seeing the buy button on the updated page. This is the worst case that can happen.

Evaluate

Don’t get me wrong: Service workers are an awesome piece of technology and incredibly useful for progressive web apps and other use cases. But you have to be more than careful when implementing them.

I decided to remove the gatsby-plugin-offline from this site for the reasons stated above. It’s as easy as throwing a single line into the Gatsby.js config to enable it, but comes at the cost of trusting others to not make any mistakes which is unlikely. The amount of effort to dig into it and struggles which can occur do not satisfy the benefits for this site.

Decide for yourself and the requirements of your project if it’s really necessary to have a service worker and behave accordingly.

I maintain a newsletter to share what I learn

Never miss an article about React and Javascript.