Skip to content

fix: stop the theme script failing on every page in production - #9

Merged
playlikeboy merged 1 commit into
mainfrom
fix/theme-script-on-workers
Nov 12, 2026
Merged

playlikeboy merged 1 commit into
mainfrom
fix/theme-script-on-workers

Conversation

@playlikeboy

Copy link
Copy Markdown
Owner

Problem

Every page on production logs ReferenceError: __name is not defined from an inline script. It predates the recent feature work.

Cause

Wrangler bundles the Worker with esbuild's keep-names option on by default, which wraps functions in a __name(...) helper. next-themes copies its theme function's source into an inline <script> so the theme is set before first paint. The helper call is copied with it, and the helper does not exist in the browser. The script throws, and the theme is applied only after hydration, which dark-mode users see as a flash.

Fix

"keep_names": false in wrangler.jsonc. Nothing in the Worker reads function or class names at run time, and the app's error classes set name explicitly.

Risk and rollback

This changes how the Worker is bundled, which CI does not exercise (CI builds with next build, not the Worker bundle). After deploy I will check the page error is gone and that sign-in start, the API and photo search still respond. Rollback is wrangler rollback to the previous version.

Test plan

  • CI (tests, lint, typecheck, build, smoke, end-to-end)
  • After deploy: no page errors on /app; /api/auth/me, sign-in start, /api/workspaces (401) and /api/unsplash/search respond as before

Wrangler bundles the Worker with esbuild's keep-names option, which
wraps functions in a `__name` helper. next-themes serialises its theme
function into an inline script, so that helper call reached the browser
and threw "__name is not defined" on every page load. The theme was then
applied only after hydration, so dark-mode users saw a flash.

Disables keep_names. Nothing in the Worker reads function names at run
time, and the app's error classes set their names explicitly.
@playlikeboy
playlikeboy merged commit 7bf5a29 into main Nov 12, 2026
1 check passed
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