Your app should still work when the internet doesn’t

Anyone who has lived in Lebanon knows the sound. The lights dip, there’s a click somewhere in the building, and the generator takes over. The lights come back in a second. The router takes a lot longer.
Now picture that happening in a clothing shop at 7:30 on a Saturday night, with four people waiting at the till. If the sales app needs the internet to save a sale, the cashier is stuck. She apologizes, she refreshes, she writes things on a piece of paper to type in later (and some of them never get typed in).
We’ve seen versions of this so many times that we changed how we build apps. Today, almost everything we make for shops, warehouses and field teams is what developers call “offline-first”. It sounds like jargon, but the idea is simple, and I think more business owners should ask their developers about it.
So what is it?
Most apps ask the server for permission before they do anything. You tap “save”, the app sends your data off, waits, and only then tells you it worked. On a good connection you don’t notice. On a bad one, you stare at a spinner.
An offline-first app saves to the phone (or tablet, or the till’s PC) first. Straight away. The screen updates, the cashier moves on to the next customer, and somewhere in the background the app sends everything to the server when it can. If the internet is down for ten minutes, the sales just wait in line on the device and go up when the connection comes back.
A nice side effect: these apps feel fast even on perfect Wi-Fi, because nobody is ever waiting on a round trip to a server in Frankfurt.
How we actually build it
There’s no magic library that does all of this for you, unfortunately. It comes down to a few decisions, made early.
Keep a real database on the device. Not a few values in local storage. On mobile we use SQLite; in the browser, IndexedDB. The app reads from it and writes to it all the time, and treats the server as something to sync with, not something to wait for.
Write every change to an outbox too. When a sale happens, we save it, and we also add a line to a small “to send” table. A background job works through that table and removes each line once the server confirms it got it. Here’s roughly what that looks like:
async function recordSale(sale: Sale) {
const id = crypto.randomUUID(); // made on the device, not by the server
await db.sales.put({ ...sale, id, synced: false });
await db.outbox.add({ id, type: "sale.created", payload: sale, at: Date.now() });
syncSoon(); // try now, try again later if we're offline
}
The ID line matters more than it looks. If you wait for the server to give each sale its number, you can’t do anything with that sale while you’re offline: you can’t print a receipt that refers to it, you can’t attach a return to it. So the device makes the ID itself. And the server is built so that if the same sale arrives twice (it happens, connections drop halfway through), it only counts it once.
Decide in advance what happens when two people edit the same thing. This is the part teams skip, and it’s the one that bites later. Someone in the stockroom updates a product while the shop’s tablet is offline and does the same. Who wins? For a customer’s phone number, the latest edit is fine. For stock and money, we never overwrite a number. We record movements instead: five received, one sold, two returned. Movements from different devices don’t fight each other. They just add up.
Be honest with the user. A small line at the top saying “3 sales waiting to sync” is enough. People don’t panic when they can see what’s going on. They panic when the app goes quiet.
Mistakes we’ve made (so you don’t have to)
We didn’t get this right the first time, so here are a few things we learned the hard way.
Don’t trust the phone’s clock. Some of them are off by hours, and if you sort orders by device time you’ll get some very strange reports.
Don’t send the whole database on every sync. It works fine in testing with forty records and falls apart on mobile data with forty thousand. Only send what changed.
Remember that app updates touch the local database too. Somebody out there will install your new version with two weeks of unsynced data sitting on the old format. Plan for that person.
And please don’t test only on the office Wi-Fi. Turn on airplane mode in the middle of a checkout. Pull the router’s plug during a sync. That’s where you find the real bugs, and it’s a lot cheaper to find them yourself than to hear about them from a client on a Saturday night.
Do you need this?
Not always. It’s more work: a local database, a sync layer, rules for conflicts, more testing. For a company website or a dashboard people open from their desk, a normal online app is fine.
But if your app is used where the work happens (on a shop floor, in a warehouse, on a delivery route, in a clinic), and if a lost action means lost money, then yes. Especially here, where power and internet are things you hope for rather than count on. We’d rather spend a bit more at the start than have a client lose a Saturday’s sales.
If you’re planning something like this and want a second opinion, get in touch. We like talking about this stuff more than we probably should.