PersistedState
State that round-trips through a JSON file so it survives gova dev reloads.
PersistedState behaves exactly like State, but the value is written to
disk whenever it changes. On the next render (which, in development, usually
means after a gova dev reload), the value is seeded back from the file.
Use it for small UI state that would be annoying to lose on every code change: the selected tab, the text in a draft field, the current filter.
After the user types into the field and saves a source file, the dev server restarts the app and the draft is restored.
Storage location
PersistedState resolves the storage directory in this order:
PersistDir("...")option passed at the call site.- The
GOVA_DEV_STATEenvironment variable, whichgova devsets. $XDG_STATE_HOME/govaif set.~/.cache/govaas the final fallback.
Each key maps to a JSON file named <sanitized-key>.json in that
directory. Characters that are unsafe for a filename are replaced with
underscores.
Guarantees and limits
- Values are serialized with
encoding/json. The type must be JSON encodable. - Writes are best effort. If the filesystem is read-only or the disk is full, the call silently drops.
- Writes happen on every state change. Avoid
PersistedStatefor high-frequency updates, like a value bound to a mouse-move handler. PersistedStateis not intended for user data. For anything that should survive an app uninstall or sync across machines, write your own storage layer.
Opting out in production
PersistedState is active in every build, not just dev. If your app
has data you do not want cached between launches, use plain State
instead. There is no automatic cleanup; delete the state directory to
reset values during testing.