Overview
By default, kweri’s cache lives in memory and is lost on page reload. APersistenceAdapter lets you save the cache to any storage medium and restore it on the next session.
PersistenceAdapter interface
localStorage example
How hydration works
- On construction, kweri calls
adapter.restore() - If the returned data’s
versiondoesn’t matchCACHE_VERSION, it’s discarded - Matching entries are loaded into the
CacheStore - All restored entries have
updatedAtforced to0— they are immediately stale and will background-refetch on first access
What gets persisted
Onlysuccess entries are saved. Errors, loading states, and idle entries are excluded.
Versioning
CACHE_VERSION is a constant (1) that guards against restoring cache from an incompatible schema. Bump it whenever the CacheEntry structure changes in a breaking way:
restore() should return null.
Selective persistence
To persist only specific queries, wrap the adapter:Error handling
All exceptions thrown bysave() and restore() are silently caught by kweri. A failing persistence adapter will never crash your application.