mirror of
https://github.com/cloudflare/vinext.git
synced 2026-05-09 08:25:34 +02:00
[PR #410] [MERGED] feat: AsyncLocalStorage for ExecutionContext (ctx) propagation #553
Labels
No labels
enhancement
enhancement
good first issue
help wanted
nextjs-tracking
nextjs-tracking
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/vinext#553
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/cloudflare/vinext/pull/410
Author: @james-elicx
Created: 3/10/2026
Status: ✅ Merged
Merged: 3/10/2026
Merged by: @james-elicx
Base:
main← Head:feat/execution-context-als📝 Commits (1)
7a3d9a3feat: AsyncLocalStorage for ExecutionContext (ctx) propagation📊 Changes
8 files changed (+276 additions, -28 deletions)
View changed files
📝
packages/vinext/src/cloudflare/kv-cache-handler.ts(+23 -13)📝
packages/vinext/src/deploy.ts(+10 -3)📝
packages/vinext/src/entries/pages-server-entry.ts(+19 -3)📝
packages/vinext/src/server/app-router-entry.ts(+8 -4)➕
packages/vinext/src/shims/request-context.ts(+81 -0)📝
tests/__snapshots__/entry-templates.test.ts.snap(+16 -3)📝
tests/deploy.test.ts(+8 -2)➕
tests/request-context.test.ts(+111 -0)📄 Description
Summary
Extracts
ExecutionContext(ctx) propagation from #405 into a standalone feature. Adds anAsyncLocalStorage-based mechanism so Cloudflare Workers'ctx(used forwaitUntil) is accessible anywhere in the call stack without manual threading.What changed
shims/request-context.ts— New ALS module keyed onSymbol.for("vinext.requestContext.als"), following the same pattern asheaders.ts,cache.ts, andnavigation-state.ts. ExportsrunWithExecutionContext(ctx, fn)andgetRequestExecutionContext().server/app-router-entry.ts—fetch(request, _env?, ctx?)now wraps the RSC handler inrunWithExecutionContext(ctx, ...)whenctxis present.entries/app-rsc-entry.ts— Generated RSC entry importsrunWithExecutionContextandgetRequestExecutionContextfrom the shim for downstream use.deploy.ts— Generated Cloudflare worker entry updated tofetch(request, env, ctx)and forwardsctxtohandler.fetch(request, env, ctx).cloudflare/kv-cache-handler.ts— UsesgetRequestExecutionContext() ?? this.ctxso the ALS-provided ctx takes priority over the constructor-injected one.Design
getRequestExecutionContext()returnsnullwhen called outside a request scope (e.g. Node.js dev server), making it safe to call anywhere. The ALS scope is established byapp-router-entry.tsbefore any RSC/SSR/routing logic runs, so it's available throughout the entire request lifecycle.Tests
tests/request-context.test.tscovering: null outside scope, correct value inside scope, async propagation, concurrent request isolation, nested scope override/restore, andwaitUntilcallable from inside scope.tests/deploy.test.tsassertions for the new worker entry signature.tests/__snapshots__/entry-templates.test.ts.snap.Related
Extracted from #405 (ISR caching) — the ctx plumbing is a prerequisite for
waitUntil-based background cache writes but is generally useful on its own.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.