fix(frontend): guard against SSR/hydration mismatch in theme provider - #1158
Open
omoh5 wants to merge 1 commit into
Open
fix(frontend): guard against SSR/hydration mismatch in theme provider#1158omoh5 wants to merge 1 commit into
omoh5 wants to merge 1 commit into
Conversation
…LabsCrypt#1083) Add blocking inline script in <head> that reads localStorage theme preference before first paint, preventing flash-of-wrong-theme when client theme differs from server-rendered default. Add comprehensive JSDoc comments documenting the SSR/hydration strategy in both theme-provider.tsx and layout.tsx.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #1083
This PR adds a blocking inline script that reads the user's stored theme preference from
localStorageand applies thedarkclass to<html>before the browser paints a single pixel. This prevents the flash-of-wrong-theme (FART) when the server renders a default theme class that differs from the user's persisted preference.Problem
theme-provider.tsxwrapsnext-themes'ThemeProvider, which runs as a client component. In Next.js 16 + React 19, the script injected bynext-themesvia portal may not be truly blocking — it runs after React hydrates, causing:lightand the server rendersdark, the browser paints dark first, then swaps to light.darkclass on<html>than what the server produced.Solution
Two-part approach inside
layout.tsx:<script>in<head>— readsflowfi-themefrom localStorage and synchronously adds/removes thedarkclass on<html>before the first paint. Handlesdark,light, andsystem(viamatchMedia) preferences. No flicker occurs because the correct class is present from the very first rendered frame.suppressHydrationWarningon<html>(already present) — tells React to skip the hydration-difference check for this element because we intentionally mutate its class list before hydration.Added comprehensive documentation:
theme-provider.tsx: Full JSDoc block explaining the SSR/hydration strategy and all configuration options.layout.tsx: Inline comments explaining both thesuppressHydrationWarningand the blocking script.Files Changed
frontend/src/context/theme-provider.tsxfrontend/src/app/layout.tsx<script>in<head>; updated commentsTesting
tsc --noEmit— no new errorsManual Verification
To reproduce the original issue:
lightin settings