Lesson 4 of 110 of 7 steps

Lesson 4 · about 30 minutes

Works with no signal

Your app starts remembering things: feeds, bookings, scores. Entries are saved on the phone first and sent when there's signal, so nothing is lost in a car park. Two phones can share the same log, and there are backups.

A Cloudflare D1 database behind the app, written only through your Worker. An offline queue on the phone that syncs when it's back online, and backups you can restore.

A D1 database that only your Worker can read and write, with every field checked. The phone saves entries to a queue first, with ids it makes itself, so a retry never saves anything twice. It syncs with a cursor, adds database columns before the code that needs them, and keeps backups with Time Travel plus a nightly copy in R2.

How much have you built before?
This lesson's wording changes to match.
Free plan is plentyWorks with no signal
📱 Your phonethe app
⚙️ Workerchecks and saves
🗄️ Databaseyour entries

Every entry goes through the Worker, which checks it before saving. Nothing else can write to your data.

What you'll make

  • A database for your app's entries
  • Entries that save with no signal and sync later
  • Two phones sharing one log
  • Backups you can restore

You'll need: an app online from lesson 2 or 3.

1

Somewhere to keep entries

Until now your app only remembered things on one phone. Now it gets a database at Cloudflare, called D1, so entries are kept safely and every phone can see them. Think of it as one shared notebook that lives online, instead of a separate notepad on each phone.

Say to your agent
Give this app a D1 database for its entries. Only the Worker should read or write it, and check every field before saving.
$ npx wrangler d1 create my-first-app-db

Your agent runs this and adds the database to wrangler.jsonc. Free plan: up to 5 GB.

2

Every entry is checked

Your phone never talks to the database directly. It sends entries to your app's server code, the Worker, which checks each one first: is it the right kind of thing, the right size, a sensible date, and from someone allowed to add it?

One clean…() per record type: allow-list fields, check types, ranges and lengths, reject the rest with a plain 400 message. Never trust ids or roles from the browser.

3

Save on the phone first

Every entry is saved on the phone straight away, then sent to the database when there's a connection. The screen updates instantly either way.

Say to your agent
Make it work with no signal: save entries on the phone first, send them when it's back online, and show a small note while they're waiting.
Try it
On the phone, waiting
Saved on the server
You should see

Try it: switch the signal off, add a couple of entries, then switch it back on and watch them move across.

4

Try it with no signal

Now try the real thing on your phone: turn on Airplane mode, add an entry, turn Airplane mode off.

On a computer: Chrome DevTools → Network → set "Offline".

✈ → 📶
You should see

The entry appears straight away, a small "waiting to save" note shows while offline, and it disappears once it's sent.

My entry vanished after I reconnected
The server probably rejected it. Ask your agent to show what the server said: a rejected entry should show a plain message, not disappear silently.
5

Two phones, one log

Open the app on a second phone or a second browser, signed in to the same log (lesson 5 covers sign-in). Add an entry on one; it shows on the other within seconds.

Sync with a cursor (updated_at), paged, with a "more" flag; soft deletes so deletions travel too; last write wins per row is fine for small groups.

6

Changing what you store later

Apps grow: one day you'll want to store something new. Your agent does this with small, numbered changes to the database, applied before the new version goes live, and only ever adding things while older versions are still on people's phones.

Say to your agent
I'd also like to add a short note to each entry. Add it safely, so older versions of the app keep working.
$ npx wrangler d1 migrations apply DB --remote$ npx wrangler deploy

Database change first, then the code that uses it.

7

Backups

D1 keeps a rolling history you can restore to any minute: 7 days on the free plan, 30 days on Workers Paid (from Cloudflare's docs, November 2026). For longer, your agent can save a nightly copy to your own storage.

Say to your agent
Set up a nightly backup and show me how a restore would work, without touching the live data.

Lesson learned: test a restore once, on a copy, counting rows. A backup you've never restored is a hope, not a backup.

Lesson 4 doneYou just gave your app a memory that works with no signal, and backups.
Next up · lesson 5, about 30 minutesSign-in with an emailed code, so only the right people get in.

Did it work?

Tick these off. They're saved in this browser, so you can come back later.

  • My app saves entries in a database
  • I added something with no signal and it saved when I was back online
  • Two phones (or two browsers) see the same entries
  • I know how far back I can restore