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

fix: stabilize virtualizer ref for React 19 and allow Cloudflare Insights - #433

Merged
leoisadev1 merged 1 commit into
mainfrom
fix/react-19-virtualizer-stability
Nov 26, 2025
Merged

leoisadev1 merged 1 commit into
mainfrom
fix/react-19-virtualizer-stability

Conversation

@leoisadev1

Copy link
Copy Markdown
Member

Summary

  • Create stable measureElementRef callback to prevent infinite re-renders from virtualizer.measureElement
  • Disable virtualization until after mount to prevent hydration mismatch
  • Add https://static.cloudflareinsights.com to CSP script-src

Problem

After Next.js 16 migration, React errors #418 (hydration mismatch) and #185 (max update depth exceeded) still occur on page reload. The root cause was the virtualizer's measureElement callback creating new function references on every render, which:

  1. Triggers element measurements → state updates → re-render → infinite loop
  2. Causes hydration mismatches when measurements run before hydration completes

Solution

  1. Stable ref callback: Wrap virtualizer.measureElement in useCallback with isMounted guard
  2. Deferred virtualization: Only enable virtualization after component mounts (isMounted && messages.length > 20)
  3. CSP fix: Allow Cloudflare Insights beacon script

Test plan

  • Reload chat page with messages - no React errors in console
  • Scroll through long chat with 20+ messages - virtualization works correctly
  • Verify Cloudflare Insights loads without CSP errors
  • Stream a new response and reload mid-stream - handles reconnection gracefully

🤖 Generated with Claude Code

…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]>
@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:16pm

@leoisadev1
leoisadev1 merged commit 580a61f into main Nov 26, 2025
@greptile-apps

greptile-apps Bot commented Nov 26, 2025

Copy link
Copy Markdown
Contributor

Greptile Overview

Greptile Summary

Fixes React 19 hydration mismatch and infinite re-render errors by stabilizing the virtualizer ref callback and deferring virtualization until after mount. Also adds Cloudflare Insights to CSP whitelist.

Key Changes:

  • Wrapped virtualizer.measureElement in stable useCallback to prevent infinite re-renders caused by unstable ref callback
  • Deferred virtualization with isMounted && messages.length > 20 condition to prevent hydration mismatches
  • Added https://static.cloudflareinsights.com to CSP script-src directive

Analysis:
The root cause analysis is accurate - passing virtualizer.measureElement directly as a ref prop creates a new function reference on every render, triggering measurements → state updates → re-render cycle. The solution correctly wraps it in useCallback with appropriate dependencies (virtualizer and isMounted).

The isMounted guard pattern effectively prevents hydration mismatches by rendering non-virtualized content on the server and during initial client render, then switching to virtualization after mount.

Note: The same virtualizer pattern is used in apps/web/src/components/app-sidebar.tsx:613 without this fix, which may need the same treatment if similar issues arise there.

Confidence Score: 4/5

  • This PR is safe to merge with minimal risk - it addresses legitimate React 19 compatibility issues with a well-reasoned solution
  • Score of 4/5 reflects: (1) correct identification and fix of React 19 virtualizer ref stability issue, (2) appropriate hydration mismatch prevention via deferred virtualization, (3) straightforward CSP addition for Cloudflare Insights. Minor concern: the virtualizer dependency in useCallback is included, which is necessary but worth monitoring - the virtualizer object should be stable per @tanstack/react-virtual design, but if it changes unexpectedly, could cause issues. Also, app-sidebar.tsx uses the same pattern without this fix and may need updating.
  • Pay attention to apps/web/src/components/app-sidebar.tsx which uses the same virtualizer.measureElement pattern without the stable callback wrapper - consider applying the same fix if similar issues occur

Important Files Changed

File Analysis

Filename Score Overview
apps/web/src/components/chat-messages-panel.tsx 4/5 Added stable measureElementRef callback and deferred virtualization to fix React 19 hydration mismatch and infinite re-render issues
apps/web/src/proxy.ts 5/5 Added https://static.cloudflareinsights.com to CSP script-src whitelist to allow Cloudflare Insights beacon

Sequence Diagram

sequenceDiagram
    participant Browser
    participant React
    participant ChatPanel
    participant Virtualizer
    participant DOM

    Browser->>React: Initial page load
    React->>ChatPanel: Render (isMounted = false)
    Note over ChatPanel: shouldVirtualize = false<br/>(prevents hydration mismatch)
    ChatPanel->>DOM: Render non-virtualized list
    React->>DOM: Hydrate
    
    DOM->>ChatPanel: useEffect fires
    ChatPanel->>ChatPanel: setIsMounted(true)
    
    React->>ChatPanel: Re-render (isMounted = true)
    Note over ChatPanel: shouldVirtualize = true<br/>(messages.length > 20)
    ChatPanel->>Virtualizer: Initialize with enabled=true
    
    Virtualizer->>ChatPanel: Return virtualizer instance
    ChatPanel->>ChatPanel: Create stable measureElementRef<br/>via useCallback
    
    ChatPanel->>DOM: Render virtualized items
    DOM->>ChatPanel: Call measureElementRef for each item
    ChatPanel->>Virtualizer: measureElement(node)
    Virtualizer->>Virtualizer: Update measurements
    Note over Virtualizer: Stable callback prevents<br/>infinite re-render loop
    
    Browser->>ChatPanel: User scrolls
    Virtualizer->>Virtualizer: Update scroll position
    Virtualizer->>ChatPanel: Trigger re-render with new items
    ChatPanel->>DOM: Render visible items
    Note over ChatPanel,DOM: measureElementRef remains stable<br/>no infinite loop
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.

2 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

This branch was successfully deployed

1 active deployment
Preview — 38ad10a3 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