Skip to content

fix(viewer): apply saved theme before first paint to stop dark-mode flash#10

Merged
MattNotarangelo merged 1 commit into
mainfrom
fix/theme-label-fouc
Jun 30, 2026
Merged

fix(viewer): apply saved theme before first paint to stop dark-mode flash#10
MattNotarangelo merged 1 commit into
mainfrom
fix/theme-label-fouc

Conversation

@MattNotarangelo

@MattNotarangelo MattNotarangelo commented Jun 30, 2026

Copy link
Copy Markdown
Owner

Problem

On reload, a light-mode user saw the whole page flash dark before snapping to light. The theme was only applied by the deferred app bundle (main.ts), which runs after first paint, while the CSS :root default is dark.

Fix

Apply the saved theme to <html> before first paint, from a tiny same-origin script (public/theme-init.js) loaded as a blocking <script> in <head> — ahead of the deferred bundle. main.ts still owns the theme at runtime; this only sets the initial attribute.

  • No CSP change: script-src 'self' already covers a same-origin file, so there's no inline-script hash to maintain.
  • Total change: 3 lines in index.html + one ~0.5 KB static file.

Scoped deliberately to the jarring full-page flash. The minor button-label and disabled-state flashes are left out.

Test plan

  • npm run build passes (tsc --noEmit + Vite); theme-init.js copied to dist/, path rewritten for base: './'
  • Browser (vite preview): with cv.theme=light, reload → data-theme="light" and light background on first paint
  • Zero console/CSP errors (external script allowed by 'self')

🤖 Generated with Claude Code

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jun 30, 2026

Copy link
Copy Markdown

Deploying caveviewer with  Cloudflare Pages  Cloudflare Pages

Latest commit: 82a4a60
Status: ✅  Deploy successful!
Preview URL: https://963eb0b9.caveviewer.pages.dev
Branch Preview URL: https://fix-theme-label-fouc.caveviewer.pages.dev

View logs

…lash

A light-mode reload briefly painted the default dark theme because the
theme was only applied by the deferred app bundle, after first paint.

Apply the saved theme to <html> from a tiny same-origin script loaded
blocking in <head>, before the bundle. No CSP change needed: script-src
'self' already covers it.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@MattNotarangelo MattNotarangelo changed the title fix(viewer): apply persisted theme & labels before first paint fix(viewer): apply saved theme before first paint to stop dark-mode flash Jun 30, 2026
@MattNotarangelo MattNotarangelo merged commit 8cf76f3 into main Jun 30, 2026
2 checks passed
@MattNotarangelo MattNotarangelo deleted the fix/theme-label-fouc branch June 30, 2026 23:11
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.

1 participant