Skip to content

Commit

Permalink
Move WorkStore and ActionStore to /server/app-render (#70934)
Browse files Browse the repository at this point in the history
These are only for the server and really primarily just for the RSC
layer. So it never made much sense why these lived in the
`/client/components/` folder. While we're add it, I'm just moving these
over to join the other ones already there.

---------

Co-authored-by: Janka Uryga <[email protected]>
  • Loading branch information
2 people authored and kdy1 committed Oct 10, 2024
1 parent 4dd5832 commit 6a25930
Show file tree
Hide file tree
Showing 47 changed files with 76 additions and 79 deletions.
2 changes: 1 addition & 1 deletion packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ const browserNonTranspileModules = [
const precompileRegex = /[\\/]next[\\/]dist[\\/]compiled[\\/]/

const asyncStoragesRegex =
/next[\\/]dist[\\/](esm[\\/])?(client[\\/]components|server[\\/]app-render|)[\\/](work-async-storage|action-async-storage|work-unit-async-storage)/
/next[\\/]dist[\\/](esm[\\/])?server[\\/]app-render[\\/](work-async-storage|action-async-storage|work-unit-async-storage)/

// Support for NODE_PATH
const nodePathList = (process.env.NODE_PATH || '')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BailoutToCSRError } from '../../shared/lib/lazy-dynamic/bailout-to-csr'
import { workAsyncStorage } from './work-async-storage.external'
import { workAsyncStorage } from '../../server/app-render/work-async-storage.external'

export function bailoutToClientRendering(reason: string): void | never {
const workStore = workAsyncStorage.getStore()
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/client/components/client-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function ClientPageRoot({
}) {
if (typeof window === 'undefined') {
const { workAsyncStorage } =
require('./work-async-storage.external') as typeof import('./work-async-storage.external')
require('../../server/app-render/work-async-storage.external') as typeof import('../../server/app-render/work-async-storage.external')

let clientSearchParams: Promise<ParsedUrlQuery>
let clientParams: Promise<Params>
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/client/components/client-segment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function ClientSegmentRoot({
}) {
if (typeof window === 'undefined') {
const { workAsyncStorage } =
require('./work-async-storage.external') as typeof import('./work-async-storage.external')
require('../../server/app-render/work-async-storage.external') as typeof import('../../server/app-render/work-async-storage.external')

let clientParams: Promise<Params>
// We are going to instrument the searchParams prop with tracking for the
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/client/components/error-boundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React, { type JSX } from 'react'
import { useUntrackedPathname } from './navigation-untracked'
import { isNextRouterError } from './is-next-router-error'
import { handleHardNavError } from './nav-failure-handler'
import { workAsyncStorage } from './work-async-storage.external'
import { workAsyncStorage } from '../../server/app-render/work-async-storage.external'

const styles = {
error: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function hasFallbackRouteParams() {
if (typeof window === 'undefined') {
// AsyncLocalStorage should not be included in the client bundle.
const { workAsyncStorage } =
require('./work-async-storage.external') as typeof import('./work-async-storage.external')
require('../../server/app-render/work-async-storage.external') as typeof import('../../server/app-render/work-async-storage.external')

const workStore = workAsyncStorage.getStore()
if (!workStore) return false
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/client/components/redirect.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { actionAsyncStorage } from './action-async-storage.external'
import { actionAsyncStorage } from '../../server/app-render/action-async-storage.external'
import { RedirectStatusCode } from './redirect-status-code'

const REDIRECT_ERROR_CODE = 'NEXT_REDIRECT'
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/export/routes/app-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { isBailoutToCSRError } from '../../shared/lib/lazy-dynamic/bailout-to-cs
import { NodeNextRequest, NodeNextResponse } from '../../server/base-http/node'
import { NEXT_IS_PRERENDER_HEADER } from '../../client/components/app-router-headers'
import type { FetchMetrics } from '../../server/base-http'
import type { WorkStore } from '../../client/components/work-async-storage.external'
import type { WorkStore } from '../../server/app-render/work-async-storage.external'
import type { FallbackRouteParams } from '../../server/request/fallback-params'

export const enum ExportedAppPageFiles {
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/lib/metadata/metadata-context.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AppRenderContext } from '../../server/app-render/app-render'
import type { MetadataContext } from './types/resolvers'
import type { WorkStore } from '../../client/components/work-async-storage.external'
import type { WorkStore } from '../../server/app-render/work-async-storage.external'
import { trackFallbackParamAccessed } from '../../server/app-render/dynamic-rendering'

export function createMetadataContext(
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/lib/metadata/metadata.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import type {
} from './types/metadata-interface'
import { isNotFoundError } from '../../client/components/not-found'
import type { MetadataContext } from './types/resolvers'
import type { WorkStore } from '../../client/components/work-async-storage.external'
import type { WorkStore } from '../../server/app-render/work-async-storage.external'

// Use a promise to share the status of the metadata resolving,
// returning two components `MetadataTree` and `MetadataOutlet`
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/lib/metadata/resolve-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import { getTracer } from '../../server/lib/trace/tracer'
import { ResolveMetadataSpan } from '../../server/lib/trace/constants'
import { PAGE_SEGMENT_KEY } from '../../shared/lib/segment'
import * as Log from '../../build/output/log'
import type { WorkStore } from '../../client/components/work-async-storage.external'
import type { WorkStore } from '../../server/app-render/work-async-storage.external'
import type {
Params,
CreateServerParamsForMetadata,
Expand Down
9 changes: 3 additions & 6 deletions packages/next/src/server/after/after-context.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { DetachedPromise } from '../../lib/detached-promise'
import { AsyncLocalStorage } from 'async_hooks'

import type { WorkStore } from '../../client/components/work-async-storage.external'
import type { WorkStore } from '../app-render/work-async-storage.external'
import type { AfterContext } from './after-context'

describe('AfterContext', () => {
// 'async-local-storage.ts' needs `AsyncLocalStorage` on `globalThis` at import time,
// so we have to do some contortions here to set it up before running anything else
type WASMod =
typeof import('../../client/components/work-async-storage.external')
type WASMod = typeof import('../app-render/work-async-storage.external')
type AfterMod = typeof import('./after')
type AfterContextMod = typeof import('./after-context')

Expand All @@ -20,9 +19,7 @@ describe('AfterContext', () => {
// @ts-expect-error
globalThis.AsyncLocalStorage = AsyncLocalStorage

const WASMod = await import(
'../../client/components/work-async-storage.external'
)
const WASMod = await import('../app-render/work-async-storage.external')
workAsyncStorage = WASMod.workAsyncStorage

const AfterContextMod = await import('./after-context')
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/server/after/after-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import type { RequestLifecycleOpts } from '../base-server'
import type { AfterCallback, AfterTask } from './after'
import { InvariantError } from '../../shared/lib/invariant-error'
import { isThenable } from '../../shared/lib/is-thenable'
import { workAsyncStorage } from '../../client/components/work-async-storage.external'
import { workAsyncStorage } from '../app-render/work-async-storage.external'
import { withExecuteRevalidates } from './revalidation-utils'
import { bindSnapshot } from '../../client/components/async-local-storage'
import { bindSnapshot } from '../app-render/async-local-storage'

export type AfterContextOpts = {
waitUntil: RequestLifecycleOpts['waitUntil'] | undefined
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/server/after/after.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { workAsyncStorage } from '../../client/components/work-async-storage.external'
import { workUnitAsyncStorage } from '../../server/app-render/work-unit-async-storage.external'
import { workAsyncStorage } from '../app-render/work-async-storage.external'
import { workUnitAsyncStorage } from '../app-render/work-unit-async-storage.external'
import { StaticGenBailoutError } from '../../client/components/static-generation-bailout'

import { markCurrentScopeAsDynamic } from '../app-render/dynamic-rendering'
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/after/revalidation-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { WorkStore } from '../../client/components/work-async-storage.external'
import type { WorkStore } from '../app-render/work-async-storage.external'

/** Run a callback, and execute any *new* revalidations added during its runtime. */
export async function withExecuteRevalidates<T>(
Expand Down
6 changes: 3 additions & 3 deletions packages/next/src/server/app-render/action-handler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http'
import type { SizeLimit } from '../../types'
import type { RequestStore } from '../../server/app-render/work-unit-async-storage.external'
import type { RequestStore } from '../app-render/work-unit-async-storage.external'
import type { AppRenderContext, GenerateFlight } from './app-render'
import type { AppPageModule } from '../../server/route-modules/app-page/module'
import type { AppPageModule } from '../route-modules/app-page/module'
import type { BaseNextRequest, BaseNextResponse } from '../base-http'

import {
Expand All @@ -19,7 +19,7 @@ import {
type RedirectType,
} from '../../client/components/redirect'
import RenderResult from '../render-result'
import type { WorkStore } from '../../client/components/work-async-storage.external'
import type { WorkStore } from '../app-render/work-async-storage.external'
import { FlightRenderResult } from './flight-render-result'
import {
filterReqHeaders,
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/server/app-render/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import type {
FlightData,
InitialRSCPayload,
} from './types'
import type { WorkStore } from '../../client/components/work-async-storage.external'
import type { RequestStore } from '../../server/app-render/work-unit-async-storage.external'
import type { WorkStore } from '../app-render/work-async-storage.external'
import type { RequestStore } from '../app-render/work-unit-async-storage.external'
import type { NextParsedUrlQuery } from '../request-meta'
import type { LoaderTree } from '../lib/app-dir-module'
import type { AppPageModule } from '../route-modules/app-page/module'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createSnapshot } from '../../client/components/async-local-storage'
import { createSnapshot } from '../app-render/async-local-storage'

export const runInCleanSnapshot: <R, TArgs extends any[]>(
fn: (...args: TArgs) => R,
Expand Down
6 changes: 3 additions & 3 deletions packages/next/src/server/app-render/dynamic-rendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
* read that data outside the cache and pass it in as an argument to the cached function.
*/

import type { WorkStore } from '../../client/components/work-async-storage.external'
import type { WorkUnitStore } from '../../server/app-render/work-unit-async-storage.external'
import type { WorkStore } from '../app-render/work-async-storage.external'
import type { WorkUnitStore } from '../app-render/work-unit-async-storage.external'

// Once postpone is in stable we should switch to importing the postpone export directly
import React from 'react'
Expand All @@ -33,7 +33,7 @@ import {
type PrerenderStoreLegacy,
type PrerenderStoreModern,
} from './work-unit-async-storage.external'
import { workAsyncStorage } from '../../client/components/work-async-storage.external'
import { workAsyncStorage } from '../app-render/work-async-storage.external'
import { makeHangingPromise } from '../dynamic-rendering-utils'
import {
METADATA_BOUNDARY_NAME,
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/server/app-render/entry-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export { prerender } from 'react-server-dom-webpack/static.edge'

import LayoutRouter from '../../client/components/layout-router'
import RenderFromTemplateContext from '../../client/components/render-from-template-context'
import { workAsyncStorage } from '../../client/components/work-async-storage.external'
import { workAsyncStorage } from '../app-render/work-async-storage.external'
import { workUnitAsyncStorage } from './work-unit-async-storage.external'
import { actionAsyncStorage } from '../../client/components/action-async-storage.external'
import { actionAsyncStorage } from '../app-render/action-async-storage.external'
import { ClientPageRoot } from '../../client/components/client-page'
import { ClientSegmentRoot } from '../../client/components/client-segment'
import {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { AsyncLocalStorage } from 'async_hooks'
import type { IncrementalCache } from '../../server/lib/incremental-cache'
import type { DynamicServerError } from './hooks-server-context'
import type { FetchMetrics } from '../../server/base-http'
import type { FallbackRouteParams } from '../../server/request/fallback-params'
import type { IncrementalCache } from '../lib/incremental-cache'
import type { DynamicServerError } from '../../client/components/hooks-server-context'
import type { FetchMetrics } from '../base-http'
import type { FallbackRouteParams } from '../request/fallback-params'
import type { DeepReadonly } from '../../shared/lib/deep-readonly'
import type { AppSegmentConfig } from '../../build/segment-config/app/app-segment-config'
import type { AfterContext } from '../../server/after/after-context'
import type { AfterContext } from '../after/after-context'

// Share the instance module in the next-shared layer
import { workAsyncStorage } from './work-async-storage-instance' with { 'turbopack-transition': 'next-shared' }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createAsyncLocalStorage } from '../../client/components/async-local-storage'
import { createAsyncLocalStorage } from './async-local-storage'
import type { WorkUnitAsyncStorage } from './work-unit-async-storage.external'

export const workUnitAsyncStorage: WorkUnitAsyncStorage =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { AsyncLocalStorage } from 'async_hooks'
import type { DraftModeProvider } from '../../server/async-storage/draft-mode-provider'
import type { ResponseCookies } from '../../server/web/spec-extension/cookies'
import type { ReadonlyHeaders } from '../../server/web/spec-extension/adapters/headers'
import type { ReadonlyRequestCookies } from '../../server/web/spec-extension/adapters/request-cookies'
import type { DraftModeProvider } from '../async-storage/draft-mode-provider'
import type { ResponseCookies } from '../web/spec-extension/cookies'
import type { ReadonlyHeaders } from '../web/spec-extension/adapters/headers'
import type { ReadonlyRequestCookies } from '../web/spec-extension/adapters/request-cookies'
import type { CacheSignal } from './cache-signal'
import type { DynamicTrackingState } from './dynamic-rendering'

// Share the instance module in the next-shared layer
import { workUnitAsyncStorage } from './work-unit-async-storage-instance' with { 'turbopack-transition': 'next-shared' }
import type { ServerComponentsHmrCache } from '../../server/response-cache'
import type { ServerComponentsHmrCache } from '../response-cache'

export type RequestStore = {
type: 'request'
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/async-storage/cache-scope.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createAsyncLocalStorage } from '../../client/components/async-local-storage'
import { createAsyncLocalStorage } from '../app-render/async-local-storage'

export interface CacheScopeStore {
cache?: Map<string, any>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { AsyncLocalStorage } from 'async_hooks'
import type {
RequestStore,
WorkUnitStore,
} from '../../server/app-render/work-unit-async-storage.external'
} from '../app-render/work-unit-async-storage.external'
import type { RenderOpts } from '../app-render/types'
import type { WithStore } from './with-store'
import type { NextRequest } from '../web/spec-extension/request'
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/async-storage/with-work-store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { WithStore } from './with-store'
import type { WorkStore } from '../../client/components/work-async-storage.external'
import type { WorkStore } from '../app-render/work-async-storage.external'
import type { AsyncLocalStorage } from 'async_hooks'
import type { IncrementalCache } from '../lib/incremental-cache'
import type { RenderOpts } from '../app-render/types'
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/server/lib/patch-fetch.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AsyncLocalStorage } from 'node:async_hooks'
import type { WorkUnitStore } from '../../server/app-render/work-unit-async-storage.external'
import type { WorkStore } from '../../client/components/work-async-storage.external'
import type { WorkUnitStore } from '../app-render/work-unit-async-storage.external'
import type { WorkStore } from '../app-render/work-async-storage.external'
import type { IncrementalCache } from './incremental-cache'
import { createPatchedFetcher } from './patch-fetch'

Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/server/lib/patch-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {
WorkAsyncStorage,
WorkStore,
} from '../../client/components/work-async-storage.external'
} from '../app-render/work-async-storage.external'

import { AppRenderSpan, NextNodeServerSpan } from './trace/constants'
import { getTracer, SpanKind } from './trace/tracer'
Expand All @@ -19,7 +19,7 @@ import type {
WorkUnitAsyncStorage,
WorkUnitStore,
RequestStore,
} from '../../server/app-render/work-unit-async-storage.external'
} from '../app-render/work-unit-async-storage.external'
import {
CachedRouteKind,
IncrementalCacheKind,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { workAsyncStorage } from '../../client/components/work-async-storage.external'
import { workAsyncStorage } from '../app-render/work-async-storage.external'
import { workUnitAsyncStorage } from '../app-render/work-unit-async-storage.external'
import { abortOnSynchronousDynamicDataAccess } from '../app-render/dynamic-rendering'

Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/request/connection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { workAsyncStorage } from '../../client/components/work-async-storage.external'
import { workAsyncStorage } from '../app-render/work-async-storage.external'
import { workUnitAsyncStorage } from '../app-render/work-unit-async-storage.external'
import {
postponeWithTracking,
Expand Down
12 changes: 6 additions & 6 deletions packages/next/src/server/request/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {
type ReadonlyRequestCookies,
type ResponseCookies,
RequestCookiesAdapter,
} from '../../server/web/spec-extension/adapters/request-cookies'
import { RequestCookies } from '../../server/web/spec-extension/cookies'
import { workAsyncStorage } from '../../client/components/work-async-storage.external'
} from '../web/spec-extension/adapters/request-cookies'
import { RequestCookies } from '../web/spec-extension/cookies'
import { workAsyncStorage } from '../app-render/work-async-storage.external'
import {
workUnitAsyncStorage,
type PrerenderStoreModern,
Expand All @@ -14,9 +14,9 @@ import {
abortAndThrowOnSynchronousDynamicDataAccess,
throwToInterruptStaticGeneration,
trackDynamicDataInDynamicRender,
} from '../../server/app-render/dynamic-rendering'
import { getExpectedRequestStore } from '../../server/app-render/work-unit-async-storage.external'
import { actionAsyncStorage } from '../../client/components/action-async-storage.external'
} from '../app-render/dynamic-rendering'
import { getExpectedRequestStore } from '../app-render/work-unit-async-storage.external'
import { actionAsyncStorage } from '../app-render/action-async-storage.external'
import { StaticGenBailoutError } from '../../client/components/static-generation-bailout'
import { makeResolvedReactPromise } from './utils'
import { makeHangingPromise } from '../dynamic-rendering-utils'
Expand Down
6 changes: 3 additions & 3 deletions packages/next/src/server/request/draft-mode.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { getExpectedRequestStore } from '../../server/app-render/work-unit-async-storage.external'
import { getExpectedRequestStore } from '../app-render/work-unit-async-storage.external'

import type { DraftModeProvider } from '../../server/async-storage/draft-mode-provider'
import type { DraftModeProvider } from '../async-storage/draft-mode-provider'

import { workAsyncStorage } from '../../client/components/work-async-storage.external'
import { workAsyncStorage } from '../app-render/work-async-storage.external'
import { workUnitAsyncStorage } from '../app-render/work-unit-async-storage.external'
import { trackDynamicDataAccessed } from '../app-render/dynamic-rendering'
import { createDedupedByCallsiteServerErrorLoggerDev } from '../create-deduped-by-callsite-server-error-loger'
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/server/request/headers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
HeadersAdapter,
type ReadonlyHeaders,
} from '../../server/web/spec-extension/adapters/headers'
import { workAsyncStorage } from '../../client/components/work-async-storage.external'
} from '../web/spec-extension/adapters/headers'
import { workAsyncStorage } from '../app-render/work-async-storage.external'
import { getExpectedRequestStore } from '../app-render/work-unit-async-storage.external'
import {
workUnitAsyncStorage,
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/request/params.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { WorkStore } from '../../client/components/work-async-storage.external'
import type { WorkStore } from '../app-render/work-async-storage.external'
import type { FallbackRouteParams } from './fallback-params'

import { ReflectAdapter } from '../web/spec-extension/adapters/reflect'
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/request/search-params.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { WorkStore } from '../../client/components/work-async-storage.external'
import type { WorkStore } from '../app-render/work-async-storage.external'

import { ReflectAdapter } from '../web/spec-extension/adapters/reflect'
import {
Expand Down
Loading

0 comments on commit 6a25930

Please sign in to comment.