You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
refactor(react-query): hoist every staleTime into a named exported constant (#5427)
* refactor(react-query): hoist every staleTime into a named exported constant
Adds a repo-wide convention (documented in .claude/rules/sim-queries.md,
CLAUDE.md, and the react-query-best-practices skill/command) that every
staleTime value must come from a named exported constant instead of an
inline numeric literal. This prevents a server-side prefetch and its
client hook from independently duplicating the same duration and
drifting out of sync, which Greptile caught on PR #5426.
Applies the convention across all of hooks/queries/** and their
prefetch.ts consumers.
* refactor(react-query): use FOLDER_LIST_STALE_TIME in folders.ts hooks
useFolders and useFolderMap still used inline 60 * 1000 despite folders.ts
already exporting FOLDER_LIST_STALE_TIME — the same prefetch-drift gap this
refactor closes everywhere else. Same value, no behavior change.
Copy file name to clipboardExpand all lines: .agents/skills/react-query-best-practices/SKILL.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,7 +31,7 @@ Read these before analyzing:
31
31
32
32
### Query hooks
33
33
- Every `queryFn` must forward `signal` for request cancellation
34
-
- Every query must have an explicit `staleTime` (default 0 is almost never correct)
34
+
- Every query must have an explicit `staleTime` (default 0 is almost never correct), assigned from a named exported constant — never an inline numeric literal. A server-side prefetch hydrating the same query key must import and reuse that constant instead of restating the number
35
35
-`keepPreviousData` / `placeholderData` only on variable-key queries (where params change), never on static keys
36
36
- Use `enabled` to prevent queries from running without required params
Copy file name to clipboardExpand all lines: .claude/commands/react-query-best-practices.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,7 +31,7 @@ Read these before analyzing:
31
31
32
32
### Query hooks
33
33
- Every `queryFn` must forward `signal` for request cancellation
34
-
- Every query must have an explicit `staleTime` (default 0 is almost never correct)
34
+
- Every query must have an explicit `staleTime` (default 0 is almost never correct), assigned from a named exported constant — never an inline numeric literal. A server-side prefetch hydrating the same query key must import and reuse that constant instead of restating the number
35
35
-`keepPreviousData` / `placeholderData` only on variable-key queries (where params change), never on static keys
36
36
- Use `enabled` to prevent queries from running without required params
Copy file name to clipboardExpand all lines: .claude/rules/sim-queries.md
+4-2Lines changed: 4 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -50,14 +50,16 @@ The `'use client'` hook module then imports these back for its hooks. **Never**
50
50
## Query Hook
51
51
52
52
- Every `queryFn` must destructure and forward `signal` for request cancellation
53
-
- Every query must have an explicit `staleTime`
53
+
- Every query must have an explicit `staleTime`, assigned from a named exported constant (`ENTITY_LIST_STALE_TIME`), never an inline numeric literal. A server-side prefetch (`prefetch.ts`) hydrating the same query key must import and reuse that constant, not restate the number — this is what keeps a prefetched cache entry from going stale out of sync with the client hook that reads it
54
54
- Use `keepPreviousData` only on variable-key queries (where params change), never on static keys
55
55
- Same-origin JSON calls must go through `requestJson(contract, ...)` from `@/lib/api/client/request` against the contract in `@/lib/api/contracts/**`
Copy file name to clipboardExpand all lines: .cursor/commands/react-query-best-practices.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,7 +26,7 @@ Read these before analyzing:
26
26
27
27
### Query hooks
28
28
- Every `queryFn` must forward `signal` for request cancellation
29
-
- Every query must have an explicit `staleTime` (default 0 is almost never correct)
29
+
- Every query must have an explicit `staleTime` (default 0 is almost never correct), assigned from a named exported constant — never an inline numeric literal. A server-side prefetch hydrating the same query key must import and reuse that constant instead of restating the number
30
30
- `keepPreviousData` / `placeholderData` only on variable-key queries (where params change), never on static keys
31
31
- Use `enabled` to prevent queries from running without required params
import { requestJson } from '@/lib/api/client/request'
291
291
import { listEntitiesContract, type EntityList } from '@/lib/api/contracts/entities'
292
292
293
+
export const ENTITY_LIST_STALE_TIME = 60 * 1000
294
+
293
295
async function fetchEntities(workspaceId: string, signal?: AbortSignal): Promise<EntityList> {
294
296
const data = await requestJson(listEntitiesContract, {
295
297
query: { workspaceId },
@@ -303,7 +305,7 @@ export function useEntityList(workspaceId?: string) {
303
305
queryKey: entityKeys.list(workspaceId),
304
306
queryFn: ({ signal }) => fetchEntities(workspaceId as string, signal),
305
307
enabled: Boolean(workspaceId),
306
-
staleTime: 60 * 1000,
308
+
staleTime: ENTITY_LIST_STALE_TIME,
307
309
placeholderData: keepPreviousData,
308
310
})
309
311
}
@@ -326,7 +328,7 @@ export const entityKeys = {
326
328
### Query Hooks
327
329
328
330
- Every `queryFn` must forward `signal` for request cancellation
329
-
- Every query must have an explicit `staleTime`
331
+
- Every query must have an explicit `staleTime`, assigned from a named exported constant, never an inline numeric literal — a server-side prefetch hydrating the same query key must import and reuse that constant so the two never drift out of sync
330
332
- Use `keepPreviousData` only on variable-key queries (where params change), never on static keys
331
333
332
334
```typescript
@@ -335,7 +337,7 @@ export function useEntityList(workspaceId?: string) {
335
337
queryKey: entityKeys.list(workspaceId),
336
338
queryFn: ({ signal }) => fetchEntities(workspaceId as string, signal),
0 commit comments