Unverified Commit 771824da authored by Sebastian Markbåge's avatar Sebastian Markbåge Committed by GitHub

Move AfterContext to WorkStore (#70806)

We want to support `after()` to run after all work has completed.
Ideally in any scenario that runs the parent. If there's multiple units
of work (e.g. batched Actions) or inner work like `"use cache"` /
`unstable_cache`, they should ideally all run at the end to avoid
blocking the other work.

It's difficult to remember how to wire this up each time we shadow a
unit or work. It's simpler just to put this on WorkStore which is where
we already have all internal "after" like `pendingRevalidates` and
`pendingRevalidateWrites`.

This is dependent on having a [WorkStore in
Middleware](https://github.com/vercel/next.js/pull/70808/) where we
currently support `after()`. We do not support `after()` in `pages/`.
parent c53ef9b3
......@@ -6,7 +6,6 @@ import type { ReadonlyRequestCookies } from '../../server/web/spec-extension/ada
// Share the instance module in the next-shared layer
import { requestAsyncStorage } from './request-async-storage-instance' with { 'turbopack-transition': 'next-shared' }
import type { AfterContext } from '../../server/after/after-context'
import type { ServerComponentsHmrCache } from '../../server/response-cache'
import { cacheAsyncStorage } from '../../server/app-render/cache-async-storage.external'
......@@ -34,7 +33,6 @@ export interface RequestStore {
readonly cookies: ReadonlyRequestCookies
readonly mutableCookies: ResponseCookies
readonly draftMode: DraftModeProvider
readonly afterContext: AfterContext | undefined
readonly isHmrRefresh?: boolean
readonly serverComponentsHmrCache?: ServerComponentsHmrCache
}
......
......@@ -6,6 +6,7 @@ import type { Revalidate } from '../../server/lib/revalidate'
import type { FallbackRouteParams } from '../../server/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'
// Share the instance module in the next-shared layer
import { workAsyncStorage } from './work-async-storage-instance' with { 'turbopack-transition': 'next-shared' }
......@@ -43,6 +44,7 @@ export interface WorkStore {
dynamicShouldError?: boolean
pendingRevalidates?: Record<string, Promise<any>>
pendingRevalidateWrites?: Array<Promise<void>> // This is like pendingRevalidates but isn't used for deduping.
readonly afterContext: AfterContext | undefined
dynamicUsageDescription?: string
dynamicUsageStack?: string
......
import { DetachedPromise } from '../../lib/detached-promise'
import { AsyncLocalStorage } from 'async_hooks'
import type { RequestStore } from '../../client/components/request-async-storage.external'
import type { WorkStore } from '../../client/components/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 RASMod =
typeof import('../../client/components/request-async-storage.external')
type WASMod =
typeof import('../../client/components/work-async-storage.external')
type AfterMod = typeof import('./after')
type AfterContextMod = typeof import('./after-context')
let requestAsyncStorage: RASMod['requestAsyncStorage']
let workAsyncStorage: WASMod['workAsyncStorage']
let AfterContext: AfterContextMod['AfterContext']
let after: AfterMod['unstable_after']
......@@ -20,10 +20,10 @@ describe('AfterContext', () => {
// @ts-expect-error
globalThis.AsyncLocalStorage = AsyncLocalStorage
const RASMod = await import(
'../../client/components/request-async-storage.external'
const WASMod = await import(
'../../client/components/work-async-storage.external'
)
requestAsyncStorage = RASMod.requestAsyncStorage
workAsyncStorage = WASMod.workAsyncStorage
const AfterContextMod = await import('./after-context')
AfterContext = AfterContextMod.AfterContext
......@@ -33,11 +33,9 @@ describe('AfterContext', () => {
})
const createRun =
(afterContext: AfterContext, requestStore: RequestStore) =>
(_afterContext: AfterContext, workStore: WorkStore) =>
<T>(cb: () => T): T => {
return afterContext.run(requestStore, () =>
requestAsyncStorage.run(requestStore, cb)
)
return workAsyncStorage.run(workStore, cb)
}
it('runs after() callbacks from a run() callback that resolves', async () => {
......@@ -54,8 +52,8 @@ describe('AfterContext', () => {
onClose,
})
const requestStore = createMockRequestStore(afterContext)
const run = createRun(afterContext, requestStore)
const workStore = createMockWorkStore(afterContext)
const run = createRun(afterContext, workStore)
// ==================================
......@@ -120,9 +118,9 @@ describe('AfterContext', () => {
onClose,
})
const requestStore = createMockRequestStore(afterContext)
const workStore = createMockWorkStore(afterContext)
const run = createRun(afterContext, requestStore)
const run = createRun(afterContext, workStore)
// ==================================
......@@ -167,9 +165,9 @@ describe('AfterContext', () => {
onClose,
})
const requestStore = createMockRequestStore(afterContext)
const workStore = createMockWorkStore(afterContext)
const run = createRun(afterContext, requestStore)
const run = createRun(afterContext, workStore)
// ==================================
......@@ -257,8 +255,8 @@ describe('AfterContext', () => {
onClose,
})
const requestStore = createMockRequestStore(afterContext)
const run = createRun(afterContext, requestStore)
const workStore = createMockWorkStore(afterContext)
const run = createRun(afterContext, workStore)
// ==================================
......@@ -316,9 +314,9 @@ describe('AfterContext', () => {
onClose,
})
const requestStore = createMockRequestStore(afterContext)
const workStore = createMockWorkStore(afterContext)
const run = createRun(afterContext, requestStore)
const run = createRun(afterContext, workStore)
// ==================================
......@@ -353,7 +351,7 @@ describe('AfterContext', () => {
onClose,
})
const requestStore = createMockRequestStore(afterContext)
const workStore = createMockWorkStore(afterContext)
// ==================================
......@@ -367,13 +365,11 @@ describe('AfterContext', () => {
const promise3 = new DetachedPromise<string>()
const afterCallback3 = jest.fn(() => promise3.promise)
requestAsyncStorage.run(requestStore, () =>
afterContext.run(requestStore, () => {
after(afterCallback1)
after(afterCallback2)
after(afterCallback3)
})
)
workAsyncStorage.run(workStore, () => {
after(afterCallback1)
after(afterCallback2)
after(afterCallback3)
})
expect(afterCallback1).not.toHaveBeenCalled()
expect(afterCallback2).not.toHaveBeenCalled()
......@@ -405,9 +401,9 @@ describe('AfterContext', () => {
onClose,
})
const requestStore = createMockRequestStore(afterContext)
const workStore = createMockWorkStore(afterContext)
const run = createRun(afterContext, requestStore)
const run = createRun(afterContext, workStore)
// ==================================
......@@ -434,9 +430,9 @@ describe('AfterContext', () => {
onClose,
})
const requestStore = createMockRequestStore(afterContext)
const workStore = createMockWorkStore(afterContext)
const run = createRun(afterContext, requestStore)
const run = createRun(afterContext, workStore)
// ==================================
......@@ -452,7 +448,7 @@ describe('AfterContext', () => {
expect(afterCallback1).not.toHaveBeenCalled()
})
it('shadows requestAsyncStorage within after callbacks', async () => {
it('does NOT shadow workAsyncStorage within after callbacks', async () => {
const waitUntil = jest.fn()
let onCloseCallback: (() => void) | undefined = undefined
......@@ -465,19 +461,19 @@ describe('AfterContext', () => {
onClose,
})
const requestStore = createMockRequestStore(afterContext)
const run = createRun(afterContext, requestStore)
const workStore = createMockWorkStore(afterContext)
const run = createRun(afterContext, workStore)
// ==================================
const stores = new DetachedPromise<
[RequestStore | undefined, RequestStore | undefined]
[WorkStore | undefined, WorkStore | undefined]
>()
await run(async () => {
const store1 = requestAsyncStorage.getStore()
const store1 = workAsyncStorage.getStore()
after(() => {
const store2 = requestAsyncStorage.getStore()
const store2 = workAsyncStorage.getStore()
stores.resolve([store1, store2])
})
})
......@@ -486,30 +482,34 @@ describe('AfterContext', () => {
onCloseCallback!()
const [store1, store2] = await stores.promise
// if we use .toBe, the proxy from createMockRequestStore throws because jest checks '$$typeof'
// if we use .toBe, the proxy from createMockWorkStore throws because jest checks '$$typeof'
expect(store1).toBeTruthy()
expect(store2).toBeTruthy()
expect(store1 === requestStore).toBe(true)
expect(store2 !== store1).toBe(true)
expect(store1 === workStore).toBe(true)
expect(store2 === store1).toBe(true)
})
})
const createMockRequestStore = (afterContext: AfterContext): RequestStore => {
const partialStore: Partial<RequestStore> = {
url: { pathname: '/', search: '' },
const createMockWorkStore = (afterContext: AfterContext): WorkStore => {
const partialStore: Partial<WorkStore> = {
afterContext: afterContext,
draftMode: undefined,
isHmrRefresh: false,
serverComponentsHmrCache: undefined,
forceStatic: false,
forceDynamic: false,
dynamicShouldError: false,
isStaticGeneration: false,
revalidatedTags: [],
pendingRevalidates: undefined,
pendingRevalidateWrites: undefined,
incrementalCache: undefined,
}
return new Proxy(partialStore as RequestStore, {
return new Proxy(partialStore as WorkStore, {
get(target, key) {
if (key in target) {
return target[key as keyof typeof target]
}
throw new Error(
`RequestStore property not mocked: '${typeof key === 'symbol' ? key.toString() : key}'`
`WorkStore property not mocked: '${typeof key === 'symbol' ? key.toString() : key}'`
)
},
})
......
......@@ -33,11 +33,6 @@ export class AfterContext {
this.callbackQueue.pause()
}
public run<T>(requestStore: RequestStore, callback: () => T): T {
this.requestStore = requestStore
return callback()
}
public after(task: AfterTask): void {
if (isThenable(task)) {
task.catch(() => {}) // avoid unhandled rejection crashes
......@@ -61,9 +56,10 @@ export class AfterContext {
errorWaitUntilNotAvailable()
}
if (!this.requestStore) {
throw new InvariantError(
'unstable_after: Expected `AfterContext.requestStore` to be initialized'
)
// We just stash the first request store we have but this is not sufficient.
// TODO: We should store a request store per callback since each callback might
// be inside a different store. E.g. inside different batched actions, prerenders or caches.
this.requestStore = requestAsyncStorage.getStore()
}
if (!this.onClose) {
throw new InvariantError(
......@@ -98,19 +94,27 @@ export class AfterContext {
private async runCallbacksOnClose() {
await new Promise<void>((resolve) => this.onClose!(resolve))
return this.runCallbacks(this.requestStore!)
return this.runCallbacks(this.requestStore)
}
private async runCallbacks(requestStore: RequestStore): Promise<void> {
private async runCallbacks(
requestStore: undefined | RequestStore
): Promise<void> {
if (this.callbackQueue.size === 0) return
const readonlyRequestStore: RequestStore =
wrapRequestStoreForAfterCallbacks(requestStore)
const readonlyRequestStore: undefined | RequestStore =
requestStore === undefined
? undefined
: // TODO: This is not sufficient. It should just be the same store that mutates.
wrapRequestStoreForAfterCallbacks(requestStore)
const workStore = workAsyncStorage.getStore()
return withExecuteRevalidates(workStore, () =>
requestAsyncStorage.run(readonlyRequestStore, async () => {
// Clearing it out or running the first request store.
// TODO: This needs to be the request store that was active at the time the
// callback was scheduled but p-queue makes this hard so need further refactoring.
requestAsyncStorage.run(readonlyRequestStore as any, async () => {
this.callbackQueue.start()
await this.callbackQueue.onIdle()
})
......@@ -141,7 +145,6 @@ function wrapRequestStoreForAfterCallbacks(
},
// TODO(after): calling a `cookies.set()` in an after() that's in an action doesn't currently error.
mutableCookies: new ResponseCookies(new Headers()),
afterContext: requestStore.afterContext,
isHmrRefresh: requestStore.isHmrRefresh,
serverComponentsHmrCache: requestStore.serverComponentsHmrCache,
}
......
import { requestAsyncStorage } from '../../client/components/request-async-storage.external'
import { workAsyncStorage } from '../../client/components/work-async-storage.external'
import { cacheAsyncStorage } from '../../server/app-render/cache-async-storage.external'
import { StaticGenBailoutError } from '../../client/components/static-generation-bailout'
......@@ -11,28 +10,20 @@ export type AfterCallback<T = unknown> = () => T | Promise<T>
/**
* This function allows you to schedule callbacks to be executed after the current request finishes.
*/
export function unstable_after<T>(task: AfterTask<T>) {
const callingExpression = 'unstable_after'
// TODO: This is not safe. afterContext should move to WorkStore.
const requestStore = requestAsyncStorage.getStore()
if (!requestStore) {
throw new Error(
`\`${callingExpression}\` was called outside a request scope. Read more: https://nextjs.org/docs/messages/next-dynamic-api-wrong-context`
)
}
const { afterContext } = requestStore
if (!afterContext) {
throw new Error(
'`unstable_after()` must be explicitly enabled by setting `experimental.after: true` in your next.config.js.'
)
}
export function unstable_after<T>(task: AfterTask<T>): void {
const workStore = workAsyncStorage.getStore()
const cacheStore = cacheAsyncStorage.getStore()
if (workStore) {
const { afterContext } = workStore
if (!afterContext) {
throw new Error(
'`unstable_after()` must be explicitly enabled by setting `experimental.after: true` in your next.config.js.'
)
}
// TODO: After should not cause dynamic.
const callingExpression = 'unstable_after'
if (workStore.forceStatic) {
throw new StaticGenBailoutError(
`Route ${workStore.route} with \`dynamic = "force-static"\` couldn't be rendered statically because it used \`${callingExpression}\`. See more info here: https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic#dynamic-rendering`
......@@ -40,7 +31,9 @@ export function unstable_after<T>(task: AfterTask<T>) {
} else {
markCurrentScopeAsDynamic(workStore, cacheStore, callingExpression)
}
}
return afterContext.after(task)
afterContext.after(task)
} else {
// TODO: Error for pages?
}
}
......@@ -20,8 +20,6 @@ import {
import { ResponseCookies, RequestCookies } from '../web/spec-extension/cookies'
import { DraftModeProvider } from './draft-mode-provider'
import { splitCookiesString } from '../web/utils'
import { AfterContext } from '../after/after-context'
import type { RequestLifecycleOpts } from '../base-server'
import type { ServerComponentsHmrCache } from '../response-cache'
function getHeaders(headers: Headers | IncomingHttpHeaders): ReadonlyHeaders {
......@@ -41,11 +39,9 @@ function getMutableCookies(
return MutableRequestCookiesAdapter.wrap(cookies, onUpdateCookies)
}
export type WrapperRenderOpts = RequestLifecycleOpts &
Partial<Pick<RenderOpts, 'onUpdateCookies'>> & {
experimental: Pick<RenderOpts['experimental'], 'after'>
previewProps?: __ApiPreviewProps
}
export type WrapperRenderOpts = Partial<Pick<RenderOpts, 'onUpdateCookies'>> & {
previewProps?: __ApiPreviewProps
}
export type RequestContext = RequestResponsePair & {
/**
......@@ -188,34 +184,11 @@ export const withRequestStore: WithStore<RequestStore, RequestContext> = <
return cache.draftMode
},
afterContext: createAfterContext(renderOpts),
isHmrRefresh,
serverComponentsHmrCache:
serverComponentsHmrCache ||
(globalThis as any).__serverComponentsHmrCache,
}
if (store.afterContext) {
return store.afterContext.run(store, () =>
storage.run(store, callback, store)
)
}
return storage.run(store, callback, store)
}
function createAfterContext(
renderOpts: WrapperRenderOpts | undefined
): AfterContext | undefined {
if (!isAfterEnabled(renderOpts)) {
return undefined
}
const { waitUntil, onClose } = renderOpts
return new AfterContext({ waitUntil, onClose })
}
function isAfterEnabled(
renderOpts: WrapperRenderOpts | undefined
): renderOpts is WrapperRenderOpts {
return renderOpts?.experimental?.after ?? false
}
......@@ -8,6 +8,8 @@ import type { RequestLifecycleOpts } from '../base-server'
import type { FallbackRouteParams } from '../request/fallback-params'
import type { AppSegmentConfig } from '../../build/segment-config/app/app-segment-config'
import { AfterContext } from '../after/after-context'
import { normalizeAppPath } from '../../shared/lib/router/utils/app-paths'
export type WorkStoreContext = {
......@@ -118,6 +120,8 @@ export const withWorkStore: WithStore<WorkStore, WorkStoreContext> = <Result>(
buildId: renderOpts.buildId,
reactLoadableManifest: renderOpts?.reactLoadableManifest || {},
assetPrefix: renderOpts?.assetPrefix || '',
afterContext: createAfterContext(renderOpts),
}
// TODO: remove this when we resolve accessing the store outside the execution context
......@@ -125,3 +129,16 @@ export const withWorkStore: WithStore<WorkStore, WorkStoreContext> = <Result>(
return storage.run(store, callback, store)
}
function createAfterContext(
renderOpts: Partial<RequestLifecycleOpts> & {
experimental: Pick<RenderOpts['experimental'], 'after'>
}
): AfterContext | undefined {
const isAfterEnabled = renderOpts?.experimental?.after ?? false
if (!isAfterEnabled) {
return undefined
}
const { waitUntil, onClose } = renderOpts
return new AfterContext({ waitUntil, onClose })
}
......@@ -554,9 +554,6 @@ export class AppRouteRouteModule extends RouteModule<
url: req.nextUrl,
renderOpts: {
previewProps: context.prerenderManifest.preview,
waitUntil: context.renderOpts.waitUntil,
onClose: context.renderOpts.onClose,
experimental: context.renderOpts.experimental,
},
}
......
......@@ -12,12 +12,12 @@ import { stripInternalSearchParams } from '../internal-utils'
import { normalizeRscURL } from '../../shared/lib/router/utils/app-paths'
import { FLIGHT_HEADERS } from '../../client/components/app-router-headers'
import { ensureInstrumentationRegistered } from './globals'
import {
withRequestStore,
type WrapperRenderOpts,
} from '../async-storage/with-request-store'
import { withRequestStore } from '../async-storage/with-request-store'
import { requestAsyncStorage } from '../../client/components/request-async-storage.external'
import { withWorkStore } from '../async-storage/with-work-store'
import {
withWorkStore,
type WorkStoreContext,
} from '../async-storage/with-work-store'
import { workAsyncStorage } from '../../client/components/work-async-storage.external'
import { NEXT_ROUTER_PREFETCH_HEADER } from '../../client/components/app-router-headers'
import { getTracer } from '../lib/trace/tracer'
......@@ -212,11 +212,12 @@ export async function adapter(
// if we're in an edge function, we only get a subset of `nextConfig` (no `experimental`),
// so we have to inject it via DefinePlugin.
// in `next start` this will be passed normally (see `NextNodeServer.runMiddleware`).
const isAfterEnabled =
params.request.nextConfig?.experimental?.after ??
!!process.env.__NEXT_AFTER
let waitUntil: WrapperRenderOpts['waitUntil'] = undefined
let waitUntil: WorkStoreContext['renderOpts']['waitUntil'] = undefined
let closeController: CloseController | undefined = undefined
if (isAfterEnabled) {
......@@ -250,6 +251,10 @@ export async function adapter(
},
buildId: buildId ?? '',
supportsDynamicResponse: true,
waitUntil,
onClose: closeController
? closeController.onClose.bind(closeController)
: undefined,
},
requestEndedState: { ended: false },
isPrefetchRequest: request.headers.has(
......@@ -268,13 +273,6 @@ export async function adapter(
cookiesFromResponse = cookies
},
previewProps,
waitUntil,
onClose: closeController
? closeController.onClose.bind(closeController)
: undefined,
experimental: {
after: isAfterEnabled,
},
},
},
() => params.handler(request, event)
......
......@@ -356,6 +356,7 @@ export function unstable_cache<T extends Callback>(
isStaticGeneration: false,
fallbackRouteParams: null,
buildId: '', // Since this is a fake one it can't "use cache" anyway.
afterContext: undefined, // TODO: Support Pages after()
},
() => cacheAsyncStorage.run(innerCacheStore, cb, ...args)
)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment