fix: preserve client-side useConfig() settings in server-side pageContext (vikejs/vike-vue#233)#224
Merged
Merged
Conversation
…ntext
`onRenderHtml()` deleted the whole `pageContext._configViaHook` object
before the (prerendered) `*.pageContext.json` was serialized. Since the
`title` set via `useConfig({ title })` (e.g. inside a server `+data()`
hook) is stored in `_configViaHook`, prerendered pages lost their title
upon client-side navigation on statically deployed apps.
Instead of deleting `_configViaHook` entirely, keep only the settings the
client-side applies upon navigation (`title` and `lang`, see
applyHeadSettings()) and remove the rest (HTML-only and/or
non-serializable values such as `<Head>` components). When nothing
client-side is left, `_configViaHook` is dropped altogether.
Port of vikejs/vike-vue#234 (fixes vikejs/vike-vue#233) to vike-react.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SvFjBdBmQ2wVMBHwHmouky
The added `useConfig() in +data() upon client-side navigation` test
deterministically broke the unrelated `+react.{server.client}.js` test in
the same example run (the identifier-prefix log assertion is sensitive to
preceding navigation/log state — a vike-react-example-specific
interaction). The source fix is unchanged; aligning with #225.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SvFjBdBmQ2wVMBHwHmouky
Restores the regression test removed in the previous commit, fixed so it
no longer breaks the unrelated +react.{server.client}.js test.
Root cause: resolveStreamSetting() does `stream.reverse()`, mutating the
shared config.stream array in place, so the render mode of `/` alternates
(renderToString ↔ renderToStream) on every server render. The
+react.{server.client}.js test asserts the server identifierPrefix, which
only applies on the renderToString path, so it depends on the exact number
of prior `/` renders. The old test added a full `page.goto('/')`, flipping
that parity.
Fix: start the test from /star-wars instead of /, so it doesn't add a
server render of `/` and leaves the parity untouched. The test still
covers #233: the movie page's <title> (set via useConfig({ title }) in its
server-side +data() hook) must update upon client-side navigation.
Verified locally: the full example's preview e2e suite passes (both this
test and +react.{server.client}.js).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SvFjBdBmQ2wVMBHwHmouky
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SvFjBdBmQ2wVMBHwHmouky
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.
Port of vikejs/vike-vue#234 (which fixed vikejs/vike-vue#233) to vike-react, which has the identical bug.
Problem
When a prerendered app sets
titleviauseConfig({ title })inside a server+data()hook, the initial HTML has the correct<title>, butdocument.titleis not updated upon client-side navigation on statically deployed apps. A page refresh fixes it.Root cause
Settings passed via
useConfig()are stored inpageContext._configViaHook(part ofpassToClient). ButonRenderHtml()deleted the entire_configViaHookobject before the prerendered*.pageContext.jsonwas serialized:For non-prerendered apps this is harmless: client-side navigation re-runs
+data()on the server (withisClientSideNavigation), repopulating_configViaHookin a freshpageContext.jsonthat never passes throughonRenderHtml(). But for prerendered apps the*.pageContext.jsonfiles are produced from the render thatonRenderHtml()mutated — so thetitlewas stripped and lost on client navigation.Fix
Instead of deleting
_configViaHookwholesale, keep only the settings the client-side actually applies upon navigation —titleandlang(seeapplyHeadSettings()) — and drop everything else (HTML-only and/or non-serializable values such as<Head>components). When nothing client-side remains,_configViaHookis dropped entirely (preserving the original "don't ship an empty object" behavior):The client-side settings list is now a single shared
configsClientSideconstant (['title', 'lang']), reused by bothonRenderHtml()anduseConfig()(server). Previously the server hook hard-coded['title']; it now also includeslang, whichapplyHeadSettings()applies on the client too.On the client,
_configViaHookis only read fortitle/langgetHeadSetting()(used byonRenderClient→applyHead) reads onlytitleandlang.Headentry is read in the head rendering only server-side; the client only ever builds the page app.So narrowing the client payload to
title/langis safe.Testing
tsc --noEmitand biome pass.fullexample (useConfig() in +data() upon client-side navigation) that navigates client-side to a page whose<title>is set viauseConfig({ title })in its server+data()hook, and assertsdocument.titleupdates. The dev e2e suite passes locally.🤖 Generated with Claude Code
Generated by Claude Code