Repository navigation
fix: resolve React hydration and infinite loop errors in chat panel - #432
Conversation
- Add isMounted state to defer hydration-sensitive operations - Add suppressHydrationWarning to ScrollArea components - Guard all scroll/resize effects until after hydration completes - Prevent infinite loops by debouncing ResizeObserver callbacks - Only update isAtBottom state when value actually changes - Defer scroll-to-bottom button render until after hydration Fixes React error #418 (hydration mismatch) and #185 (max update depth) that occurred when reloading the page after stream completion. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Greptile OverviewGreptile SummaryFixed React 19 hydration mismatches and infinite ResizeObserver loops in the chat panel by adding Key Changes
ImpactThe changes successfully address the reported React errors (#418 hydration mismatch, #185 max update depth) by ensuring:
Confidence Score: 4/5
Important Files ChangedFile Analysis
Sequence DiagramsequenceDiagram
participant Browser
participant ChatMessagesPanel
participant React
participant DOM
participant ResizeObserver
Note over Browser,DOM: Initial Page Load (SSR)
Browser->>React: Render on server
React->>DOM: Generate HTML (isMounted=false)
Note over DOM: suppressHydrationWarning on ScrollArea
Note over Browser,DOM: Client Hydration
Browser->>React: Hydrate client-side
React->>ChatMessagesPanel: useEffect fires
ChatMessagesPanel->>ChatMessagesPanel: setIsMounted(true)
Note over ChatMessagesPanel: isMounted now true
ChatMessagesPanel->>DOM: useLayoutEffect (initial sync)
DOM->>ChatMessagesPanel: scrollToBottom("auto")
ChatMessagesPanel->>ChatMessagesPanel: setIsAtBottom(true)
ChatMessagesPanel->>ResizeObserver: Observe contentRef
Note over Browser,DOM: User Interaction
Browser->>ChatMessagesPanel: scroll event
ChatMessagesPanel->>ChatMessagesPanel: computeIsAtBottom()
ChatMessagesPanel->>ChatMessagesPanel: setIsAtBottom (only if changed)
Note over Browser,DOM: Content Resize (streaming)
DOM->>ResizeObserver: Content size changed
ResizeObserver->>ChatMessagesPanel: Callback triggered
alt isResizingRef.current === true
ChatMessagesPanel->>ChatMessagesPanel: Skip (debounce)
else isResizingRef.current === false
ChatMessagesPanel->>ChatMessagesPanel: Set isResizingRef = true
ChatMessagesPanel->>DOM: requestAnimationFrame
DOM->>ChatMessagesPanel: scrollToBottom("smooth")
ChatMessagesPanel->>ChatMessagesPanel: setTimeout(100ms)
ChatMessagesPanel->>ChatMessagesPanel: Reset isResizingRef = false
end
Note over Browser,DOM: Scroll to Bottom Button
alt isMounted && !isAtBottom && hasMessages
ChatMessagesPanel->>DOM: Render button
else
ChatMessagesPanel->>DOM: Skip render (prevent hydration mismatch)
end
|
| const atBottom = computeIsAtBottom(node); | ||
| setIsAtBottom(atBottom); | ||
| // Only update state if value actually changed to prevent unnecessary re-renders | ||
| setIsAtBottom((prev) => (prev !== atBottom ? atBottom : prev)); |
There was a problem hiding this comment.
style: Logic issue: returns prev when values match, preventing update
When prev === atBottom, this returns prev instead of atBottom. While the values are equal, returning the same reference is correct for preventing re-renders, but the ternary operator logic reads backwards. Consider:
| setIsAtBottom((prev) => (prev !== atBottom ? atBottom : prev)); | |
| setIsAtBottom((prev) => (prev === atBottom ? prev : atBottom)); |
This makes the intent clearer: "if unchanged, return prev; otherwise update to new value"
Confidence: 3/5 - The current code works correctly, but the reversed condition (prev !== atBottom ? atBottom : prev) is confusing and error-prone for future maintenance.
…lare Insights The previous fix (#432) addressed some React 19 hydration issues but missed the root cause of the infinite loop (error #185): the virtualizer.measureElement callback creates a new function reference on every render, triggering re-measurements that cause state updates, which causes more re-renders. Changes: - Wrap virtualizer.measureElement in useCallback to create a stable ref callback - Guard measurement with isMounted check to skip during hydration - Disable virtualization entirely until after mount (isMounted && messages.length > 20) - Add Cloudflare Insights to CSP script-src to fix blocked beacon.min.js 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
…lare Insights (#433) The previous fix (#432) addressed some React 19 hydration issues but missed the root cause of the infinite loop (error #185): the virtualizer.measureElement callback creates a new function reference on every render, triggering re-measurements that cause state updates, which causes more re-renders. Changes: - Wrap virtualizer.measureElement in useCallback to create a stable ref callback - Guard measurement with isMounted check to skip during hydration - Disable virtualization entirely until after mount (isMounted && messages.length > 20) - Add Cloudflare Insights to CSP script-src to fix blocked beacon.min.js 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <[email protected]>
Summary
Changes
isMountedstate to defer hydration-sensitive operations until after client hydrationsuppressHydrationWarningto ScrollArea componentsisResizingRefisAtBottomstate when value actually changes (prevents unnecessary re-renders)Test plan
🤖 Generated with Claude Code