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

fix(web): defer AppSidebar render and remove virtualizer to fix React errors #418/#185 - #439

Merged
leoisadev1 merged 1 commit into
mainfrom
fix/hydration-errors-v3
Nov 27, 2025
Merged

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

Conversation

@leoisadev1

Copy link
Copy Markdown
Member

Summary

Changes

1. Defer AppSidebar rendering (dashboard-layout-inner.tsx)

  • AppSidebar uses hooks that cause SSR/client mismatch:
    • `useChatReadStatus` - accesses localStorage
    • `useChatList` - Convex real-time subscription
    • `useTheme` - next-themes SSR issues
  • Now renders placeholder during SSR, real sidebar after mount
  • Prevents hydration mismatch errors

2. Remove @tanstack/react-virtual (chat-messages-panel.tsx)

Root Cause

The errors occur because:

  1. Server renders HTML without client-side state (localStorage, Convex subscriptions)
  2. Client hydrates with different state, causing mismatch (fix: use headers() as fallback for cookie retrieval #418)
  3. The virtualizer library triggers cascading re-renders during hydration (fix(web): improve accessibility #185)

… errors

- Defer AppSidebar rendering until after client mount in dashboard-layout-inner.tsx
  - AppSidebar uses hooks that access localStorage (useChatReadStatus) and
    Convex real-time data (useChatList) which causes hydration mismatches
  - Show placeholder div during SSR to prevent layout shift

- Remove @tanstack/react-virtual from chat-messages-panel.tsx entirely
  - The library causes infinite re-render loops (error #185) and
    hydration mismatches (#418) with React 19
  - Virtualization was already disabled (shouldVirtualize = false)
  - Now the code and dependency are completely removed

Fixes React errors #418 (hydration mismatch) and #185 (max update depth)
@vercel

vercel Bot commented Nov 27, 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 Ready Ready Preview Comment Nov 27, 2025 11:42pm

@greptile-apps

greptile-apps Bot commented Nov 27, 2025

Copy link
Copy Markdown
Contributor

Greptile Overview

Greptile Summary

Fixed React 19 hydration errors (#418) and infinite re-render issues (#185) by deferring AppSidebar render and removing @tanstack/react-virtual from chat messages.

Key Changes:

  • dashboard-layout-inner.tsx: Deferred AppSidebar rendering until client mount using isMounted state. Added placeholder div to prevent layout shift during SSR. This prevents hydration mismatches from localStorage access (useChatReadStatus) and Convex real-time subscriptions (useChatList).
  • chat-messages-panel.tsx: Completely removed @tanstack/react-virtual implementation (80+ lines). All messages now render directly without virtualization, eliminating React 19 cascading re-render loops.

Observations:

  • @tanstack/react-virtual still used in app-sidebar.tsx for chat list virtualization (>100 chats). This creates architectural inconsistency - same library is safe in sidebar but removed from messages panel.
  • Placeholder div relies on --sb-width CSS variable. Should verify variable is defined or add fallback width.
  • Removing virtualization may cause performance issues with very long chat histories (100+ messages), but fixes critical production errors.

Confidence Score: 4/5

  • This PR is safe to merge with minimal risk - fixes critical production errors with well-documented workarounds
  • Score reflects effective fixes for critical hydration errors using standard React SSR patterns (deferred rendering, client-only hooks). Minor concerns: (1) architectural inconsistency with @tanstack/react-virtual still in sidebar but removed from messages, (2) CSS variable dependency without fallback, (3) potential performance regression with long message lists. These are non-critical and can be addressed in follow-ups.
  • Verify --sb-width CSS variable is defined in dashboard-layout-inner.tsx:65. Consider documenting why virtualization is safe in sidebar (app-sidebar.tsx:558) but removed from messages panel.

Important Files Changed

File Analysis

Filename Score Overview
apps/web/src/components/dashboard-layout-inner.tsx 4/5 Defers AppSidebar rendering until client mount to prevent hydration mismatches. Added placeholder div to prevent layout shift during SSR.
apps/web/src/components/chat-messages-panel.tsx 3/5 Removed @tanstack/react-virtual implementation completely. All virtualization code and related state removed, rendering all messages directly.

Sequence Diagram

sequenceDiagram
    participant Browser
    participant Server as Next.js SSR
    participant Client as React Hydration
    participant AppSidebar
    participant ChatPanel as ChatMessagesPanel
    participant LocalStorage
    participant Convex

    Note over Browser,Server: Initial Page Load (SSR)
    Browser->>Server: Request /dashboard/chat/:id
    Server->>Server: Render dashboard-layout-inner
    Note over Server: isMounted = false (SSR)
    Server->>Server: Render placeholder div<br/>(w-[var(--sb-width)] bg-sidebar)
    Note over Server: Skip AppSidebar render
    Server->>Server: Render ChatMessagesPanel
    Note over Server: No virtualization code executed
    Server-->>Browser: Return HTML with placeholder

    Note over Browser,Client: Client-Side Hydration
    Browser->>Client: Start React hydration
    Client->>Client: useEffect triggers
    Client->>Client: setIsMounted(true)
    
    Note over Client,AppSidebar: AppSidebar Mount (Post-Hydration)
    Client->>AppSidebar: Render AppSidebar (isMounted=true)
    AppSidebar->>LocalStorage: Load chat read times<br/>(useChatReadStatus)
    LocalStorage-->>AppSidebar: Return read times
    AppSidebar->>Convex: Subscribe to chat list<br/>(useChatList)
    Convex-->>AppSidebar: Return real-time chats
    AppSidebar->>Client: Render complete sidebar

    Note over Client,ChatPanel: ChatPanel Render
    Client->>ChatPanel: Render messages
    Note over ChatPanel: No virtualizer initialization
    ChatPanel->>ChatPanel: Map over messages array directly
    ChatPanel->>Client: Render all messages

    Note over Browser,Convex: Runtime Updates
    Convex->>AppSidebar: Push streaming status update
    AppSidebar->>AppSidebar: Update chat indicator
    Browser->>LocalStorage: User views chat
    LocalStorage->>AppSidebar: Update read status
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, 2 comments

Edit Code Review Agent Settings | Greptile

Comment on lines +61 to +66
{isMounted ? (
<AppSidebar initialChats={chats} />
) : (
// Placeholder to prevent layout shift - matches sidebar width
<div className="w-[var(--sb-width)] h-full bg-sidebar" />
)}

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: check that --sb-width CSS variable is defined. placeholder width depends on this variable matching actual sidebar width to prevent layout shift during SSR. consider adding fallback width like w-64 (256px) if variable isn't set.

Suggested change
{isMounted ? (
<AppSidebar initialChats={chats} />
) : (
// Placeholder to prevent layout shift - matches sidebar width
<div className="w-[var(--sb-width)] h-full bg-sidebar" />
)}
) : (
// Placeholder to prevent layout shift - matches sidebar width
<div className="w-64 h-full bg-sidebar" style={{ width: 'var(--sb-width, 256px)' }} />
)}

Score: 3/5 - likely fine if CSS variable exists, but fallback would be safer

Comment on lines +15 to +16
// NOTE: useVirtualizer removed - disabled due to React 19 hydration issues
// See: https://github.com/TanStack/virtual/issues/743

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: @tanstack/react-virtual still imported in app-sidebar.tsx:15 for chat list virtualization (>100 chats). consider removing package dependency entirely or documenting why it's safe in sidebar but not here. inconsistency could cause confusion.

Score: 2/5 - not critical but creates architectural inconsistency

@leoisadev1
leoisadev1 merged commit 81a8a8f into main Nov 27, 2025
@leoisadev1
leoisadev1 deleted the fix/hydration-errors-v3 branch November 27, 2025 23:43

This branch was successfully deployed

1 active deployment
Preview — da72b5d0 Deployed Nov 27, 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