Caching server driven payloads
Cache the page document in your PageLoader. The framework does not ship a cache. Stale-while-revalidate is a host policy.
Cache the page document in your PageLoader. The framework does not ship a cache. Stale-while-revalidate is a host policy. If the network is down and you still have bytes, return those bytes. Flow-UI will decode them the same way it decodes a live response. It never knows the difference.
The FAQ is one paragraph: your PageLoader decides; serve cached bytes when the network fails; the package renders them identically. PageLoader: the networking seam is the protocol. This article is what to cache, what not to cache, and why a framework cache would guess wrong about your auth and freshness rules.
Hacker News threads on server driven UI often mention prefetching a document to disk so the next launch paints immediately. That is a host idea. Implement it in the loader. Do not wait for Flow-UI to grow URLCache configuration. It will not.
There is no framework cache to turn on#
PageStore holds the current PageModel in memory for the life of the store. That is not a disk cache and not a cross-launch cache. Killing the app drops it. A new store starts at .loading and calls loadPage(.initial) again.
WidgetStateStore is ephemeral UI state (stepper counts, accordion expansion). It is not a payload cache. Refresh clears it. Pagination does not.
Image bytes are FlowImageLoader, another host seam. Caching images in the page document cache is usually the wrong layer. Cache images in the image loader.
Unknown widget policy, diagnostics, and decode live above Data. Cached bytes that contain a type this binary does not know still skip or placeholder. Caching does not freeze schema forever. See unknown widgets and schema versioning.
That sketch is host code. DiskStore is yours. The package has no such type.
A first-paint policy: return disk bytes immediately from .initial if present, then let the host kick a background refresh that writes a new file and asks the store to refresh(). Or return cache only in catch after the network throws. Both are valid. Flow-UI does not pick.
What to cache#
Cache PageResponse JSON for .initial of page ids the user actually opens (home, a keyed category). Key the file by pageID plus any cohort or locale your backend varies on. If two users can share a device, include a user key or do not persist.
Consider caching .refresh responses as the new disk value after a successful pull, so the next cold start is the last good document.
Be careful with .nextPage. Concatenating pages on disk means you must store the merged document yourself. PageStore merges in memory. If you only cache the first page, a cold start shows page one until pagination runs again, which is usually correct.
Be careful with .action. An ActionResponse is not a page. Caching the last mutation blob and returning it for .initial will fail page decode. If you want optimistic UI after a favourite, that is in-memory PageMutation already applied. Persist a new page snapshot only if you also run a full fetch and save that.
Stale-while-revalidate#
Show cached .initial bytes (store will decode and render). In parallel, fetch. On success, call refresh() or apply(.replacePage) from a host-owned path. refresh() keeps content visible during the round trip and resets widget UI state. If you did not want steppers reset, apply replacePage yourself and accept that you are outside the built-in refresh gesture.
ETags and If-None-Match belong in the loader. A 304 can return the cached Data you already have. The package will decode it again. If decode is expensive, the host can skip calling loadPage until the validator says the bytes changed, but PageStore always decodes whatever you return. There is no “bytes unchanged, skip decode” API.
Offline#
When URLSession throws and disk has bytes, return the bytes. The user sees last night’s home. Diagnostics still run. Actions may still fail; throw from .action or return a structured error toast from your handler. Do not pretend a favourite persisted if you queued nothing.
When disk is empty and the network throws, throw. The store shows the failed state. That is honest.
When not to use server driven UI still applies: a cached JSON page will not help a map canvas. It will help a merchandising feed.
What not to cache#
Do not cache unknown-user personalized pages on a shared iPad without a key.
Do not cache api responses as if they were pages.
Do not cache a document that includes one-time tokens in widget actions unless you understand expiry. A stale api payload might submit a dead nonce.
Do not invent a Flow-UI Cache-Control parser in the renderer. If the backend sends cache headers, the loader’s HTTP stack is the place they already work (URLSession plus URLCache) if you use them.
Do not store decoded PageModel in UserDefaults. Store Data. Schema evolves. Your disk should be the envelope bytes, decoded with today’s registry at read time.
Prefetch#
A HN-style prefetch: when the user is on home, the host fetches pageID: "next_likely" and writes disk. When they open that page, .initial hits disk first. PageStore is not involved until the view exists. Prefetch is loader plus your navigation predictor. The package will not prefetch because it does not know your graph and does not network.
Warm the image loader separately if the document is image-heavy. Prefetching JSON without images still flickers media.
Invalidation#
A cache without invalidation is a bug farm. Logout should drop user-keyed files. A locale change should drop or namespace them. An app update that registers a new required widget does not require you to delete disk: unknown types skip, and new types render when present. An app update that changes the meaning of a JSON key is a schema break; delete the cache or namespace by a host schema integer you send as a header.
Do not invalidate by decoding PageModel and comparing hashes of widgets. Hash the raw Data if you need an equality check. Decode is the store’s job.
Memory: PageStore already holds the live model. Disk cache is for process death and for .initial before the radio returns. Holding three decoded pages in a global beside the store duplicates Observation state and will desync after a mutation. If you need a page the user left, keep the Data or keep the store. Not both with two writers.
When a cached document is older than your product’s comfort (a flash sale that ended), either bound TTL in the loader or send refresh as soon as you have network. Flow-UI will not expire bytes for you.
Where this sits#
FAQ is the canonical short answer. PageLoader: the networking seam is the function you wrap. Unknown widgets and schema versioning is why cached old documents still render on new binaries (and why new types skip on old binaries). No ranking claims about cache hit rates. No framework switch named enableCache.
Bytes in the loader. Policy in the host. Decode in the store.
Read next#
- FAQ
- PageLoader: the networking seam
- Unknown widgets and schema versioning
- When not to use server driven UI
Cache documents in PageLoader. Flow-UI does not ship a cache.
Related documentation
Related articles
- Production6 min read
Decode diagnostics in production
Surface DecodeDiagnostics in debug overlays and logs. A malformed widget should be a telemetry event, not a blank screen.
- Production6 min read
Swift 6 and MainActor widgets
WidgetView is MainActor. Swift 6 language mode is the default for Flow-UI. Register widgets that are safe to render on the main actor.
- Production6 min read
Accessibility in server driven SwiftUI
Native widgets inherit VoiceOver. The schema still has to send alt text, button traits, and type that scales.
More on the blog.
Read the source
Flow-UI is MIT licensed. The schema, renderer and starter widgets live on GitHub.
GitHubGet started with Flow-UI
Install the Swift package, register a widget, and render a page from JSON.
Get started