diff --git a/packages/next/src/server/after/after-context.ts b/packages/next/src/server/after/after-context.ts index c7f74fde9cebf..7eccb40fbc24e 100644 --- a/packages/next/src/server/after/after-context.ts +++ b/packages/next/src/server/after/after-context.ts @@ -10,7 +10,7 @@ import { workUnitAsyncStorage, type WorkUnitStore, } from '../app-render/work-unit-async-storage.external' -import { workUnitSnapshotAsyncStorage } from '../app-render/work-unit-snapshot-async-storage.external' +import { afterTaskAsyncStorage } from '../app-render/after-task-async-storage.external' export type AfterContextOpts = { isEnabled: boolean @@ -89,7 +89,7 @@ export class AfterContext { if (!originalPhase) { await callback() } else { - await workUnitSnapshotAsyncStorage.run({ phase: originalPhase }, () => + await afterTaskAsyncStorage.run({ phase: originalPhase }, () => callback() ) } diff --git a/packages/next/src/server/app-render/after-task-async-storage-instance.ts b/packages/next/src/server/app-render/after-task-async-storage-instance.ts new file mode 100644 index 0000000000000..9c6f18720e848 --- /dev/null +++ b/packages/next/src/server/app-render/after-task-async-storage-instance.ts @@ -0,0 +1,5 @@ +import type { AfterTaskAsyncStorage } from './after-task-async-storage.external' +import { createAsyncLocalStorage } from './async-local-storage' + +export const _afterTaskAsyncStorage: AfterTaskAsyncStorage = + createAsyncLocalStorage() diff --git a/packages/next/src/server/app-render/after-task-async-storage.external.ts b/packages/next/src/server/app-render/after-task-async-storage.external.ts new file mode 100644 index 0000000000000..01bbdbade6c7f --- /dev/null +++ b/packages/next/src/server/app-render/after-task-async-storage.external.ts @@ -0,0 +1,13 @@ +import type { AsyncLocalStorage } from 'async_hooks' + +// Share the instance module in the next-shared layer +import { _afterTaskAsyncStorage as afterTaskAsyncStorage } from './after-task-async-storage-instance' with { 'turbopack-transition': 'next-shared' } +import type { WorkUnitStore } from './work-unit-async-storage.external' + +export interface AfterTaskStore { + readonly phase: WorkUnitStore['phase'] +} + +export type AfterTaskAsyncStorage = AsyncLocalStorage + +export { afterTaskAsyncStorage } diff --git a/packages/next/src/server/app-render/work-unit-snapshot-async-storage-instance.ts b/packages/next/src/server/app-render/work-unit-snapshot-async-storage-instance.ts deleted file mode 100644 index 1a755f7b8e8c5..0000000000000 --- a/packages/next/src/server/app-render/work-unit-snapshot-async-storage-instance.ts +++ /dev/null @@ -1,5 +0,0 @@ -import type { WorkUnitSnapshotAsyncStorage } from './work-unit-snapshot-async-storage.external' -import { createAsyncLocalStorage } from './async-local-storage' - -export const _workUnitSnapshotAsyncStorage: WorkUnitSnapshotAsyncStorage = - createAsyncLocalStorage() diff --git a/packages/next/src/server/app-render/work-unit-snapshot-async-storage.external.ts b/packages/next/src/server/app-render/work-unit-snapshot-async-storage.external.ts deleted file mode 100644 index 2d6980aa141fa..0000000000000 --- a/packages/next/src/server/app-render/work-unit-snapshot-async-storage.external.ts +++ /dev/null @@ -1,14 +0,0 @@ -import type { AsyncLocalStorage } from 'async_hooks' - -// Share the instance module in the next-shared layer -import { _workUnitSnapshotAsyncStorage as workUnitSnapshotAsyncStorage } from './work-unit-snapshot-async-storage-instance' with { 'turbopack-transition': 'next-shared' } -import type { WorkUnitStore } from './work-unit-async-storage.external' - -export interface WorkUnitSnapshotStore { - readonly phase: WorkUnitStore['phase'] -} - -export type WorkUnitSnapshotAsyncStorage = - AsyncLocalStorage - -export { workUnitSnapshotAsyncStorage } diff --git a/packages/next/src/server/request/utils.ts b/packages/next/src/server/request/utils.ts index 27c19669e53b9..e375a2985835b 100644 --- a/packages/next/src/server/request/utils.ts +++ b/packages/next/src/server/request/utils.ts @@ -1,5 +1,5 @@ import { StaticGenBailoutError } from '../../client/components/static-generation-bailout' -import { workUnitSnapshotAsyncStorage } from '../app-render/work-unit-snapshot-async-storage.external' +import { afterTaskAsyncStorage } from '../app-render/after-task-async-storage.external' // This regex will have fast negatives meaning valid identifiers may not pass // this test. However this is only used during static generation to provide hints @@ -44,10 +44,10 @@ export function throwWithStaticGenerationBailoutErrorWithDynamicError( export function isRequestAPICallableInsideAfter() { // const workStore = workAsyncStorage.getStore() // const workUnitStore = workUnitAsyncStorage.getStore() - const workUnitSnapshotStore = workUnitSnapshotAsyncStorage.getStore() + const afterTaskStore = afterTaskAsyncStorage.getStore() return ( // workUnitStore?.type === 'request' && - workUnitSnapshotStore?.phase === 'action' + afterTaskStore?.phase === 'action' ) }