Skip to content

fix: getPageContext() reads the pageContext Vike sets for a hook only while that hook runs synchronously - #3759

Open
nitedani-agent wants to merge 5 commits into
vikejs:mainfrom
nitedani-agent:fix/getpagecontext-mixup
Open

nitedani-agent wants to merge 5 commits into
vikejs:mainfrom
nitedani-agent:fix/getpagecontext-mixup

Conversation

@nitedani-agent

@nitedani-agent nitedani-agent commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

ClaudeCodex Claude Fable 5.2 (high effort) wrote this; GPT-6.2-Sol (Codex, high effort) reviewed it.

On the server, getPageContext() can return the pageContext of another HTTP request. Vike keeps the pageContext of the hook it calls in one variable for the whole process and clears it in a microtask, so code of another request that runs before that microtask reads it. In my runs it happened when two requests waited for the same promise (a memoized fetch(), a shared cache) and one of them read after an await. You asked for this PR in telefunc/telefunc#520 (comment).

Runs on vike preview main (e9c6bca92) This PR (3c7d65a07)
getPageContext() in +data, +guard and +onBeforeRender, 180 of the reads with { asyncHook: true }: 4160 requests, alone and in overlapping pairs, 2990 reads another request's pageContext in 70 reads, 10 of them with that option in 0
useConfig() of vike-react 0.6.32 after an await in +data: 820 overlapping pairs 20 responses with the <title> another request set 0
getPageContext() after awaits in +onAfterRenderHtml, a hook vike-react calls: 1220 overlapping pairs the other request's pageContext in 20 pairs in 0

Vike now sets the variable only while the hook runs synchronously, and puts the previous value back when the hook returns. On the server, providePageContext(pageContext) writes to the request's AsyncLocalStorage store when there is one, so the published vike-react and vike-vue, which call it before the hooks they call themselves, need no change. In +data, +guard and +onBeforeRender, a read before the first await returns the request's own pageContext, as on main.

Without AsyncLocalStorage, providePageContext(pageContext) still writes the variable, so another request can still read the pageContext an extension provided for its hook there. I'd close that with providePageContext(pageContext, fn) in a PR of its own after this one, as in telefunc/telefunc#520. Is that the right direction?

Note Kind Blocks merge Next
With a store, getPageContext() right after providePageContext(), in the synchronous part of a hook Vike calls with a pageContext, such as +data (not in a +onHookCall wrapper), returns that hook's pageContext (on main: the given value, null included) bug no nothing, because every call in vike-react 0.6.32, vike-vue 0.9.16 and vike-solid 0.8.8 passes the pageContext its own callback received
In +onHookCall, getPageContext() right after hook.call() returns null (the hook's pageContext on main) bug no nothing, because +onHookCall gets pageContext as its second argument

What you should see

1. A line the first run of the table printed on main, for two requests sent together that wait for one promise. The same run on this PR printed none. The scripts, and how to run them, are in this gist.

request /a?id=892&data=shared.k891,helper.10: +data `helper.10` got the pageContext of /b?id=893&data=shared.k891
Approach

Eight candidates, rated on how sure I am each is the right fix (approach): Vike sets the variable only for the synchronous part of the hook it calls, providePageContext(pageContext) writes to the request's store when there is one and to the variable when there is none, and providePageContext(pageContext, fn) comes in a second pull request (8/10, chosen); the same in one pull request, with nothing written where there is no store (6/10: useConfig() in +onAfterRenderHtml would read null there, where it works on main); the scoped set plus the callback form, the extensions switched later (5/10: a Vike update alone fixes nothing for the hooks extensions call); the scoped set alone (3/10: those hooks still leak); the store slot without a callback form (3/10); the call without a callback as a usage error, as in Telefunc (2/10: it breaks every published vike-react and vike-vue app with a callable cumulative hook); getPageContext() reading the store first (1/10); changing nothing (0/10).

Loop A

Three passes that count only what they reproduce (first, second, third), none of which reproduced a bug.

  • On 685933cfb: request A runs a hook and request B only reads, across six kinds of hook call and five ways of waiting, on Node 26.13.1 and Node 22.23.4. main returned A's pageContext to B in 100 reads of each row, this PR in 0 (the one row left at 100 is a hook an extension calls where there is no store, which the description names). 30 edge cases passed (nested hooks, a hook that throws synchronously), and so did 5 cases with a second copy of Vike in the process. The three HTTP scripts, a streamed component, workerd with and without nodejs_compat (single requests only), and test-e2e test/vike-react and test/vike-solid agreed.
  • It found three lines of the fix that no test covered. The third test, added in 11a16e59d, fails when the queued clear or the fall-back without a store is removed. The third, that the value put back after a nested hook is the outer hook's pageContext and not null, is covered by the pass's probe and by no test.
  • On 11a16e59d and on 3c7d65a07: the three later commits change one comment and the spec. The built packages/vike/dist of 3c7d65a07 and of 685933cfb differ in that one comment line.
Loop B

Two rounds of review and of a pass over the shape of every file and function (review 1, shape 1, review 2, shape 2, what became of each finding).

  • Round 1, on 685933cfb: the two tests only failed when the cross-request read came back, so the third test was added (11a16e59d) and a comment reworded (439f05b8c). Declined as nits: equal alternatives, names, and 10 to 50 ns per call.
  • Round 2, on 11a16e59d: the third test's title claimed more than it asserts, so it was renamed (3c7d65a07). The pass rated the PR 8/10.
Fresh read

One cold read of 3c7d65a07 as the maintainer who merges (report): no bug, nothing worth changing, "merge as is: yes". It reverted the fix in two parts and saw a test fail for each (execHook.ts at the base: the first test; the store slot at the base: the second), ran four more mutations (each turned a test red), and sent 500 overlapping pairs of requests through its own probe: main returned the other request's pageContext 500 times, this PR 0 times with a store. It did not check overlapping requests on workerd with nodejs_compat, nor the vike-vue and vike-solid e2e lanes (those two are under Gates).

Gates

On 3c7d65a07, on my machine (log):

pnpm run format:check -> exit 0 (2157 files)
pnpm run lint -> exit 0 (440 files)
pnpm run docs:lint -> exit 0 (311 pages)
pnpm exec vitest run --project unit -> exit 0 (265 tests)
pnpm exec test-types -> exit 0 (36 projects)
pnpm run build, in packages/vike -> exit 0
pnpm exec test-e2e test/vike-react -> exit 0 (7 test files)
pnpm exec test-e2e test/vike-vue -> exit 0 (5 test files)
pnpm exec test-e2e test/vike-solid -> exit 0 (2 test files)
pnpm exec test-e2e test/playground -> exit 0 (4 test files)

CI on 3c7d65a07: 19 checks, all successful.

🤖 Generated with Claude Code

nitedani-agent and others added 2 commits November 20, 2026 18:22
… an `await`

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
nitedani-agent and others added 3 commits November 20, 2026 19:12
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…called next

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@nitedani-agent
nitedani-agent marked this pull request as ready for review November 20, 2026 19:18
@nitedani-agent

Copy link
Copy Markdown
Contributor Author

ClaudeCodex Claude Fable 5.2 (high effort) wrote this; GPT-6.2-Sol (Codex, high effort) reviewed it.

Ready for review at 3c7d65a07, and CI is green (19 checks). The three tests in getPageContext.spec.ts are there to prove the fix, so I'll remove them in a last commit once you've approved.

This branch has not been deployed

No deployments
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.

1 participant