Skip to content

fix: White flash on language switch in dark mode #101 - #135

Merged
DataDave-Dev merged 3 commits into
DataDave-Dev:mainfrom
rue-eru:main
Jun 22, 2026
Merged

DataDave-Dev merged 3 commits into
DataDave-Dev:mainfrom
rue-eru:main

Conversation

@rue-eru

@rue-eru rue-eru commented Jun 20, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Changes

  • Moved theme control from client-side useEffect to server-side layout
  • Theme is now stored in a cookie
  • Added cookie synchronization on theme toggle

Details

  • Before: the dark class on <html> was managed on the client via useEffect

Now: the theme cookie is immediately set as a className to <html>, so dark class exists from the start and renders on the server

  • Before: When changing the language, React re-renders <html> and resets its className

Now: We have synchronization in ThemeToggle and store the theme in both localStorage and cookies (max-age=31536000 for one year).

  • Before: useEffect activates after page rendering due to being on a client-side, which caused the darkclass to appear with a delay leading to a white screen

Now: useIsDark only reads state, but doesn't overwrite it. The dependency on pathname has been removed.

Testing

I couldn't test locally due to build issues, but the logic follows the root cause analysis. Please review and test on your end.

Files changed

  • src/app/[lang]/layout.tsx : added cookie reading and className for theme
  • src/components/layout/ThemeToggle.tsx : added cookie sync on toggle
  • src/lib/use-theme.ts : removed useEffect and pathname dependency

Closes #101

Summary by CodeRabbit

  • New Features

    • Theme preferences now persist using cookies for improved consistency across sessions.
  • Bug Fixes

    • Eliminated theme flash on initial page load by applying saved preference during server-side rendering.

@vercel

vercel Bot commented Jun 20, 2026

Copy link
Copy Markdown

@rue-eru is attempting to deploy a commit to the davidsupport's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Jun 20, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: abde2351-0f62-4e53-858f-1ec776e2660b

📥 Commits

Reviewing files that changed from the base of the PR and between c47ce4d and 9379a00.

📒 Files selected for processing (1)
  • src/app/[lang]/layout.tsx

📝 Walkthrough

Walkthrough

Theme class ownership is moved from imperative client-side effects to the server. ThemeToggle now writes the selected theme to a cookie on each toggle. The layout server component reads that cookie via cookies() and renders <html className="dark"> directly. useIsDark is reduced to a pure MutationObserver-based reader, removing its routing-driven effect.

Changes

Cookie-based theme persistence

Layer / File(s) Summary
Cookie write in ThemeToggle and simplified useIsDark hook
src/components/layout/ThemeToggle.tsx, src/lib/use-theme.ts
ThemeToggle.toggle writes document.cookie with theme=<value>; path=/; max-age=...; SameSite=Lax after toggling the DOM class. useIsDark drops useEffect, usePathname, and class-initialization logic, retaining only a useSyncExternalStore subscription that returns classList.contains("dark").
Server layout reads theme cookie → <html className>
src/app/[lang]/layout.tsx
Adds cookies to the next/headers import, reads cookies().get("theme"), derives isDark, and passes className={isDark ? "dark" : undefined} to <html>. The x-nonce extraction from request headers is preserved for the inline CSP script.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • DataDave-Dev/weftmap#99: Modifies src/lib/use-theme.ts to add an effect that initializes/restores the dark class — the same file this PR simplifies by removing that effect-based pattern entirely.

Poem

🐇 A cookie now carries the dark,
No flash when the language paths arc.
The server reads "theme",
Paints dark on the seam,
No white blink left — only the mark! 🌙

🚥 Pre-merge checks | ✅ 2 | ❌ 3

❌ Failed checks (2 warnings, 1 inconclusive)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The PR implementation follows the suggested fix from issue #101, moving theme control from client-side useEffect to server-side cookie storage and layout rendering; however, a CSP nonce issue flagged in CodeRabbit review must be addressed. Add the missing nonce attribute to the inline script in src/app/[lang]/layout.tsx by importing and calling headers() to retrieve the x-nonce value and applying it to the script element.
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive The description provides a comprehensive overview with changes, details, testing notes, and affected files, though it does not follow the repository's template structure (which requires language selection, type checkboxes, and build/lint checklist). Align the description format with the template: add language tags, select the fix type checkbox, and include the pnpm lint/typecheck/test/build verification checklist.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically identifies the main issue fixed: eliminating the white flash when switching languages in dark mode, with the linked issue reference #101.
Out of Scope Changes check ✅ Passed All changes directly support the core objective: theme management now server-driven via cookies, ThemeToggle synchronizes to cookies, and useIsDark simplified to read-only mode. No unrelated modifications detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai 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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/app/[lang]/layout.tsx (1)

