fix(dashboard): fetch a snapshot restored from the offline copy on first mount

The offline persister restores the last dashboard snapshot at boot and
marks it invalidated so the first mount refetches it, but the snapshot
cell read refetchOnMount: false and never did. A page reloaded right
after a write, before the persister had flushed the newer snapshot,
painted the pre-write dashboard until the two-minute poll; the weight
just entered had no tile. A cell with live data still reads as it is on
remount; a restored, invalidated one fetches.
This commit is contained in:
Marc-André Bombeck committed 2026-10-23 19:25:30 +02:00
1 parent 0470e4e850
commit 9d1147a052
2 files changed
+14 -2

No files matched your search

@@ -83,7 +83,13 @@ describe("useDashboardSnapshot — auto-refresh on an open page", () => {
useDashboardSnapshot();
const opts = lastOpts();
expect(opts.staleTime).toBe(60_000);
expect(opts.refetchOnMount).toBe(false);
const refetchOnMount = opts.refetchOnMount as (query: {
state: { isInvalidated: boolean };
}) => boolean;
// A live cell is read as it is on remount ...
expect(refetchOnMount({ state: { isInvalidated: false } })).toBe(false);
// ... and one restored from the offline copy (invalidated) fetches.
expect(refetchOnMount({ state: { isInvalidated: true } })).toBe(true);
});
it("always refetches on focus so freshness is not suppressed by staleTime", () => {
+7 -1
View File
@@ -215,7 +215,13 @@ export function useDashboardSnapshot(enabled = true) {
queryFn: makeSnapshotQueryFn(queryClient),
enabled,
staleTime: 60_000,
refetchOnMount: false,
// A remount of a cell that holds live data reads it as it is, but a cell
// restored from the offline copy (`restorePersistedQueryCache`) arrives
// invalidated and must fetch on its first mount. Plain `false` kept the
// restored copy on screen: a page reloaded right after a write, before
// the persister had flushed the newer snapshot, painted the pre-write
// dashboard until the two-minute poll.
refetchOnMount: (query) => query.state.isInvalidated,
// Keep the snapshot and Today digest in lockstep after a background sync.
// Both are out-of-band reads, so staleTime cannot signal that a worker
// landed a new arrival while the browser was hidden.