fix: fall back to an available choice when the selected one is absent on the page (#169)#170
Merged
Merged
Conversation
… on the page (#169) A page can define a choice group with only a subset of the group's choices (e.g. `bodyHtmlEnd` has `React`/`Vue` but not `Solid`). When a choice persisted in localStorage (e.g. `Solid`, selected on another page) has no content on the current page, the hidden `<select>` that drives CSS visibility still matched its (empty) `<option>` — hiding every real choice and showing the empty one — while the visible selector hid itself. Result: nothing shown. Resolve the displayed choice to an available one (the stored choice if it has content, else the group's default, else the first available choice) without overwriting the stored global preference. The SSR `initializeChoiceGroup` script now skips choices marked `data-empty`, keeping the server-rendered fallback and avoiding a flash. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BcbPYNNRXEBpDZEzzZwVkT
Remove the e2e/unit test additions (and the demo page-4 reproduction) for this edge-case quick fix, and trim comments down to the rationale the code can't convey on its own. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BcbPYNNRXEBpDZEzzZwVkT
…absent on the page
Collaborator
|
@brillout, LGTM ? |
Owner
Author
|
I didn't review the code in details, so as you wish. From my high-level perspective I think we can merge. |
Collaborator
|
Alright, thanks. Merging now. |
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.
Fixes #169.
Root cause
A page can define a choice group with only a subset of the group's choices. For
uiFramework(React/Vue/Solid),https://vike.dev/ClientOnlyhas all three, buthttps://vike.dev/bodyHtmlEndonly hasReact/Vue— soSolidis an empty choice there.When
Solidis selected onClientOnly, it's persisted tolocalStorage(docpress:choice:uiFramework). Navigating tobodyHtmlEnd:<select>that drives choice visibility via CSS still renders an<option value="Solid">(the option list is the full config, not just the page's choices). Withvalue="Solid"it gets:checked, so the CSS hides every real.choiceand shows only the emptySolidone → no content.CustomSelectfilters out empty choices and adds ahiddenclass when the selected choice is empty → no selector either.Net result: nothing is shown.
Fix
Resolve the displayed choice to one that actually has content on the current page, via a small helper
getAvailableChoice():default, if available; otherwiseThis is applied to the hidden
<select>(drives content visibility),CustomSelect, andTabs. The stored global preference is not overwritten — pages that do haveSolidkeep showing it.To avoid a pre-hydration flash, the inline SSR script (
initializeChoiceGroup) now marks empty<option>s withdata-emptyand only applies the stored choice when it has content on the page, keeping the server-rendered fallback otherwise. Stale choices (no longer in the config at all) are still cleaned up fromlocalStorage.Tests
getAvailableChoice.spec.ts): covers the fallback cases (default available, default empty, nothing empty, everything empty).demo/testRun.ts+demo/pages/page-4):page-4now definesuiFrameworkwith onlyReact/Vue. The test selectsSolid(absent here), reloads, and asserts the content falls back toReactinstead of showing nothing.Verified locally against the demo dev server with Playwright: the fix renders the fallback content, keeps the selector visible, preserves the
Solidpreference, andfeatures(which hasSolid) still rendersSolid. Reverting the fix reproduces the blank page, confirming the test catches the regression.build,test:types, andtest:unitsall pass.🤖 Generated with Claude Code
https://claude.ai/code/session_01BcbPYNNRXEBpDZEzzZwVkT
Generated by Claude Code