Learn Fate

Keep progress across reloads

Chapter 6 of 6

The tracker currently forgets your count on a full reload. Persisting client state lets Fate restore it from IndexedDB before the view renders.

Install the storage adapter.

pnpm add @nkzw/fate-indexeddb

Import the persistence factory and adapter in state.ts. In createAppClient, replace createClient() with the configured client below. Keep the existing .registerClientSchema(...) call on that instance.

import { createPersistence } from '@nkzw/fate/persistence';
import { createIndexedDBStorage } from '@nkzw/fate-indexeddb';

createClient({
  persistence: createPersistence({
    key: 'reading-tracker',
    storage: createIndexedDBStorage(),
  }),
});

Keep the key stable so later visits load the same saved data. If the app adds sign-in, scope the key to the account. If you rename or remove a saved field, give the client state a new schema version and migrate existing values.

Persistence lets this local tracker work across reloads. Server reads and offline server writes have their own policies. If you queue a remote mutation, decide when to replay it and how the server will recognize duplicates. If you use server rendering, create a separate client per request and follow Fate’s hydration guidance.

Finish the tracker

  1. Click one panel and confirm both panels show the new count.
  2. Reload and confirm the count survives.
  3. Open a second tab, make another change, and reload the first tab to check the saved total.
  4. Add a field to the local schema with a default, such as a daily page goal. Select it in one view and confirm that the saved count is still available.
  5. Explain the full path in your own words: the root request resolves the reference, the view reads selected fields, the mutation changes the record, and persistence saves the client state.

Persisting client state · Persistence and hydration