fix: persist dark-mode default to localStorage #564
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.
Fix: Dark-Mode Default Not Persisted to localStorage
Problem
When theme.colors.default_mode is set to 'dark', the initial value is calculated correctly (isDark = true) but never written to localStorage. After the first page reload getStyleStatus() overwrites isDark with the cached false, so users always see the light mode on the first visit even if the theme default is dark.
Root Cause
styleStatus.isDark is only computed at module load time; there is no call to setStyleStatus() when the cache is empty, so the correct default value is lost after the first read.
Solution
Write the initial isDark value to localStorage before the first getStyleStatus() call.
Changed
Test Steps
No breaking changes; existing users keep their current choice.
问题描述
当 _config.yml 中 theme.colors.default_mode 设为 dark 时,脚本虽然正确计算出 isDark = true,但并未将结果写入 localStorage。 首次刷新页面后 getStyleStatus() 会把 isDark 覆盖为缓存中的 false,导致用户首次访问永远显示浅色模式。
根本原因
styleStatus.isDark 仅在模块加载时计算一次;当本地缓存为空时,代码没有调用 setStyleStatus() 写入默认值,造成第一次读取后丢失正确值。
修复方案
在第一次调用 getStyleStatus() 之前,先将计算出的初始 isDark 写入 localStorage。
改动文件
测试步骤
无破坏性变更,老用户已有选择会被保留。