Repository navigation
fix(theme): keep the chosen theme when moving between pages - #370
Merged
Merged
Conversation
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.
Contributor
There was a problem hiding this comment.
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.
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.
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.astroonly 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 savedtheme=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 onpageshowwhen the page was restored by the back or forward button. The script is wrapped so it no longer leaves a globalthemeconstant, and reading storage can no longer throw.src/components/theme-toggle.tsx: the icon and label follow thedarkclass on<html>instead of the button's own clicks, so they stay right when the script above changes the class. A blockedlocalStorageno 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:
pnpm build,pnpm test(49 tests),pnpm test:slugsandpnpm test:pagespass. 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.