cenko.tech

Stet: a private notes app that doesn't need the server to open

An offline-first notes PWA for one person on an iPhone, and the sync rules that let two devices disagree without losing a word.

I wanted somewhere to get a thought down the moment it arrives, often on my phone and often with bad signal. So I built Stet, a notes app for exactly one person. It installs to an iPhone home screen, opens without a connection, and syncs to a database when it can.

The name is the proofreader’s mark for “let it stand”. That is also the main rule of the app: whatever you wrote stays, even when two devices disagree about it.

The repo is private, because this is my own notebook and not a product, so there is nothing to install here. This post is about the decisions, which apply to any small offline-first app.

What it does#

  • Rich text with headings, bold, italic, lists and tick-able checklists, using Tiptap.
  • Folders, pinned notes and full-text search that ignores accents.
  • A trash that keeps notes for 30 days.
  • Light and dark themes, with the note text set in a serif.

It has one account, set in the server’s configuration, with no sign-up page, sharing or collaboration. Images, export and tags are left out of this first version.

The stack is React and Vite on the client, Express and MySQL on the server.

The device comes first#

The host I deploy to stops the app when it’s idle and starts it again on the next request. That cold start rules out any design where opening the app waits on the network, so the app shows your notes without asking the server for anything.

Every edit goes into IndexedDB on the device, and the service worker caches the whole app, so launching works in airplane mode. Sync happens afterwards, without blocking anything.

Typing doesn’t write to the database on every keystroke. Content writes wait 150 ms, and anything still pending is flushed when you leave the app, because that is the last sure chance to save.

iPhone web apps get no background sync, so sync has to run at moments when the app is awake:

  • on launch
  • on returning to the app
  • on reconnecting
  • two seconds after an edit
  • 30 seconds after a failed attempt

The same host also replaces the app’s files on every deploy, so nothing can live on its disk. All server-side data is in MySQL, including sessions and login failures.

When two devices disagree#

Each note carries the server revision it was last based on. When a device sends an edit, the server compares that revision with the last time the note’s text changed. If the text has moved on since, the two devices edited the same note without seeing each other’s work:

Phone and laptop both have the note at revision 40. Both go offline.

Laptop edits at 10:00, syncs first   → text saved, revision 41
Phone edits at 10:05, syncs later    → based on 40, but the text is at 41: conflict

Live note:  the phone's text (newer)
New note:   the laptop's text, marked "Older version"

There is no merging. Automatically merging two versions of a personal note can produce a sentence nobody wrote, and with one user a conflict should be rare. The newer text stays in the note, and the older text becomes a separate note that I can read, copy from or delete.

The rest of the sync logic is a list of smaller cases where the obvious approach loses data:

A late edit can still be the older one. If the phone wrote at 10:00 but only reaches the server at 11:00, after the laptop wrote at 10:30, the laptop’s text stays live. The phone’s text is still kept as a copy.

A delete never beats newer words. If I permanently delete a note on one device after typing more into it on another, the delete only wins if it happened after the last edit. If the note was already deleted when the new words arrive, they come back as a copy.

Pinning isn’t editing. Text and metadata (pin, folder, trash) have separate timestamps. Pinning a note on the phone can’t undo text written on the laptop, and it doesn’t create a conflict.

Retries can’t pile up duplicates. If the response to a sync is lost, the device sends the same changes again. A conflict copy’s ID is a hash of the note ID and its text, so the retry produces the same ID and the insert does nothing.

Editing during a sync is safe. Each note has a local version number that goes up on every edit. When a response arrives, a note whose version changed since the request was sent keeps its local text, and that text goes out on the next sync.

The cursor can’t skip a change. Each device asks for everything newer than the last revision it saw. Revisions come from a single counter row, and that row’s lock is held until the transaction commits, so writing syncs run one at a time and a lower revision can’t appear after a higher one has been read.

The rule behind all of these is that the server may choose which version is shown, but a sync never throws text away. The test suite has 27 tests, 11 on the client and 16 on the server, and they cover the conflict, late edit, delete, pinning, retry and in-flight cases by name. By default the server tests run against a built-in SQLite stand-in. They can also be pointed at a real, empty MySQL database.

Making it feel installed#

The fiddliest part wasn’t sync. It was iOS.

The keyboard only opens from inside a tap. iOS opens the keyboard only when focus happens during the tap itself, so there’s no time to mount an editor and focus it afterwards. Stet creates one editor when the app starts and keeps it for the whole session. Tapping “New note” loads an empty document into it synchronously, so it can be focused in the same tap.

The keyboard doesn’t resize the page. iOS only shrinks the visual viewport, which would leave the toolbar hidden behind the keyboard. The app shell follows visualViewport instead, updating its height through a CSS variable, so the bars stay above the keyboard.

The home screen app has its own login. It keeps its login separate from Safari, so you sign in from the installed app, not before installing it.

An abandoned note shouldn’t exist. A new note stays local until something is typed into it. If you open one and leave it empty, it disappears and nothing is sent to the server.

What “private” means here#

Private means a single account on my own deployment, with no notes service in between. It doesn’t mean end-to-end encrypted. Notes aren’t encrypted by the app, in IndexedDB or in MySQL, so anyone with access to the database or an unlocked device can read them. That’s acceptable for a notebook like this, but I wouldn’t keep passwords in it.

Login is deliberately simple. The password exists only as a scrypt hash in the server’s configuration, sessions are stored as hashes, and the session cookie is HttpOnly and Secure. Repeated failures are rate limited, cross-site writes are refused, and the Content Security Policy only allows the app’s own origin.

The limits#

  • “Newer” is decided by each device’s clock. If a clock is badly wrong, the wrong version can end up as the live note. Both texts are still kept, so it’s annoying rather than lossy.
  • Conflicts work on whole notes. Editing two different paragraphs on two devices still produces a copy.
  • Sync can only run while the app is open. An edit made offline stays on that device until the app is next opened with a connection.
  • Text sizes are fixed and don’t follow the iOS Text Size setting.
  • Signing out clears the device’s copy, so anything that hasn’t synced is lost from that device. The confirmation says how many notes that affects before you go ahead.

Stet is small because it serves one person with one job. Dropping accounts, sharing and live collaboration removed most of what makes notes apps complicated, and left the one problem I actually cared about: not losing a word.

cd ~/blog