Repository navigation
Commit 41443ce
fix(next): use latest interceptors in
## Problem
`useServerFunction` memoized `execute` with `useCallback(..., [fn,
...toArray(options.interceptors)])`. The dependency array's length
changed with the number of interceptors. React only compares deps up to
the shorter length (it just logs a dev warning), so when interceptors
were added or removed at the end, `execute` was not recreated and kept
running the old ones:
- `[a]` → `[a, b]`: `b` never runs
- `undefined` → `[a]`: `a` never runs
- `[a, b]` → `[a]`: the removed `b` still runs
## Fix
The latest `options.interceptors` are stored in a ref that is updated on
every render. `execute` reads the ref, so its deps are just `[fn]`. As a
result `execute` always uses the current interceptors and keeps the same
identity across renders. Before, it was recreated on every render
whenever interceptors were passed inline, which is what the docs show.
`useOptimisticServerFunction` no longer wraps its `onStart` interceptor
in `useCallback`. That wrapper was only there to keep the old spread
deps stable.
I searched `packages/next`, `packages/tanstack-query`, `packages/swr`
and `packages/pinia-colada` for the same spread-into-deps pattern. This
was the only place it appeared.
## Tests
Added one `renderHook` + `rerender` test that walks `undefined → [a, b]
→ [a] → undefined`. After each rerender it checks that `execute` keeps
the same identity and that only the current interceptors run. The test
fails on the old code and passes with the fix. `pnpm vitest run
packages/next`, `pnpm type:check` and `pnpm lint` all pass.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01VVaw7RfVeu6u8YEbwh664n
---------
Co-authored-by: Claude <noreply@anthropic.com>useServerFunction execute (#2388)1 parent 9505dac commit 41443ce
3 files changed
Lines changed: 50 additions & 5 deletions
File tree
- packages/next/src/hooks
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
| 33 | + | |
34 | 34 | | |
35 | | - | |
| 35 | + | |
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
221 | 221 | | |
222 | 222 | | |
223 | 223 | | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
224 | 266 | | |
225 | 267 | | |
226 | 268 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
123 | 126 | | |
124 | 127 | | |
125 | 128 | | |
| |||
135 | 138 | | |
136 | 139 | | |
137 | 140 | | |
138 | | - | |
| 141 | + | |
139 | 142 | | |
140 | 143 | | |
141 | 144 | | |
| |||
164 | 167 | | |
165 | 168 | | |
166 | 169 | | |
167 | | - | |
| 170 | + | |
168 | 171 | | |
169 | 172 | | |
170 | 173 | | |
| |||
0 commit comments