mirror of
https://github.com/cloudflare/vinext.git
synced 2026-05-09 08:25:34 +02:00
[PR #607] [CLOSED] feat: add per-request store API (getRequestStore) #708
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#708
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/607
Author: @JamesbbBriz
Created: 3/20/2026
Status: ❌ Closed
Base:
main← Head:feat/per-request-store📝 Commits (3)
f7cdddffeat: add per-request store API (getRequestStore)371d00dfix: return fresh Map outside request scope to prevent data leakage985e6ecfix: wrap Pages Router API routes in unified request context📊 Changes
5 files changed (+95 additions, -5 deletions)
View changed files
📝
packages/vinext/package.json(+4 -0)📝
packages/vinext/src/entries/pages-server-entry.ts(+6 -1)📝
packages/vinext/src/server/api-handler.ts(+9 -2)➕
packages/vinext/src/shims/request-store.ts(+69 -0)📝
packages/vinext/src/shims/unified-request-context.ts(+7 -2)📄 Description
Summary
Adds a user-accessible per-request key-value store backed by vinext's existing
AsyncLocalStorage-based unified request context.Primary use case: per-request database clients on Cloudflare Workers. Global DB singletons cause alternating request failures (#537) because Workers isolates reuse across requests but I/O contexts are per-request.
getRequestStore()gives each request its ownMapthat's automatically garbage-collected when the request completes — no manual cleanup, no TTL hacks.API
Motivation
We're running a production SaaS (OptiTalent) on vinext + Cloudflare Workers with Prisma v7 + Hyperdrive + R2. We hit the exact alternating-failure pattern described in #537 and initially worked around it with a 50ms TTL heuristic. It works, but it's fragile — the correct solution is framework-level per-request scoping.
vinext already has the infrastructure for this via
UnifiedRequestContext+AsyncLocalStorage. This PR simply exposes it to users via a clean public API, similar to:event.localsc.set()/c.get()contextScope & Limitations
getRequestStore()works in any code path wrapped byrunWithRequestContext()(the unified ALS scope):app/api/*/route.ts)pages/api/*)985e6ec(both dev + prod)middleware.ts)runWithExecutionContext, inherits into unified scopeChanges
packages/vinext/src/shims/unified-request-context.tsuserStore: Map<string, unknown>toUnifiedRequestContextpackages/vinext/src/shims/request-store.tsgetRequestStore()public APIpackages/vinext/package.json"./request-store"exportTest plan
getRequestStore()returns isolated Maps across concurrentrunWithRequestContext()callsgetRequestStore()returns fallback Map outside request scopeCloses #537
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.