< Back

Offline Is Not An Error

Stop treating it like one

calendar

Sep 30, 2024

Header for posting Offline Is Not An Error
Photo by Bermix Studio on Unsplash

Does your web app work offline? Does it work when you disconnect from the internet and start it up? Or does it show that useless white screen with a dinosaur that tells users they’re offline?

Chances are, your web app does the latter and while that’s what most web apps do, it’s a really bad user experience.

Most web apps fall apart when you try to use them while you’re offline. Most won’t even start up and give you the T-Rex immediately and others will gradually deteriorate and start throwing errors frantically. The problem is that they all treat offline as an error, while it’s not.

Offline is not an error. It’s a common state for your app to be in, and your app should be able to handle it and provide the best possible user experience for that situation.

Don’t believe me? You think that offline support doesn’t apply to your web app because it needs a network to function?

Here’s a fun experiment: go to a remote area, a concrete building, or a parking garage and try your web app there. There’s a big chance you won’t have any network in there.

That is what I mean by “offline”, and it’s a very common situation that your web app can be in.

Users go to remote areas and parking garages where they temporarily have no network, and in those situations, your web app shouldn’t fall apart by only showing a useless white screen, telling me I’m offline. Duh.

Native apps don’t show a dinosaur when they’re offline, and your web app shouldn't either.

X and Instagram don’t treat offline as an error and show you your personal feed as it was when the app went offline. You won’t be able to see any new content, and you can’t post anything, but instead they’ll save your postings while offline and post them when the network is back.

Both X and Instagram need a network to function, yet they provide a meaningful user-experience while offline.

(sad fact: their web apps don’t!)

Where to start

Offline support doesn’t mean that you have to provide the exact same user experience as when the user is online. It means that you should provide the best-possible user-experience possible.

At the very least, your web app should start up normally while offline and any outgoing requests should be saved locally and sent when the network is back. That is what offline support is about, and it’s not hard to implement.

I created a basic Service Worker to help you add offline support to your web app. A Service Worker is a special kind of Web Worker that can intercept and respond to network requests. When a request is made while the web app is offline, the Service Worker can intercept it and serve a response from its cache. This cache typically contains static assets like images, stylesheets, JavaScript etc. but it can also contain full HTML pages. The Service Worker matches any request against its cache and then serves the response from there.

It can also cache responses to API calls or save outgoing requests while the web app is offline and then send them when the app is back online.

This basic version takes care of caching all assets locally, providing responses for the routes you provide, and storing POST requests while the app is offline, which are retried when the app is back online.

A basic Service Worker to make your web app work offline - DannyMoerkerke/basic-service-worker

Say goodbye to the dinosaur

Offline is not an error, but a very common state that your web app can be in and that it should be able to handle. Your web app doesn’t need to provide the exact same user experience while offline, but it should provide the best possible user experience for that situation.


Join my email list Modern Web Weekly, for a weekly update on Progressive Web Apps (PWA), Web Components and new features of the modern web platform, tested and explained in plain English.