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

fix: use headers() as fallback for cookie retrieval - #418

Merged
leoisadev1 merged 1 commit into
mainfrom
fix/try-headers-for-cookies
Nov 23, 2025
Merged

leoisadev1 merged 1 commit into
mainfrom
fix/try-headers-for-cookies

Conversation

@leoisadev1

Copy link
Copy Markdown
Member

Try headers().get('cookie') when cookies() returns empty. This helps diagnose if the issue is with the cookies() API specifically.

@vercel

vercel Bot commented Nov 23, 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 23, 2025 7:54pm

@leoisadev1
leoisadev1 merged commit e8153e9 into main Nov 23, 2025
@greptile-apps

greptile-apps Bot commented Nov 23, 2025

Copy link
Copy Markdown
Contributor

Greptile Overview

Greptile Summary

Added headers().get('cookie') as a fallback when cookies() returns empty to diagnose production cookie issues

Critical Issues Found:

  • The fallback logic breaks session caching - when rawCookieHeader is used, the allCookies array remains empty, preventing cache key generation (line 75-77)
  • This defeats the 30-second TTL cache optimization, causing auth API calls on every server component render
  • The fix is incomplete: if rawCookieHeader has value, it should be parsed back into the allCookies array

Confidence Score: 1/5

  • This PR introduces a critical logic bug that breaks session caching when the fallback is triggered
  • The fallback mechanism works for cookie forwarding but completely breaks the cache key lookup logic on lines 75-77, which depends on the allCookies array that remains empty when rawCookieHeader is used. This causes performance degradation (auth API call every request) and defeats the purpose of the 30-second cache.
  • apps/web/src/lib/auth-server.ts needs immediate attention - the cache key logic must be fixed before merge

Important Files Changed

File Analysis

Filename Score Overview
apps/web/src/lib/auth-server.ts 1/5 Added headers().get('cookie') fallback but broke session caching when fallback is used - cache key lookup depends on allCookies array which remains empty

Sequence Diagram

sequenceDiagram
    participant SC as Server Component
    participant RS as resolveUserContext
    participant NH as Next.js headers()
    participant NC as Next.js cookies()
    participant Cache as Session Cache
    participant API as /api/auth/get-session

    SC->>RS: getUserContext()
    RS->>NC: cookies()
    NC-->>RS: allCookies[] (may be empty)
    RS->>NH: headers().get('cookie')
    NH-->>RS: rawCookieHeader (may be null)
    
    alt rawCookieHeader exists
        RS->>RS: cookieHeader = rawCookieHeader
        Note over RS: ⚠️ Bug: allCookies stays empty
    else rawCookieHeader is null
        RS->>RS: cookieHeader = join(allCookies)
    end
    
    RS->>RS: Find session cookie in allCookies
    Note over RS: ⚠️ Bug: sessionCookie undefined<br/>when rawCookieHeader used
    
    alt cacheKey exists
        RS->>Cache: Check cache with sessionCookie.value
        alt Cache hit
            Cache-->>RS: Cached user
            RS-->>SC: UserContext
        else Cache miss
            RS->>API: fetch with cookieHeader
            API-->>RS: User data
            RS->>Cache: Store with cacheKey
            RS-->>SC: UserContext
        end
    else No cacheKey (Bug scenario)
        Note over RS: Cache disabled
        RS->>API: fetch with cookieHeader
        API-->>RS: User data
        Note over RS: Cannot cache without key
        RS-->>SC: UserContext
    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.

Additional Comments (1)

  1. apps/web/src/lib/auth-server.ts, line 75-77 (link)

    logic: when rawCookieHeader has value but cookies() returned empty (the exact scenario this PR tries to fix), allCookies will be empty and sessionCookie will be undefined, disabling caching entirely

    Score: 5/5 - Critical logic flaw that defeats the purpose of the 30-second cache

1 file reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

Comment on lines +63 to 65
const cookieHeader = rawCookieHeader || allCookies
.map((c) => `${c.name}=${c.value}`)
.join("; ");

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.

logic: logic error: when rawCookieHeader is a non-empty string, the allCookies array is never used, breaking the cache key lookup on line 75-77 which depends on allCookies

Suggested change
const cookieHeader = rawCookieHeader || allCookies
.map((c) => `${c.name}=${c.value}`)
.join("; ");
// Prefer raw cookie header if available, fall back to cookies()
const cookieHeader = rawCookieHeader || allCookies
.map((c) => `${c.name}=${c.value}`)
.join("; ");
// IMPORTANT: Parse rawCookieHeader into allCookies if it was used
// so the cache key lookup below can still find session cookies
if (rawCookieHeader && allCookies.length === 0) {
// Parse the cookie header string back into the allCookies array
const parsedCookies = rawCookieHeader.split(";").map(cookie => {
const [name, ...valueParts] = cookie.trim().split("=");
return { name: name.trim(), value: valueParts.join("=") };
});
allCookies.push(...parsedCookies);
}

Score: 5/5 - This will cause session cache to fail when rawCookieHeader is used, forcing repeated auth API calls every request

leoisadev1 added a commit that referenced this pull request Nov 26, 2025
- 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]>
leoisadev1 added a commit that referenced this pull request Nov 26, 2025
…432)

- 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]>
leoisadev1 added a commit that referenced this pull request Nov 27, 2025
React 19's compiler optimizations can eliminate virtualizer.getVirtualItems()
calls when the return value appears unused. This causes the virtualizer to
not render items correctly, leading to infinite re-render loops (error #185)
and hydration mismatches (error #418).

Fix: Store virtualizer in a ref and access through virtualizerRef.current
to prevent React 19 from optimizing away the virtualizer state updates.

Reference: TanStack/virtual#743

🤖 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 27, 2025
React 19's compiler optimizations can eliminate virtualizer.getVirtualItems()
calls when the return value appears unused. This causes the virtualizer to
not render items correctly, leading to infinite re-render loops (error #185)
and hydration mismatches (error #418).

Fix: Store virtualizer in a ref and access through virtualizerRef.current
to prevent React 19 from optimizing away the virtualizer state updates.

Reference: TanStack/virtual#743

🤖 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 27, 2025
EMERGENCY FIX: The @tanstack/react-virtual library causes infinite re-render
loops during hydration in React 19, even with the virtualizerRef workaround
from PR #434. The error occurs INSIDE the library code, not in our wrapper.

This completely disables virtualization until @tanstack/react-virtual
officially supports React 19.

References:
- TanStack/virtual#743
- TanStack/virtual#924

🤖 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 27, 2025
…435)

EMERGENCY FIX: The @tanstack/react-virtual library causes infinite re-render
loops during hydration in React 19, even with the virtualizerRef workaround
from PR #434. The error occurs INSIDE the library code, not in our wrapper.

This completely disables virtualization until @tanstack/react-virtual
officially supports React 19.

References:
- TanStack/virtual#743
- TanStack/virtual#924

🤖 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 27, 2025
- 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
leoisadev1 added a commit that referenced this pull request Nov 27, 2025
- 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
leoisadev1 added a commit that referenced this pull request Nov 27, 2025
… 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)
leoisadev1 added a commit that referenced this pull request Nov 27, 2025
… errors (#439)

- 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)
@leoisadev1
leoisadev1 deleted the fix/try-headers-for-cookies branch January 20, 2026 22:26

This branch was successfully deployed

1 active deployment
Preview — 8aae4dcd Deployed Nov 23, 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