Skip to content

Commit 4cabb83

Browse files
committed
fix(files): reconcile the cache when a dimension write is content-version-rejected
Previously the client discarded a success:false (CAS-rejected) response, leaving its optimistic patch — which is for superseded bytes — lingering in the file-list cache. On rejection, invalidate the list so the cache reconciles with the new content (whose real size persists on its next load). Deliberately NOT a retry: re-sending the old measurement under the new key would write the wrong size. A transport error / read-only 403 still keeps the optimistic value (it's the real displayed size).
1 parent 497b223 commit 4cabb83

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

apps/sim/hooks/queries/workspace-files.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,7 @@ export function useWorkspaceImageDimensionsAdapter(workspaceId: string): ImageDi
156156
// on a real mismatch, so we overwrite to self-correct.
157157
if (!record || (record.width === dimensions.width && record.height === dimensions.height))
158158
return
159-
// Populate the cache so this and sibling views reserve space immediately. Kept even if the PATCH
160-
// fails (a 403 for a read-only member, or a transient error): the measurement is the real displayed
161-
// size, correct regardless of whether the write landed — a later list refetch reconciles.
159+
// Populate the cache so this and sibling views reserve space immediately.
162160
queryClient.setQueryData<WorkspaceFileRecord[]>(listKey, (previous) =>
163161
previous?.map((entry) => (entry.id === record.id ? { ...entry, ...dimensions } : entry))
164162
)
@@ -167,7 +165,18 @@ export function useWorkspaceImageDimensionsAdapter(workspaceId: string): ImageDi
167165
// Send the key we measured against; the server rejects the write if the row's content (key) has
168166
// since changed, so a stale in-flight PATCH for replaced bytes can't persist the old size.
169167
body: { key: record.key, ...dimensions },
170-
}).catch(() => {})
168+
})
169+
.then((response) => {
170+
// The guard rejected the write because the file's content (key) changed since we measured —
171+
// our optimistic patch is now for superseded bytes, so refetch to reconcile the cache with the
172+
// new content (its real size is persisted when the replaced image next loads). Do NOT re-send
173+
// this measurement: it's of the old bytes and would write the wrong size under the new key.
174+
if (!response.success) void queryClient.invalidateQueries({ queryKey: listKey })
175+
})
176+
// A transport error / 403 for a read-only member leaves the optimistic value in place: the
177+
// measurement is the real displayed size, correct whether or not it persisted; a later list
178+
// refetch reconciles it.
179+
.catch(() => {})
171180
},
172181
}
173182
}, [queryClient, workspaceId])

0 commit comments

Comments
 (0)