Skip to content

Fix theme toggle button not working due to SSR hydration mismatch#208

Merged
amansrivastava merged 3 commits intomainfrom
copilot/fix-207
Aug 16, 2025
Merged

Fix theme toggle button not working due to SSR hydration mismatch#208
amansrivastava merged 3 commits intomainfrom
copilot/fix-207

Conversation

Copy link
Contributor

Copilot AI commented Aug 16, 2025

The theme mode toggle button in the header dropdown was not working due to a Next.js server-side rendering (SSR) hydration mismatch in the ThemeContext component.

Problem

The ThemeContext was accessing localStorage during the initial render without checking if it was running on the client side. This caused hydration mismatches between what the server rendered and what the client expected, preventing the theme toggle functionality from working properly.

Solution

Updated the ThemeContext to properly handle SSR by:

  1. Added client-side safety checks: Wrapped all localStorage access with typeof window !== 'undefined' checks
  2. Implemented mounted state: Added a mounted state to prevent theme application until the component has hydrated on the client
  3. Delayed theme initialization: Theme detection and application now only occurs after the component has mounted

Changes

// Before: Direct localStorage access during SSR
const savedTheme = localStorage.getItem('theme') as Theme;

// After: SSR-safe localStorage access
if (typeof window !== 'undefined') {
  const savedTheme = localStorage.getItem('theme') as Theme;
  // ...
}

Testing

The fix has been verified to work correctly:

  • ✅ Theme toggle switches between light and dark modes
  • ✅ Visual changes apply properly (backgrounds, text colors, UI elements)
  • ✅ Theme preference persists across page refreshes
  • ✅ No hydration warnings or console errors
  • ✅ SSR-compatible implementation

Screenshots

Light Mode:
Light Mode

Dark Mode:
Dark Mode

The theme mode button in the header dropdown now works as expected for all authenticated users, allowing seamless switching between light and dark themes.

Fixes #207.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: credevator <2756303+credevator@users.noreply.github.com>
Copilot AI changed the title [WIP] [Frontend Bug]: Fix theme toggle button not working due to SSR hydration mismatch Aug 16, 2025
Copilot AI requested a review from credevator August 16, 2025 06:23
amansrivastava
amansrivastava previously approved these changes Aug 16, 2025
@amansrivastava amansrivastava marked this pull request as ready for review August 16, 2025 06:24
@amansrivastava amansrivastava merged commit dfa142b into main Aug 16, 2025
1 check failed
@amansrivastava amansrivastava deleted the copilot/fix-207 branch August 16, 2025 06:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Frontend Bug]:

3 participants