51-56: ⚠️ Potential issue | 🔴 Critical

Inline script will be blocked by CSP—nonce attribute is missing.

The CSP policy requires script-src 'self' 'nonce-${nonce}' (no 'unsafe-inline'), but the inline theme script at lines 51–56 lacks a nonce attribute. The x-nonce header is set in proxy.ts but never read or applied in the layout. This will cause the script to be blocked on every request, reintroducing theme flicker on cold loads.

Add nonce consumption to the layout: import headers() from "next/server", read x-nonce, and apply it to the script element.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/app/`[lang]/layout.tsx around lines 51 - 56, The inline script element
with dangerouslySetInnerHTML that applies the theme class is missing a nonce
attribute required by your CSP policy. To fix this, import the headers function
from "next/server" at the top of the layout file, then call headers() within the
layout component to retrieve the x-nonce header value. Add the nonce attribute
to the script element by setting nonce={nonce} where nonce is the value
extracted from the headers. This ensures the inline theme script passes the CSP
check and prevents the script from being blocked on every request.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/app/`[lang]/layout.tsx:
- Around line 51-56: The inline script element with dangerouslySetInnerHTML that
applies the theme class is missing a nonce attribute required by your CSP
policy. To fix this, import the headers function from "next/server" at the top
of the layout file, then call headers() within the layout component to retrieve
the x-nonce header value. Add the nonce attribute to the script element by
setting nonce={nonce} where nonce is the value extracted from the headers. This
ensures the inline theme script passes the CSP check and prevents the script
from being blocked on every request.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 66708f0e-a555-48ad-a024-142db1ac8837

📥 Commits

Reviewing files that changed from the base of the PR and between c237c7d and c47ce4d.

📒 Files selected for processing (3)
  • src/app/[lang]/layout.tsx
  • src/components/layout/ThemeToggle.tsx
  • src/lib/use-theme.ts

@rue-eru

rue-eru commented Jun 20, 2026

Copy link
Copy Markdown
Contributor Author

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)

src/app/[lang]/layout.tsx (1)> 51-56: ⚠️ Potential issue | 🔴 Critical

Inline script will be blocked by CSP—nonce attribute is missing.
The CSP policy requires script-src 'self' 'nonce-${nonce}' (no 'unsafe-inline'), but the inline theme script at lines 51–56 lacks a nonce attribute. The x-nonce header is set in proxy.ts but never read or applied in the layout. This will cause the script to be blocked on every request, reintroducing theme flicker on cold loads.
Add nonce consumption to the layout: import headers() from "next/server", read x-nonce, and apply it to the script element.

🤖 Prompt for AI Agents

Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/app/`[lang]/layout.tsx around lines 51 - 56, The inline script element
with dangerouslySetInnerHTML that applies the theme class is missing a nonce
attribute required by your CSP policy. To fix this, import the headers function
from "next/server" at the top of the layout file, then call headers() within the
layout component to retrieve the x-nonce header value. Add the nonce attribute
to the script element by setting nonce={nonce} where nonce is the value
extracted from the headers. This ensures the inline theme script passes the CSP
check and prevents the script from being blocked on every request.

🤖 Prompt for all review comments with AI agents

Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/app/`[lang]/layout.tsx:
- Around line 51-56: The inline script element with dangerouslySetInnerHTML that
applies the theme class is missing a nonce attribute required by your CSP
policy. To fix this, import the headers function from "next/server" at the top
of the layout file, then call headers() within the layout component to retrieve
the x-nonce header value. Add the nonce attribute to the script element by
setting nonce={nonce} where nonce is the value extracted from the headers. This
ensures the inline theme script passes the CSP check and prevents the script
from being blocked on every request.

ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 66708f0e-a555-48ad-a024-142db1ac8837

📥 Commits
Reviewing files that changed from the base of the PR and between c237c7d and c47ce4d.

📒 Files selected for processing (3)

i see, let me fix it then

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.

White flash on language switch in dark mode

2 participants