Skip to content
This repository was archived by the owner on Sep 16, 2026. It is now read-only.

fix: resolve React hydration and infinite loop errors in chat panel - #432

Merged
leoisadev1 merged 1 commit into
mainfrom
fix/react-hydration-errors
Nov 26, 2025
Merged

leoisadev1 merged 1 commit into
mainfrom
fix/react-hydration-errors

Conversation

@leoisadev1

Copy link
Copy Markdown
Member

Summary

Changes

  • Add isMounted state to defer hydration-sensitive operations until after client hydration
  • Add suppressHydrationWarning to ScrollArea components
  • Guard all scroll/resize effects until after hydration completes
  • Prevent infinite loops by debouncing ResizeObserver callbacks with isResizingRef
  • Only update isAtBottom state when value actually changes (prevents unnecessary re-renders)
  • Defer scroll-to-bottom button render until after hydration

Test plan

  • Build passes locally
  • Verify chat page loads without errors on page reload
  • Verify streaming works correctly
  • Verify scroll-to-bottom button appears correctly

🤖 Generated with Claude Code

- 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]>
@vercel

vercel Bot commented Nov 26, 2025

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
openchat-web Building Building Preview Comment Nov 26, 2025 10:03pm

@leoisadev1
leoisadev1 merged commit 65f0f83 into main Nov 26, 2025
@greptile-apps

greptile-apps Bot commented Nov 26, 2025

Copy link
Copy Markdown
Contributor

Greptile Overview

Greptile Summary

Fixed React 19 hydration mismatches and infinite ResizeObserver loops in the chat panel by adding isMounted state guards and isResizingRef debouncing.

Key Changes

  • Added isMounted state to defer all scroll/resize effects until after client-side hydration completes
  • Added suppressHydrationWarning to ScrollArea components that have scroll-dependent behavior
  • Implemented isResizingRef flag with 100ms debounce to prevent infinite ResizeObserver callback loops
  • Optimized setIsAtBottom to only update when value changes, preventing unnecessary re-renders
  • Deferred scroll-to-bottom button render until after hydration (isMounted && !isAtBottom && hasMessages)

Impact

The changes successfully address the reported React errors (#418 hydration mismatch, #185 max update depth) by ensuring:

  1. Server-rendered HTML matches initial client render (hydration safety)
  2. ResizeObserver callbacks don't trigger infinite update loops
  3. State updates only occur when values actually change

Confidence Score: 4/5

  • This PR is safe to merge with minimal risk - the hydration fixes are well-implemented and address real React 19 issues
  • Score of 4 reflects solid implementation of hydration guards and infinite loop prevention. The isMounted pattern is a standard React solution for hydration mismatches, and the isResizingRef debouncing correctly prevents ResizeObserver loops. One minor style improvement suggested for ternary logic clarity, but no functional issues found. Not a 5 because the PR lacks automated tests to verify the fixes work as intended.
  • No files require special attention - the changes are focused and well-contained in chat-messages-panel.tsx

Important Files Changed

File Analysis

Filename Score Overview
apps/web/src/components/chat-messages-panel.tsx 4/5 Added hydration guards and infinite loop prevention for React 19 compatibility. One minor style suggestion on ternary logic clarity.

Sequence Diagram

sequenceDiagram
    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
Loading

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 file reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

const atBottom = computeIsAtBottom(node);
setIsAtBottom(atBottom);
// Only update state if value actually changed to prevent unnecessary re-renders
setIsAtBottom((prev) => (prev !== atBottom ? atBottom : prev));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Suggested change
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.

leoisadev1 added a commit that referenced this pull request Nov 26, 2025
…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]>
leoisadev1 added a commit that referenced this pull request Nov 26, 2025
…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]>
@leoisadev1
leoisadev1 deleted the fix/react-hydration-errors branch January 20, 2026 22:26

This branch was successfully deployed

1 active deployment
Preview — 50028f16 Deployed Nov 26, 2025 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant