Skip to content

refactor: consolidate 5 nested ALS scopes into unified request context#450

Draft
Divkix wants to merge 2 commits intocloudflare:mainfrom
Divkix:worktree-refactor/unified-als-request-context
Draft

refactor: consolidate 5 nested ALS scopes into unified request context#450
Divkix wants to merge 2 commits intocloudflare:mainfrom
Divkix:worktree-refactor/unified-als-request-context

Conversation

@Divkix
Copy link
Contributor

@Divkix Divkix commented Mar 11, 2026

Summary

  • Introduces a single UnifiedRequestContext backed by one AsyncLocalStorage instance, replacing 5-6 nested ALS scopes per App Router request
  • Each shim module (headers, navigation-state, cache, cache-runtime, fetch-cache, request-context) now checks isInsideUnifiedScope() first and reads from the unified store, falling back to its own standalone ALS for SSR/Pages Router
  • Replaces the 5-deep nesting in app-rsc-entry.ts (both main handler and ISR regen) with a single runWithRequestContext() call

Depends on: #432, #433, #434, #435, #436, #442

Closes #451

Changes

File Change
shims/unified-request-context.ts NEW — unified ALS module
shims/headers.ts _getState() dual-path, setHeadersContext() simplified, runWithHeadersContext() no-op in unified scope
shims/navigation-state.ts _getState() dual-path, runWithNavigationContext() no-op, accessors simplified
shims/cache.ts _getCacheState() dual-path, _runWithCacheState() no-op
shims/cache-runtime.ts _getPrivateState() dual-path with lazy Map, runWithPrivateCache() no-op
shims/fetch-cache.ts _getState() dual-path, runWithFetchCache() no-op, new ensureFetchPatch() export
shims/request-context.ts getRequestExecutionContext() dual-path, runWithExecutionContext() unified-aware
index.ts Added vinext/unified-request-context alias
entries/app-rsc-entry.ts Replaced 5-deep nesting with single _runWithUnifiedCtx() at both sites
tests/unified-request-context.test.ts NEW — 17 unit tests

What is NOT changed (important)

  • cacheContextStorage in cache-runtime.ts — per-cache-call ALS, NOT per-request
  • _unstableCacheAls in cache.ts — per-call scope for unstable_cache(), NOT per-request
  • SSR entry's navigation ALS — separate Vite environment, must keep its own scope
  • Pages Router entries — Phase 2 (router-state, head-state are Pages-only)

Test plan

  • 17 new unified-request-context tests pass (isolation, nesting, concurrency)
  • 660 existing shims + request-context tests pass
  • 583 app-router + ISR + fetch-cache + features tests pass
  • 296 routing + nextjs-compat tests pass
  • Typecheck, lint, format all clean
  • CI full suite + Playwright E2E

Every App Router request previously ran through 5-6 nested
AsyncLocalStorage.run() calls (headers, navigation, cache-state,
private-cache, fetch-cache, plus optional execution-context). Each
ALS scope push/pop has measurable cost on Workers isolates.

Introduce a single UnifiedRequestContext that holds all per-request
state in one flat object, backed by one ALS instance. Each shim module
checks isInsideUnifiedScope() first and reads its sub-fields from the
unified store, falling back to its own standalone ALS when outside
(SSR environment, Pages Router, tests).

- Create unified-request-context.ts with createRequestContext(),
  runWithRequestContext(), getRequestContext(), isInsideUnifiedScope()
- Rewire _getState() in headers, navigation-state, cache, cache-runtime,
  fetch-cache, and request-context to dual-path (unified first, own ALS
  fallback)
- Make each runWith*() a no-op inside unified scope
- Replace 5-deep nesting in app-rsc-entry.ts (main handler + ISR regen)
  with single _runWithUnifiedCtx() call
- Export ensureFetchPatch() from fetch-cache for standalone patch install
- Add 17 unit tests for unified context (isolation, nesting, concurrency)
@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 11, 2026

Open in StackBlitz

npm i https://pkg.pr.new/vinext@450

commit: d7b05c5

Snapshots were stale after the unified request context refactor changed
the generated entry code (new imports, removed 5-deep nesting).
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.

Consolidate per-request ALS scopes for reduced overhead on Workers

1 participant