Skip to content

Comments

fix FOUC on refresh#5608

Merged
adhami3310 merged 8 commits intomainfrom
lendemor/fix_flicker
Jul 25, 2025
Merged

fix FOUC on refresh#5608
adhami3310 merged 8 commits intomainfrom
lendemor/fix_flicker

Conversation

@Lendemor
Copy link
Contributor

No description provided.

@codspeed-hq
Copy link

codspeed-hq bot commented Jul 21, 2025

CodSpeed Performance Report

Merging #5608 will not alter performance

Comparing lendemor/fix_flicker (2b281fd) with main (c9c4203)

Summary

✅ 8 untouched benchmarks

@Lendemor Lendemor marked this pull request as ready for review July 23, 2025 15:59
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Greptile Summary

This PR implements a comprehensive fix for Flash of Unstyled Content (FOUC) that occurs when users refresh pages with saved theme preferences. The solution involves two coordinated changes:

Backend Changes (reflex/compiler/utils.py): A new preload_color_theme() function is imported and injected as the very first component in the document head. This creates a synchronous JavaScript script that runs immediately during page load, before any React components hydrate, to read the saved theme from localStorage and apply it to prevent visual flashing.

Frontend Changes (reflex/.templates/web/utils/react-theme.js): The ThemeProvider component now uses an isInitialized state flag to prevent unnecessary localStorage writes during the initial theme loading phase. This eliminates the read-write cycle that was occurring on first render when the component loaded the saved theme from localStorage.

The fix follows a standard pattern used by theme management libraries like next-themes, where a blocking script applies themes before React hydration occurs. This ensures users never see the wrong theme flash when refreshing pages. The two-phase approach in the React component (load first, then mark as initialized for future saves) maintains proper theme persistence while avoiding the initial write that was contributing to the FOUC issue.

Confidence score: 4/5

  • This is a well-implemented solution to a common web development problem using established patterns
  • The fix addresses both the cause (late React hydration) and prevents side effects (unnecessary localStorage writes)
  • The reflex/compiler/utils.py file needs attention to ensure the preload_color_theme import and function work correctly

2 files reviewed, no comments

Edit Code Review Bot Settings | Greptile


} catch (e) {
// Fallback to system preference on any error
document.documentElement.className = "system";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. we are removing other class names by setting ours
  2. it seems classname would expect a resolved theme, not the theme preference, but unsure how that makes a difference or how it's being used

@adhami3310 adhami3310 merged commit 9d9ff9e into main Jul 25, 2025
52 of 53 checks passed
@adhami3310 adhami3310 deleted the lendemor/fix_flicker branch July 25, 2025 00:32
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.

3 participants