Data evolution
Persistent state is a private JSON document for each app user. The platform infers each version's data schema from the usePersistentItem calls in app.tsx. Each key becomes a top-level field, and the hook's TypeScript type and default help define its shape.
Defaults and existing values
Keep stored keys and value types stable for UI-only changes. Renaming a key, changing a value's type, or reorganizing a stored object changes the data schema.
usePersistentItem(key, defaultValue) uses the default for a missing key and returns existing values as stored. When adding a field to an existing object, prefer an optional field with a sensible fallback where the app reads it. Store JSON values, such as ISO strings for dates and uploaded or generated URLs for media.
import { usePersistentItem } from "@/hooks/use-persistent-item";
type Settings = { compact?: boolean; accent?: string };
export default function App() {
const [settings, setSettings] = usePersistentItem<Settings>("settings", {});
const compact = settings.compact ?? false;
return (
<button type="button" onClick={() => setSettings(current => ({ ...current, compact: !compact }))}>
{compact ? "Use roomy layout" : "Use compact layout"}
</button>
);
}
Publishing changes
When you publish an update, the platform compares the live and new schemas. If the schema changed, it prepares a migration from the old shape to the new one and applies it when an existing user's data is first loaded for the new version.
Let the platform handle conversion and preserve saved values when the app mounts. Keep structural changes small. Test in preview with both fresh state and representative older values. A successful build only verifies compilation. If publishing reports a migration error, review it before retrying.