Repository navigation
fix(web): prevent React hydration errors #418 and #185 - #438
Conversation
- Fix virtualListContainerStyle useMemo in chat-messages-panel.tsx and app-sidebar.tsx - Use computed totalSize value as dependency instead of function call - Prevents infinite re-renders from dependency array function calls - Fix useChatReadStatus hydration mismatch - Use requestAnimationFrame to delay localStorage access until after hydration - Add hasMountedRef to ensure localStorage operations only happen client-side - Return stable empty values during SSR to match server render
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Greptile OverviewGreptile SummaryThis PR fixes React hydration errors (#418) and maximum update depth issues (#185) by stabilizing dependency arrays and deferring client-side operations until after hydration. Key Changes:
Issues Found:
Confidence Score: 4/5
Important Files ChangedFile Analysis
Sequence DiagramsequenceDiagram
participant Browser
participant React
participant Component
participant Virtualizer
participant LocalStorage
Note over Browser,LocalStorage: Initial SSR/Hydration Phase
React->>Component: Server render
Component->>Component: Initialize with empty values
Component->>React: Return SSR HTML
Browser->>React: Load client bundle
React->>Component: Hydrate (client render)
Note over Component: useChatReadStatus hook
Component->>Component: hasMountedRef = false
Component->>Component: isLoaded = false
Component->>React: Return empty values (matches SSR)
React->>Component: useEffect runs
Component->>Component: Set hasMountedRef = true
Component->>Browser: requestAnimationFrame()
Note over Browser,LocalStorage: After Hydration (RAF Callback)
Browser->>Component: RAF callback executes
Component->>LocalStorage: getItem(INIT_TIME_KEY)
LocalStorage-->>Component: Return init time or null
Component->>LocalStorage: getItem(STORAGE_KEY)
LocalStorage-->>Component: Return read times
Component->>Component: setReadTimes(parsed data)
Component->>Component: setIsLoaded(true)
Note over Component,Virtualizer: Virtualizer Fix
Component->>Virtualizer: getTotalSize()
Virtualizer-->>Component: Return size value
Component->>Component: useMemo([totalSize])
Note over Component: Stable dependency prevents<br/>infinite re-renders
Note over Browser,Component: User Navigates to Chat
Browser->>Component: Pathname changes
Component->>Component: useEffect detects change
Component->>Component: markAsRead(chatId)
Component->>LocalStorage: setItem(STORAGE_KEY, updated times)
|
| useEffect(() => { | ||
| if (typeof window === "undefined") return; | ||
| // Skip if already loaded or during SSR | ||
| if (hasMountedRef.current) return; | ||
| hasMountedRef.current = true; |
There was a problem hiding this comment.
style: The check on line 35 is redundant since hasMountedRef.current can only be true inside this useEffect after it's set on line 36.
The real issue: the dependency array is empty [], so this effect only runs once. But the guard if (hasMountedRef.current) return; on line 35 means the effect will ALWAYS execute its logic on first run (since hasMountedRef.current starts as false). The early return never triggers.
| useEffect(() => { | |
| if (typeof window === "undefined") return; | |
| // Skip if already loaded or during SSR | |
| if (hasMountedRef.current) return; | |
| hasMountedRef.current = true; | |
| useEffect(() => { | |
| // Only run once on mount | |
| if (hasMountedRef.current) return; | |
| hasMountedRef.current = true; |
Score: 2/5 - This is more of a code clarity issue than a bug, but the comment "Skip if already loaded" is misleading since we're checking hasMountedRef not isLoaded.
| const rafId = requestAnimationFrame(() => { | ||
| // Get or set the initialization time (when tracking first started) | ||
| let initTime = 0; | ||
| try { | ||
| const storedInitTime = localStorage.getItem(INIT_TIME_KEY); | ||
| if (storedInitTime) { | ||
| initTime = Number.parseInt(storedInitTime, 10); | ||
| } else { | ||
| // First time using this feature - set init time to now | ||
| // This means existing chats won't show as unread | ||
| initTime = Date.now(); | ||
| localStorage.setItem(INIT_TIME_KEY, String(initTime)); | ||
| } | ||
| } catch { | ||
| initTime = Date.now(); | ||
| localStorage.setItem(INIT_TIME_KEY, String(initTime)); | ||
| } | ||
| } catch { | ||
| initTime = Date.now(); | ||
| } | ||
| initTimeRef.current = initTime; | ||
|
|
||
| // Load stored read times | ||
| try { | ||
| const stored = localStorage.getItem(STORAGE_KEY); | ||
| if (stored) { | ||
| setReadTimes(JSON.parse(stored)); | ||
| initTimeRef.current = initTime; | ||
|
|
||
| // Load stored read times | ||
| try { | ||
| const stored = localStorage.getItem(STORAGE_KEY); | ||
| if (stored) { | ||
| const parsed = JSON.parse(stored); | ||
| setReadTimes(parsed); | ||
| } | ||
| } catch { | ||
| // Ignore parse errors | ||
| } | ||
| } catch { | ||
| // Ignore parse errors | ||
| } | ||
| setIsLoaded(true); | ||
| setIsLoaded(true); | ||
| }); |
There was a problem hiding this comment.
logic: Potential race condition: if the component unmounts quickly (before RAF callback fires), the RAF callback will still execute and call setReadTimes and setIsLoaded on an unmounted component.
The cleanup function cancelAnimationFrame(rafId) won't prevent this because rafId is only assigned AFTER requestAnimationFrame returns, but the function passed to requestAnimationFrame captures the initial undefined value.
| const rafId = requestAnimationFrame(() => { | |
| // Get or set the initialization time (when tracking first started) | |
| let initTime = 0; | |
| try { | |
| const storedInitTime = localStorage.getItem(INIT_TIME_KEY); | |
| if (storedInitTime) { | |
| initTime = Number.parseInt(storedInitTime, 10); | |
| } else { | |
| // First time using this feature - set init time to now | |
| // This means existing chats won't show as unread | |
| initTime = Date.now(); | |
| localStorage.setItem(INIT_TIME_KEY, String(initTime)); | |
| } | |
| } catch { | |
| initTime = Date.now(); | |
| localStorage.setItem(INIT_TIME_KEY, String(initTime)); | |
| } | |
| } catch { | |
| initTime = Date.now(); | |
| } | |
| initTimeRef.current = initTime; | |
| // Load stored read times | |
| try { | |
| const stored = localStorage.getItem(STORAGE_KEY); | |
| if (stored) { | |
| setReadTimes(JSON.parse(stored)); | |
| initTimeRef.current = initTime; | |
| // Load stored read times | |
| try { | |
| const stored = localStorage.getItem(STORAGE_KEY); | |
| if (stored) { | |
| const parsed = JSON.parse(stored); | |
| setReadTimes(parsed); | |
| } | |
| } catch { | |
| // Ignore parse errors | |
| } | |
| } catch { | |
| // Ignore parse errors | |
| } | |
| setIsLoaded(true); | |
| setIsLoaded(true); | |
| }); | |
| const rafId = requestAnimationFrame(() => { | |
| // Check if component is still mounted | |
| if (!hasMountedRef.current) return; | |
| // Get or set the initialization time (when tracking first started) |
Add a check at the start of the RAF callback to ensure we don't update state after unmount.
Score: 3/5 - This could cause React warnings about setting state on unmounted components in fast navigation scenarios.
| const markAsRead = useCallback((chatId: string) => { | ||
| // Don't mark as read until we've loaded (prevents overwriting stored data) | ||
| if (!hasMountedRef.current) return; |
There was a problem hiding this comment.
style: The comment "prevents overwriting stored data" is misleading - the guard prevents marking chats as read before the initial data is loaded.
| const markAsRead = useCallback((chatId: string) => { | |
| // Don't mark as read until we've loaded (prevents overwriting stored data) | |
| if (!hasMountedRef.current) return; | |
| // Don't mark as read until mounted and loaded (ensures we don't race with initial load) | |
| if (!hasMountedRef.current) return; |
Score: 1/5 - Minor comment clarity issue.
Summary
Changes
Fix virtualListContainerStyle useMemo in chat-messages-panel.tsx and app-sidebar.tsx
Fix useChatReadStatus hydration mismatch
Root Cause