Skip to content

fix(theme): keep the chosen theme when moving between pages - #370

Merged
yasinmiran merged 1 commit into
mainfrom
fix/theme-persistence
Nov 17, 2026
Merged

yasinmiran merged 1 commit into
mainfrom
fix/theme-persistence

Conversation

@yasinmiran

@yasinmiran yasinmiran commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

The theme switch did not hold when moving between pages. I found two separate causes.

What went wrong

A device in light mode lost the dark choice on every page load. The script in base.astro only applied dark when the stored choice was dark and the operating system also preferred dark. So on a phone or laptop set to light, pressing the switch turned the page dark and saved theme=dark, and the next page removed it again. Anyone whose device is in dark mode never saw this, which is probably why it went unnoticed.

The back button brought back the old theme. Browsers restore the previous page from memory exactly as it was left, without running the head script again. Switch the theme on one page, press back, and the earlier page still showed the theme from before, with the switch showing the wrong icon.

What I changed

  • src/layouts/base.astro: the stored choice alone decides the theme. The same check runs again on pageshow when the page was restored by the back or forward button. The script is wrapped so it no longer leaves a global theme constant, and reading storage can no longer throw.
  • src/components/theme-toggle.tsx: the icon and label follow the dark class on <html> instead of the button's own clicks, so they stay right when the script above changes the class. A blocked localStorage no longer stops the switch from working for the current page.

The default is unchanged: the site is light until the visitor picks a theme, whatever the device prefers.

How I checked

In headless Chrome I emulated the device theme, pressed the real switch, opened a second page, switched there and went back. Before the fix, the light device with a dark choice came up light on the second page, and four of the five runs showed the stale theme after going back. After the fix all five pass, on both the dev server and the built site:

Device Choice Second page After switching and going back
light dark dark matches
dark dark dark matches
dark light light matches
dark none light matches
light none light matches

pnpm build, pnpm test (49 tests), pnpm test:slugs and pnpm test:pages pass. I have not tried it on a real iPhone; Safari restores pages from memory more often than Chrome does, so the back button case is the one worth a look there.

Not in this PR

Two tabs open at once do not follow each other: a tab keeps its theme until it loads a page or is restored.

The head script applied dark only when the stored choice was dark and the device also preferred dark, so on a device in light mode every page load dropped the choice the switch had just saved. The stored choice now decides on its own.

A page restored by the back button also kept the theme it had when it was left. The script re-applies the stored choice on pageshow, and the switch reads its icon from the class on <html> so it stays in step.
Copilot AI balanced review requested due to automatic review settings November 17, 2026 17:16

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@yasinmiran
yasinmiran merged commit eed5b78 into main Nov 17, 2026
1 check passed
@yasinmiran
yasinmiran deleted the fix/theme-persistence branch November 17, 2026 18:41
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.

2 participants