useChat: streamed message updates are urgent and starve App Router navigation (~5 s commit delay)
Description
While useChat streams a response, a Next.js App Router navigation away from the chat page can take ~5 s to commit. The destination page only needs to take longer to render than the throttle window.
Cause. useChat reads messages through useSyncExternalStore. In React, store updates are always urgent (SyncLane), even when they fire inside a transition (docs). App Router navigation renders the destination inside a transition, and the chat page stays mounted until that transition commits. Each throttled chunk (every 50 ms by default) schedules an urgent render, and that render interrupts the navigation render and restarts it from scratch. If the destination takes longer than 50 ms to render, the render never completes. The navigation only commits when React expires the starved transition lane (transitionLaneExpirationMs = 5000) and renders it synchronously.
The symptom: the user clicks a link, nothing happens for ~5 s, then the page switches. It is worst on slow CPUs.
Measurements. We ran production builds with Chrome CPU throttling and a 40 s synthetic SSE stream, and measured click-to-commit (median). The "after" column uses a fork of useChat that applies message snapshots in startTransition:
| Scenario | useChat |
Messages applied in startTransition |
|---|---|---|
| CPU 4x, heavy destination | 5,423 ms | 523 ms |
| CPU 6x + Slow 4G, heavy destination | 5,759 ms | 281 ms |
useDeferredValue(messages) does not help, because the urgent render still happens on every chunk. A larger throttle only moves the threshold.
Workaround. We copied useChat into our own hook. It subscribes with chat['~registerMessagesCallback'](cb, throttle) and applies each snapshot with startTransition(() => setState(chat.messages)) instead of useSyncExternalStore. status and error stay on useSyncExternalStore. When status becomes ready or error, the hook flushes the latest messages urgently first, so a terminal status never renders next to a partial message. This works, but it depends on the internal ~registerMessagesCallback API, and we must keep the fork in sync with useChat.
Request. Please add a supported way to make streamed message updates non-urgent. For example:
useChat({
// 'sync' (default, current behavior) | 'transition'
experimental_messagesUpdatePriority: 'transition',
});With 'transition', useChat would keep the messages snapshot in useState and apply each throttled update inside startTransition, flushing before status changes to ready/error, as described above. A public subscription API on Chat (a stable registerMessagesCallback) would also let users build this without depending on internals.
Reproduction
- Next.js App Router (16.3), React 19.2. Page A renders
useChatand streams a long response. - Page B is a client page whose render takes more than 50 ms (for example, a large table), or test with DevTools CPU throttling at 4x.
- While the stream runs, click a
<Link>from page A to page B. - Expected: B commits in a few hundred ms. Actual: B commits after ~5 s.
In Chrome DevTools, the React Performance Tracks ("Scheduler" track) show repeated Transition renders, each cut off by a Blocking render, then one long render at ~5 s.
AI SDK Version
- ai: 7.0.76
- @ai-sdk/react: 4.0.79
- react / react-dom: 19.2.4
- next: 16.3.3
Code of Conduct
- I agree to follow this project's Code of Conduct
Source: vercel/ai