fix: THEME setting overridden by stale localStorage value#2054
Open
genobear wants to merge 1 commit into
Open
Conversation
Force the server-passed THEME setting to be authoritative on every page load by reconciling localStorage before Alpine.$persist hydrates, and short-circuit the Ctrl+E toggle and switchTheme() helper when a theme is forced so the documented "Will disable theme switcher" promise is honored end-to-end. Closes unfoldadmin#2046
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.
Summary
Closes #2046.
UNFOLD = {\"THEME\": \"light\"}(or\"dark\") is silently overridden by any value inlocalStorage.adminTheme. The docs promise the setting "Will disable theme switcher", but in practice it is only honored on a user's first visit — once anything has written tolocalStorage.adminTheme(auto-detection, the visible toggle, or the hiddenCtrl+Eshortcut),Alpine.$persisthydrates from localStorage and the server-renderedtheme('light')is ignored on every subsequent load.This PR makes the server-passed
THEMEauthoritative on every page load.Changes
src/unfold/static/unfold/js/app.js:Alpine.$persist(defaultTheme).as('adminTheme')runs, when the server passes an explicitdefaultTheme(i.e. anything other than\"auto\"), overwritelocalStorage.adminThemewith that value.$persistthen hydrates from the reconciled value rather than a stale one.forcedThemeon the Alpine component and short-circuit bothswitchTheme()and theCtrl+Ekeydown handler when it is set, so the documented "Will disable theme switcher" promise is honored end-to-end (no visible toggle and no hidden keybinding).THEMEis unset —defaultThemedefaults to\"auto\",forcedThemeisnull, and the existing UX (auto-detect + persisted user preference + Ctrl+E + visible switcher) is preserved.Tests
Added
tests/test_theme.pycovering:each_contextexposes the configuredTHEME(light,dark, or absent) under thethemecontext key.skeleton.html<html>element rendersclass=\"light\"/class=\"dark\"andx-data=\"theme('light')\"/x-data=\"theme('dark')\"whenTHEMEis set.THEMEis unset, the skeleton rendersx-data=\"theme()\"(Alpine falls back to\"auto\").switchTheme(...)controls) is omitted from rendered HTML whenTHEMEis set, and present when it is not.The localStorage-reconciliation and
forcedThemeshort-circuits are JS-only and not exercised by the Python test suite, but the changes are minimal and the existing template gating they back up is now covered.Test plan
uv run -- pytest .— 425 tests passpre-commit run --files src/unfold/static/unfold/js/app.js tests/test_theme.py— all hooks passUNFOLD = {\"THEME\": \"light\"}set and a stalelocalStorage.adminTheme = '\"dark\"'— page loads in light mode, Ctrl+E is inert