Skip to content

fix: preserve client-side useConfig() settings in server-side pageContext (vikejs/vike-vue#233)#224

Merged
brillout merged 4 commits into
mainfrom
claude/vike-vue-issue-233-ezibqw
Jun 26, 2026
Merged

fix: preserve client-side useConfig() settings in server-side pageContext (vikejs/vike-vue#233)#224
brillout merged 4 commits into
mainfrom
claude/vike-vue-issue-233-ezibqw

Conversation

@brillout

Copy link
Copy Markdown
Member

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 title via useConfig({ title }) inside a server +data() hook, the initial HTML has the correct <title>, but document.title is not updated upon client-side navigation on statically deployed apps. A page refresh fixes it.

Root cause

Settings passed via useConfig() are stored in pageContext._configViaHook (part of passToClient). But onRenderHtml() deleted the entire _configViaHook object before the prerendered *.pageContext.json was serialized:

// Not needed on the client-side, thus we remove it to save KBs sent to the client
delete pageContext._configViaHook

For non-prerendered apps this is harmless: client-side navigation re-runs +data() on the server (with isClientSideNavigation), repopulating _configViaHook in a fresh pageContext.json that never passes through onRenderHtml(). But for prerendered apps the *.pageContext.json files are produced from the render that onRenderHtml() mutated — so the title was stripped and lost on client navigation.

Fix

Instead of deleting _configViaHook wholesale, keep only the settings the client-side actually applies upon navigation — title and lang (see applyHeadSettings()) — and drop everything else (HTML-only and/or non-serializable values such as <Head> components). When nothing client-side remains, _configViaHook is dropped entirely (preserving the original "don't ship an empty object" behavior):

function removeServerOnlyConfigViaHook(pageContext: PageContextInternal) {
  const configViaHook = pageContext._configViaHook
  if (!configViaHook) return
  objectKeys(configViaHook).forEach((configName) => {
    if (!includes(configsClientSide, configName)) delete configViaHook[configName]
  })
  // Remove it altogether if there isn't anything left, saving KBs sent to the client
  if (objectKeys(configViaHook).length === 0) delete pageContext._configViaHook
}

The client-side settings list is now a single shared configsClientSide constant (['title', 'lang']), reused by both onRenderHtml() and useConfig() (server). Previously the server hook hard-coded ['title']; it now also includes lang, which applyHeadSettings() applies on the client too.

On the client, _configViaHook is only read for title/lang

  • getHeadSetting() (used by onRenderClientapplyHead) reads only title and lang.
  • The Head entry is read in the head rendering only server-side; the client only ever builds the page app.

So narrowing the client payload to title/lang is safe.

Testing

  • tsc --noEmit and biome pass.
  • Added an e2e test to the full example (useConfig() in +data() upon client-side navigation) that navigates client-side to a page whose <title> is set via useConfig({ title }) in its server +data() hook, and asserts document.title updates. The dev e2e suite passes locally.

🤖 Generated with Claude Code


Generated by Claude Code

claude added 2 commits June 26, 2026 13:05
…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
@brillout brillout changed the title fix: preserve serializable useConfig() settings in prerendered pageContext fix: preserve client-side useConfig() settings in server-side pageContext (vikejs/vike-vue#233) Jun 26, 2026
claude added 2 commits June 26, 2026 13:32
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
@brillout brillout merged commit 9692842 into main Jun 26, 2026
7 checks passed
@brillout brillout deleted the claude/vike-vue-issue-233-ezibqw branch June 26, 2026 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

useConfig({ title }) in +data loses title on prerendered client navigation

2 participants