garage44.eu/jeroen/cartopolis

logclone: git clone https://garage44.eu/jeroen/cartopolis.git

.. /docs/notes/web-persistence.md

raw


topic: web-persistence triggers: [storage.rs, user_store.rs, photo_store.rs, IndexedDB, idb, OPFS, localStorage, "web build", wasm32, "user.json", "does not persist", "settings lost", navigator.storage, persist, quota, Namespace, read_now, preload, "clear cache", "reset cache", ClearProgress, cache_reset.rs, platform::storage, crates/cartopolis/src/lib.rs, std::fs] updated: 2026-11-26

Everything the client keeps goes through one seam, IndexedDB-backed on the web

platform::storage is the whole of this client's persistence on every target: a get/put over /-separated, path-shaped keys in two namespaces. Natively a key is a file under cache_root() or app_data_root(); on the web it is a record in IndexedDB, one object store per namespace.

Until 2026-08-13 the web build persisted nothing. user.json went through std::fs, which fails on wasm, so every setting, bookmark and folder was silently lost on reload — the app came up on defaults every time and said nothing, because each write was best-effort by design.

The one constraint that shapes all of it

No browser offers a synchronous read on the main thread, and two callers need one: UserStore::load runs while the App is still being built, and photo_store::read_thumb runs inside a frame (lib_places::PlaceThumbs). Neither can await.

Options considered, and why they lost:

What it does instead: preload, then everything downstream is native-shaped

run() on wasm pulls the keys that must be readable synchronously into memory before the App is constructed, and starts the client inside the completion: user.json, every photos/*.thumb.jpg (thumbnails only — a full photo is megabytes and only wanted when someone opens it), and the private-chat state under private_chat::STATE_PREFIX, read on the frame a session comes up. The list is the storage::preload calls in crates/cartopolis/src/lib.rs. read_now answers out of that map. Startup then runs in exactly the order it does natively.

The corollary is a rule for new code, not just for this seam: nothing persists through std::fs (it compiles on wasm and every call fails), and a caller that must read synchronously is either on the preload list or written to survive Loading.

This is the same answer Emscripten reached with MEMFS + IDBFS + FS.syncfs(), and for the same reason. It is not a workaround for something a better API would fix; it is what a synchronous read is, in a browser.

The cost is a few milliseconds of IndexedDB before the app starts, behind a page that is already showing its loader.

What cannot be preloaded goes through the same read_now, which answers Cached::Loading on the first ask, starts the read, and has the value on a later frame. Loading and Missing are different states and collapsing them is a bug that never recovers: a caller that caches "not there" the way it would cache a filesystem miss will keep that answer forever. The photo viewer's unavailable flag exists for exactly this — see bookmarks::photo_viewer.

The namespaces are a durability statement

Namespace::Cache is re-fetchable (tiles, 3DBAG, Overpass, transit). Namespace::Data is the user's own (user.json, photos). Natively that is the cache root against the data root, so clearing tile cache cannot take a user's saved places. On the web it is what the browser is asked to keep: the first Data write calls navigator.storage.persist(), because otherwise a browser under pressure — or Safari after seven idle days — discards the origin's data without asking, which reads as the saved places vanishing on their own. A refusal is logged, not surfaced: best-effort is the status quo, not a new failure.

the_two_namespaces_do_not_see_each_other in storage.rs is the test that keeps this real rather than conventional.

It is also what makes storage::clear(Namespace::Cache) — Settings ▸ General ▸ Storage, added 2026-09-10 — safe to offer to anybody: the button cannot reach a saved place, a photo or a setting, because those are not in the half it empties. Namespace::Data is deliberately not clearable through the same call. It would not work as anybody expects: user.json is written back from memory on a timer, so it would reappear within seconds, and "delete everything I have" is a different feature with a different confirmation.

Two things about a clear are worth knowing before writing another one:

The web's in-memory view keeps Data, not the cache. It exists for read_now, and every synchronous reader reads Data. Until 2026-10-20 an awaited get or put of the cache kept a copy too, and never dropped one, so every map tile, height patch and 3DBAG cell the page ever touched stayed in the wasm heap. That was one of the things filling it to the 4 GiB limit (wasm-performance.md). A cache entry that read_now or write_now touched is still kept; storage::view_bytes() is the view's size, and the gpu log line reports it.

Gaps, deliberately left