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 ...@@ -6,7 +6,6 @@ import type { ReadonlyRequestCookies } from '../../server/web/spec-extension/ada
// Share the instance module in the next-shared layer // Share the instance module in the next-shared layer
import { requestAsyncStorage } from './request-async-storage-instance' with { 'turbopack-transition': 'next-shared' } 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 type { ServerComponentsHmrCache } from '../../server/response-cache'
import { cacheAsyncStorage } from '../../server/app-render/cache-async-storage.external' import { cacheAsyncStorage } from '../../server/app-render/cache-async-storage.external'
...@@ -34,7 +33,6 @@ export interface RequestStore { ...@@ -34,7 +33,6 @@ export interface RequestStore {
readonly cookies: ReadonlyRequestCookies readonly cookies: ReadonlyRequestCookies
readonly mutableCookies: ResponseCookies readonly mutableCookies: ResponseCookies
readonly draftMode: DraftModeProvider readonly draftMode: DraftModeProvider
readonly afterContext: AfterContext | undefined
readonly isHmrRefresh?: boolean readonly isHmrRefresh?: boolean
readonly serverComponentsHmrCache?: ServerComponentsHmrCache readonly serverComponentsHmrCache?: ServerComponentsHmrCache
} }
......
...@@ -6,6 +6,7 @@ import type { Revalidate } from '../../server/lib/revalidate' ...@@ -6,6 +6,7 @@ import type { Revalidate } from '../../server/lib/revalidate'
import type { FallbackRouteParams } from '../../server/request/fallback-params' import type { FallbackRouteParams } from '../../server/request/fallback-params'
import type { DeepReadonly } from '../../shared/lib/deep-readonly' import type { DeepReadonly } from '../../shared/lib/deep-readonly'
import type { AppSegmentConfig } from '../../build/segment-config/app/app-segment-config' 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 // Share the instance module in the next-shared layer
import { workAsyncStorage } from './work-async-storage-instance' with { 'turbopack-transition': 'next-shared' } import { workAsyncStorage } from './work-async-storage-instance' with { 'turbopack-transition': 'next-shared' }
...@@ -43,6 +44,7 @@ export interface WorkStore { ...@@ -43,6 +44,7 @@ export interface WorkStore {
dynamicShouldError?: boolean dynamicShouldError?: boolean
pendingRevalidates?: Record<string, Promise<any>> pendingRevalidates?: Record<string, Promise<any>>
pendingRevalidateWrites?: Array<Promise<void>> // This is like pendingRevalidates but isn't used for deduping. pendingRevalidateWrites?: Array<Promise<void>> // This is like pendingRevalidates but isn't used for deduping.
readonly afterContext: AfterContext | undefined
dynamicUsageDescription?: string dynamicUsageDescription?: string
dynamicUsageStack?: string dynamicUsageStack?: string
......
import { DetachedPromise } from '../../lib/detached-promise' import { DetachedPromise } from '../../lib/detached-promise'
import { AsyncLocalStorage } from 'async_hooks' 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' import type { AfterContext } from './after-context'
describe('AfterContext', () => { describe('AfterContext', () => {
// 'async-local-storage.ts' needs `AsyncLocalStorage` on `globalThis` at import time, // '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 // so we have to do some contortions here to set it up before running anything else
type RASMod = type WASMod =
typeof import('../../client/components/request-async-storage.external') typeof import('../../client/components/work-async-storage.external')
type AfterMod = typeof import('./after') type AfterMod = typeof import('./after')
type AfterContextMod = typeof import('./after-context') type AfterContextMod = typeof import('./after-context')
let requestAsyncStorage: RASMod['requestAsyncStorage'] let workAsyncStorage: WASMod['workAsyncStorage']
let AfterContext: AfterContextMod['AfterContext'] let AfterContext: AfterContextMod['AfterContext']
let after: AfterMod['unstable_after'] let after: AfterMod['unstable_after']
...@@ -20,10 +20,10 @@ describe('AfterContext', () => { ...@@ -20,10 +20,10 @@ describe('AfterContext', () => {
// @ts-expect-error // @ts-expect-error
globalThis.AsyncLocalStorage = AsyncLocalStorage globalThis.AsyncLocalStorage = AsyncLocalStorage
const RASMod = await import( const WASMod = await import(
'../../client/components/request-async-storage.external' '../../client/components/work-async-storage.external'
) )
requestAsyncStorage = RASMod.requestAsyncStorage workAsyncStorage = WASMod.workAsyncStorage
const AfterContextMod = await import('./after-context') const AfterContextMod = await import('./after-context')
AfterContext = AfterContextMod.AfterContext AfterContext = AfterContextMod.AfterContext
...@@ -33,11 +33,9 @@ describe('AfterContext', () => { ...@@ -33,11 +33,9 @@ describe('AfterContext', () => {
}) })
const createRun = const createRun =
(afterContext: AfterContext, requestStore: RequestStore) => (_afterContext: AfterContext, workStore: WorkStore) =>
<T>(cb: () => T): T => { <T>(cb: () => T): T => {
return afterContext.run(requestStore, () => return workAsyncStorage.run(workStore, cb)
requestAsyncStorage.run(requestStore, cb)
)
} }
it('runs after() callbacks from a run() callback that resolves', async () => { it('runs after() callbacks from a run() callback that resolves', async () => {
...@@ -54,8 +52,8 @@ describe('AfterContext', () => { ...@@ -54,8 +52,8 @@ describe('AfterContext', () => {
onClose, onClose,
}) })
const requestStore = createMockRequestStore(afterContext) const workStore = createMockWorkStore(afterContext)
const run = createRun(afterContext, requestStore) const run = createRun(afterContext, workStore)
// ================================== // ==================================
...@@ -120,9 +118,9 @@ describe('AfterContext', () => { ...@@ -120,9 +118,9 @@ describe('AfterContext', () => {
onClose, 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', () => { ...@@ -167,9 +165,9 @@ describe('AfterContext', () => {
onClose, 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', () => { ...@@ -257,8 +255,8 @@ describe('AfterContext', () => {
onClose, onClose,
}) })
const requestStore = createMockRequestStore(afterContext) const workStore = createMockWorkStore(afterContext)
const run = createRun(afterContext, requestStore) const run = createRun(afterContext, workStore)
// ================================== // ==================================
...@@ -316,9 +314,9 @@ describe('AfterContext', () => { ...@@ -316,9 +314,9 @@ describe('AfterContext', () => {
onClose, 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', () => { ...@@ -353,7 +351,7 @@ describe('AfterContext', () => {
onClose, onClose,
}) })
const requestStore = createMockRequestStore(afterContext) const workStore = createMockWorkStore(afterContext)
// ================================== // ==================================
...@@ -367,13 +365,11 @@ describe('AfterContext', () => { ...@@ -367,13 +365,11 @@ describe('AfterContext', () => {
const promise3 = new DetachedPromise<string>() const promise3 = new DetachedPromise<string>()
const afterCallback3 = jest.fn(() => promise3.promise) const afterCallback3 = jest.fn(() => promise3.promise)
requestAsyncStorage.run(requestStore, () => workAsyncStorage.run(workStore, () => {
afterContext.run(requestStore, () => { after(afterCallback1)
after(afterCallback1) after(afterCallback2)
after(afterCallback2) after(afterCallback3)
after(afterCallback3) })
})
)
expect(afterCallback1).not.toHaveBeenCalled() expect(afterCallback1).not.toHaveBeenCalled()
expect(afterCallback2).not.toHaveBeenCalled() expect(afterCallback2).not.toHaveBeenCalled()
...@@ -405,9 +401,9 @@ describe('AfterContext', () => { ...@@ -405,9 +401,9 @@ describe('AfterContext', () => {
onClose, 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', () => { ...@@ -434,9 +430,9 @@ describe('AfterContext', () => {
onClose, 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', () => { ...@@ -452,7 +448,7 @@ describe('AfterContext', () => {
expect(afterCallback1).not.toHaveBeenCalled() expect(afterCallback1).not.toHaveBeenCalled()
}) })
it('shadows requestAsyncStorage within after callbacks', async () => { it('does NOT shadow workAsyncStorage within after callbacks', async () => {
const waitUntil = jest.fn() const waitUntil = jest.fn()
let onCloseCallback: (() => void) | undefined = undefined let onCloseCallback: (() => void) | undefined = undefined
...@@ -465,19 +461,19 @@ describe('AfterContext', () => { ...@@ -465,19 +461,19 @@ describe('AfterContext', () => {
onClose, onClose,
}) })
const requestStore = createMockRequestStore(afterContext) const workStore = createMockWorkStore(afterContext)
const run = createRun(afterContext, requestStore) const run = createRun(afterContext, workStore)
// ================================== // ==================================
const stores = new DetachedPromise< const stores = new DetachedPromise<
[RequestStore | undefined, RequestStore | undefined] [WorkStore | undefined, WorkStore | undefined]
>() >()
await run(async () => { await run(async () => {
const store1 = requestAsyncStorage.getStore() const store1 = workAsyncStorage.getStore()
after(() => { after(() => {
const store2 = requestAsyncStorage.getStore() const store2 = workAsyncStorage.getStore()
stores.resolve([store1, store2]) stores.resolve([store1, store2])
}) })
}) })
...@@ -486,30 +482,34 @@ describe('AfterContext', () => { ...@@ -486,30 +482,34 @@ describe('AfterContext', () => {
onCloseCallback!() onCloseCallback!()
const [store1, store2] = await stores.promise 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(store1).toBeTruthy()
expect(store2).toBeTruthy() expect(store2).toBeTruthy()
expect(store1 === requestStore).toBe(true) expect(store1 === workStore).toBe(true)
expect(store2 !== store1).toBe(true) expect(store2 === store1).toBe(true)
}) })
}) })
const createMockRequestStore = (afterContext: AfterContext): RequestStore => { const createMockWorkStore = (afterContext: AfterContext): WorkStore => {
const partialStore: Partial<RequestStore> = { const partialStore: Partial<WorkStore> = {
url: { pathname: '/', search: '' },
afterContext: afterContext, afterContext: afterContext,
draftMode: undefined, forceStatic: false,
isHmrRefresh: false, forceDynamic: false,
serverComponentsHmrCache: undefined, 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) { get(target, key) {
if (key in target) { if (key in target) {
return target[key as keyof typeof target] return target[key as keyof typeof target]
} }
throw new Error( 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 { ...@@ -33,11 +33,6 @@ export class AfterContext {
this.callbackQueue.pause() this.callbackQueue.pause()
} }
public run<T>(requestStore: RequestStore, callback: () => T): T {
this.requestStore = requestStore
return callback()
}
public after(task: AfterTask): void { public after(task: AfterTask): void {
if (isThenable(task)) { if (isThenable(task)) {
task.catch(() => {}) // avoid unhandled rejection crashes task.catch(() => {}) // avoid unhandled rejection crashes
...@@ -61,9 +56,10 @@ export class AfterContext { ...@@ -61,9 +56,10 @@ export class AfterContext {
errorWaitUntilNotAvailable() errorWaitUntilNotAvailable()
} }
if (!this.requestStore) { if (!this.requestStore) {
throw new InvariantError( // We just stash the first request store we have but this is not sufficient.
'unstable_after: Expected `AfterContext.requestStore` to be initialized' // 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) { if (!this.onClose) {
throw new InvariantError( throw new InvariantError(
...@@ -98,19 +94,27 @@ export class AfterContext { ...@@ -98,19 +94,27 @@ export class AfterContext {
private async runCallbacksOnClose() { private async runCallbacksOnClose() {
await new Promise<void>((resolve) => this.onClose!(resolve)) 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 if (this.callbackQueue.size === 0) return
const readonlyRequestStore: RequestStore = const readonlyRequestStore: undefined | RequestStore =
wrapRequestStoreForAfterCallbacks(requestStore) requestStore === undefined
? undefined
: // TODO: This is not sufficient. It should just be the same store that mutates.
wrapRequestStoreForAfterCallbacks(requestStore)
const workStore = workAsyncStorage.getStore() const workStore = workAsyncStorage.getStore()
return withExecuteRevalidates(workStore, () => 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() this.callbackQueue.start()
await this.callbackQueue.onIdle() await this.callbackQueue.onIdle()
}) })
...@@ -141,7 +145,6 @@ function wrapRequestStoreForAfterCallbacks( ...@@ -141,7 +145,6 @@ function wrapRequestStoreForAfterCallbacks(
}, },
// TODO(after): calling a `cookies.set()` in an after() that's in an action doesn't currently error. // TODO(after): calling a `cookies.set()` in an after() that's in an action doesn't currently error.
mutableCookies: new ResponseCookies(new Headers()), mutableCookies: new ResponseCookies(new Headers()),
afterContext: requestStore.afterContext,
isHmrRefresh: requestStore.isHmrRefresh, isHmrRefresh: requestStore.isHmrRefresh,
serverComponentsHmrCache: requestStore.serverComponentsHmrCache, serverComponentsHmrCache: requestStore.serverComponentsHmrCache,
} }
......
import { requestAsyncStorage } from '../../client/components/request-async-storage.external'
import { workAsyncStorage } from '../../client/components/work-async-storage.external' import { workAsyncStorage } from '../../client/components/work-async-storage.external'
import { cacheAsyncStorage } from '../../server/app-render/cache-async-storage.external' import { cacheAsyncStorage } from '../../server/app-render/cache-async-storage.external'
import { StaticGenBailoutError } from '../../client/components/static-generation-bailout' import { StaticGenBailoutError } from '../../client/components/static-generation-bailout'
...@@ -11,28 +10,20 @@ export type AfterCallback<T = unknown> = () => T | Promise<T> ...@@ -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. * This function allows you to schedule callbacks to be executed after the current request finishes.
*/ */
export function unstable_after<T>(task: AfterTask<T>) { export function unstable_after<T>(task: AfterTask<T>): void {
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.'
)
}
const workStore = workAsyncStorage.getStore() const workStore = workAsyncStorage.getStore()
const cacheStore = cacheAsyncStorage.getStore() const cacheStore = cacheAsyncStorage.getStore()
if (workStore) { 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) { if (workStore.forceStatic) {
throw new StaticGenBailoutError( 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` `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>) { ...@@ -40,7 +31,9 @@ export function unstable_after<T>(task: AfterTask<T>) {
} else { } else {
markCurrentScopeAsDynamic(workStore, cacheStore, callingExpression) markCurrentScopeAsDynamic(workStore, cacheStore, callingExpression)
} }
}
return afterContext.after(task) afterContext.after(task)
} else {
// TODO: Error for pages?
}
} }
...@@ -20,8 +20,6 @@ import { ...@@ -20,8 +20,6 @@ import {
import { ResponseCookies, RequestCookies } from '../web/spec-extension/cookies' import { ResponseCookies, RequestCookies } from '../web/spec-extension/cookies'
import { DraftModeProvider } from './draft-mode-provider' import { DraftModeProvider } from './draft-mode-provider'
import { splitCookiesString } from '../web/utils' import { splitCookiesString } from '../web/utils'
import { AfterContext } from '../after/after-context'
import type { RequestLifecycleOpts } from '../base-server'
import type { ServerComponentsHmrCache } from '../response-cache' import type { ServerComponentsHmrCache } from '../response-cache'
function getHeaders(headers: Headers | IncomingHttpHeaders): ReadonlyHeaders { function getHeaders(headers: Headers | IncomingHttpHeaders): ReadonlyHeaders {
...@@ -41,11 +39,9 @@ function getMutableCookies( ...@@ -41,11 +39,9 @@ function getMutableCookies(
return MutableRequestCookiesAdapter.wrap(cookies, onUpdateCookies) return MutableRequestCookiesAdapter.wrap(cookies, onUpdateCookies)
} }
export type WrapperRenderOpts = RequestLifecycleOpts & export type WrapperRenderOpts = Partial<Pick<RenderOpts, 'onUpdateCookies'>> & {
Partial<Pick<RenderOpts, 'onUpdateCookies'>> & { previewProps?: __ApiPreviewProps
experimental: Pick<RenderOpts['experimental'], 'after'> }
previewProps?: __ApiPreviewProps
}
export type RequestContext = RequestResponsePair & { export type RequestContext = RequestResponsePair & {
/** /**
...@@ -188,34 +184,11 @@ export const withRequestStore: WithStore<RequestStore, RequestContext> = < ...@@ -188,34 +184,11 @@ export const withRequestStore: WithStore<RequestStore, RequestContext> = <
return cache.draftMode return cache.draftMode
}, },
afterContext: createAfterContext(renderOpts),
isHmrRefresh, isHmrRefresh,
serverComponentsHmrCache: serverComponentsHmrCache:
serverComponentsHmrCache || serverComponentsHmrCache ||
(globalThis as any).__serverComponentsHmrCache, (globalThis as any).__serverComponentsHmrCache,
} }
if (store.afterContext) {
return store.afterContext.run(store, () =>
storage.run(store, callback, store)
)
}
return 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' ...@@ -8,6 +8,8 @@ import type { RequestLifecycleOpts } from '../base-server'
import type { FallbackRouteParams } from '../request/fallback-params' import type { FallbackRouteParams } from '../request/fallback-params'
import type { AppSegmentConfig } from '../../build/segment-config/app/app-segment-config' 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' import { normalizeAppPath } from '../../shared/lib/router/utils/app-paths'
export type WorkStoreContext = { export type WorkStoreContext = {
...@@ -118,6 +120,8 @@ export const withWorkStore: WithStore<WorkStore, WorkStoreContext> = <Result>( ...@@ -118,6 +120,8 @@ export const withWorkStore: WithStore<WorkStore, WorkStoreContext> = <Result>(
buildId: renderOpts.buildId, buildId: renderOpts.buildId,
reactLoadableManifest: renderOpts?.reactLoadableManifest || {}, reactLoadableManifest: renderOpts?.reactLoadableManifest || {},
assetPrefix: renderOpts?.assetPrefix || '', assetPrefix: renderOpts?.assetPrefix || '',
afterContext: createAfterContext(renderOpts),
} }
// TODO: remove this when we resolve accessing the store outside the execution context // TODO: remove this when we resolve accessing the store outside the execution context
...@@ -125,3 +129,16 @@ export const withWorkStore: WithStore<WorkStore, WorkStoreContext> = <Result>( ...@@ -125,3 +129,16 @@ export const withWorkStore: WithStore<WorkStore, WorkStoreContext> = <Result>(
return storage.run(store, callback, store) 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< ...@@ -554,9 +554,6 @@ export class AppRouteRouteModule extends RouteModule<
url: req.nextUrl, url: req.nextUrl,
renderOpts: { renderOpts: {
previewProps: context.prerenderManifest.preview, 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' ...@@ -12,12 +12,12 @@ import { stripInternalSearchParams } from '../internal-utils'
import { normalizeRscURL } from '../../shared/lib/router/utils/app-paths' import { normalizeRscURL } from '../../shared/lib/router/utils/app-paths'
import { FLIGHT_HEADERS } from '../../client/components/app-router-headers' import { FLIGHT_HEADERS } from '../../client/components/app-router-headers'
import { ensureInstrumentationRegistered } from './globals' import { ensureInstrumentationRegistered } from './globals'
import { import { withRequestStore } from '../async-storage/with-request-store'
withRequestStore,
type WrapperRenderOpts,
} from '../async-storage/with-request-store'
import { requestAsyncStorage } from '../../client/components/request-async-storage.external' 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 { workAsyncStorage } from '../../client/components/work-async-storage.external'
import { NEXT_ROUTER_PREFETCH_HEADER } from '../../client/components/app-router-headers' import { NEXT_ROUTER_PREFETCH_HEADER } from '../../client/components/app-router-headers'
import { getTracer } from '../lib/trace/tracer' import { getTracer } from '../lib/trace/tracer'
...@@ -212,11 +212,12 @@ export async function adapter( ...@@ -212,11 +212,12 @@ export async function adapter(
// if we're in an edge function, we only get a subset of `nextConfig` (no `experimental`), // if we're in an edge function, we only get a subset of `nextConfig` (no `experimental`),
// so we have to inject it via DefinePlugin. // so we have to inject it via DefinePlugin.
// in `next start` this will be passed normally (see `NextNodeServer.runMiddleware`). // in `next start` this will be passed normally (see `NextNodeServer.runMiddleware`).
const isAfterEnabled = const isAfterEnabled =
params.request.nextConfig?.experimental?.after ?? params.request.nextConfig?.experimental?.after ??
!!process.env.__NEXT_AFTER !!process.env.__NEXT_AFTER
let waitUntil: WrapperRenderOpts['waitUntil'] = undefined let waitUntil: WorkStoreContext['renderOpts']['waitUntil'] = undefined
let closeController: CloseController | undefined = undefined let closeController: CloseController | undefined = undefined
if (isAfterEnabled) { if (isAfterEnabled) {
...@@ -250,6 +251,10 @@ export async function adapter( ...@@ -250,6 +251,10 @@ export async function adapter(
}, },
buildId: buildId ?? '', buildId: buildId ?? '',
supportsDynamicResponse: true, supportsDynamicResponse: true,
waitUntil,
onClose: closeController
? closeController.onClose.bind(closeController)
: undefined,
}, },
requestEndedState: { ended: false }, requestEndedState: { ended: false },
isPrefetchRequest: request.headers.has( isPrefetchRequest: request.headers.has(
...@@ -268,13 +273,6 @@ export async function adapter( ...@@ -268,13 +273,6 @@ export async function adapter(
cookiesFromResponse = cookies cookiesFromResponse = cookies
}, },
previewProps, previewProps,
waitUntil,
onClose: closeController
? closeController.onClose.bind(closeController)
: undefined,
experimental: {
after: isAfterEnabled,
},
}, },
}, },
() => params.handler(request, event) () => params.handler(request, event)
......
...@@ -356,6 +356,7 @@ export function unstable_cache<T extends Callback>( ...@@ -356,6 +356,7 @@ export function unstable_cache<T extends Callback>(
isStaticGeneration: false, isStaticGeneration: false,
fallbackRouteParams: null, fallbackRouteParams: null,
buildId: '', // Since this is a fake one it can't "use cache" anyway. buildId: '', // Since this is a fake one it can't "use cache" anyway.
afterContext: undefined, // TODO: Support Pages after()
}, },
() => cacheAsyncStorage.run(innerCacheStore, cb, ...args) () => 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