ClueShot

A cold start that draws last launch: persisting the TanStack Query cache in a mobile word game

How ClueShot restores last launch's TanStack Query cache as stale, checks whose it is, and revalidates with ETags so a cold start never opens empty.

By 5 min read

In short

ClueShot used to open on an empty shell while the boot screen waited for three requests whose answers hadn't changed. It now restores last launch's query cache from device storage, only for the same player, build, language and envelope version and when under seven days old. Every restored query is dated to the epoch, so it draws at once and is refetched immediately, and the Django API answers an unchanged refetch with a 304 and no body.

ClueShot is an Android word game I built. Each level shows a generated picture that starts obscured and clears as you spend coins, and you spell the word it hides from a bank of letter tiles; even the sound effects are synthesized on the device, so no audio files ship with the app. The client is React and TanStack Query in a Capacitor shell, and a Django REST API holds everything that counts: coins, packs, progress, goals.

That last part had a cost. Every number in the game lives on the server, so each cold start opened on an empty shell while the boot screen waited for three requests, and not one of those answers had changed since the player closed the app.

For a player on a slow phone connection, that meant staring at a boot screen to be told what they already knew.

Think of a note on the fridge. You read it the moment you walk in, but you open the cupboard before you trust it, and if you can ask "is the note still right?", the answer is one word.

What a launch does now

The app resolves the player's account first, and nothing is drawn yet. It then reads last launch's query cache from Capacitor Preferences and checks the envelope around it: same schema version, same app build, same language, same player, saved less than seven days ago and not in the future. Any mismatch deletes the blob, and the launch fetches as it always did.

A usable blob is hydrated into TanStack Query with every query's dataUpdatedAt set to zero. The map, the packs, the coins and the goals draw at once, and because they're dated to 1970, every screen that mounts treats them as stale and refetches.

Those refetches send the ETag each answer had last time. The server renders the payload, hashes it, and if nothing changed answers 304 Not Modified with no body; the client keeps what it holds. The comment beside that code puts the second-launch answer at 34 bytes.

Before, a cold start waited on three requests behind a boot screen; after, last launch's answers are drawn from device storage, marked stale, and revalidated with If-None-Match, so an unchanged map comes back as a 304 with no body.

Restored, but never trusted

The design rests on one function, asStale:

queries: state.queries.map(query => ({
  ...query,
  state: { ...query.state, dataUpdatedAt: 0 }
}))

With its real timestamp, a copy saved a minute ago would count as fresh, no screen would ask the server, and the player would be reading a snapshot with no way to tell. Dated to the epoch, the copy is only on screen until the server answers.

Most of the work was deciding what not to restore. Only six key prefixes persist, things slow to fetch and stable while the app is closed, such as the level map, packs and the player's state. A live match, a leaderboard page and the daily challenge stay out, because they change while the phone is in a pocket. A blob from another account is never hydrated, even briefly, which is why restoring waits for the account. A blob in the other language is refused, because pack names and goals are translated on the server.

Writes are debounced by a second and always rewrite the whole allowlist, so an ETag is never paired with another launch's data. Past 1 MB, the level map, the biggest and least missed piece, is dropped. A pending write is flushed when the page is hidden, because a phone doesn't close an app, it takes it away.

On the server, the ETag hashes the serialized payload rather than row timestamps, so progress, a repriced pack or the language all change it without anyone bumping a version. Responses are private, no-cache and vary on language, authorization and cookie. The trade-off is written in the code: the database does the same work as a full answer; the saving is on the wire.

How I checked it

The envelope rules live in a module with no storage behind it, so cacheEnvelope.test.ts tests them directly: it accepts this player's own copy from this build, and refuses another account's, one with no account to match, the other language, a different build, an older schema, one past the age limit and one "stamped in the future".

On the Django side, ConditionalGetTests checks a repeated request gets a 304 with an empty body, a weak validator still matches, a wrong one gets the full answer, a new level or a repriced pack changes the validator, a Persian request carrying the English validator gets the Persian answer, and the pack map is private and revalidated.

The write path, its debounce, size cap and flush, isn't tested, and I haven't timed launches on a device, so I can't give a number for how much sooner the menu appears.

Where it stands

A returning player sees their own coins and map as soon as the account resolves, and the server confirms or corrects them behind the menu. With no connection the restored numbers stay on screen, and a later change has the app say the server is unreachable, judged from the requests it already makes rather than navigator.onLine. Every app update starts cold once, since a new build never reads an old blob; dropping is simpler than migrating, and the fallback is just the old launch.

What carries over

  • Restore a persisted cache as stale, so the server's answer is the one that stays on screen.
  • Stamp saved data with its owner, build and language, and refuse any mismatch outright.
  • Persist only what is slow to fetch and can't change while the app is closed.
  • Hash the rendered payload for an ETag, so every input to the answer changes it.
  • Keep validators and the data they describe in one write.

For the player, the game now opens on their own numbers instead of a spinner. Under that sits a deliberate split, the device remembers and the server decides, with a short list of rules that keep it from the one harmful case: showing someone a number the server never confirmed.

Further reading

  • React
  • TanStack Query
  • Mobile
  • Offline-first
  • HTTP caching
  • Django

More writing