refactor: consolidate 5 nested ALS scopes into unified request context#450
Draft
Divkix wants to merge 2 commits intocloudflare:mainfrom
Draft
refactor: consolidate 5 nested ALS scopes into unified request context#450Divkix wants to merge 2 commits intocloudflare:mainfrom
Divkix wants to merge 2 commits intocloudflare:mainfrom
Conversation
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)
commit: |
Snapshots were stale after the unified request context refactor changed the generated entry code (new imports, removed 5-deep nesting).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
UnifiedRequestContextbacked by oneAsyncLocalStorageinstance, replacing 5-6 nested ALS scopes per App Router requestheaders,navigation-state,cache,cache-runtime,fetch-cache,request-context) now checksisInsideUnifiedScope()first and reads from the unified store, falling back to its own standalone ALS for SSR/Pages Routerapp-rsc-entry.ts(both main handler and ISR regen) with a singlerunWithRequestContext()callDepends on: #432, #433, #434, #435, #436, #442
Closes #451
Changes
shims/unified-request-context.tsshims/headers.ts_getState()dual-path,setHeadersContext()simplified,runWithHeadersContext()no-op in unified scopeshims/navigation-state.ts_getState()dual-path,runWithNavigationContext()no-op, accessors simplifiedshims/cache.ts_getCacheState()dual-path,_runWithCacheState()no-opshims/cache-runtime.ts_getPrivateState()dual-path with lazy Map,runWithPrivateCache()no-opshims/fetch-cache.ts_getState()dual-path,runWithFetchCache()no-op, newensureFetchPatch()exportshims/request-context.tsgetRequestExecutionContext()dual-path,runWithExecutionContext()unified-awareindex.tsvinext/unified-request-contextaliasentries/app-rsc-entry.ts_runWithUnifiedCtx()at both sitestests/unified-request-context.test.tsWhat is NOT changed (important)
cacheContextStorageincache-runtime.ts— per-cache-call ALS, NOT per-request_unstableCacheAlsincache.ts— per-call scope forunstable_cache(), NOT per-requestTest plan