Unverified Commit 56b201e2 authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

Refactor use of AsyncLocalStorage (#44668)

Previously we were targeting lower versions of Node.js for `app` directory support, but a higher version of Node.js (with support for `AsyncLocalStorage`) was made required to use `undici` instead of `node-fetch`. This PR serves to:

1. Simplify the usage of those storage objects by removing the unnecessary fallback code. Everything now just uses the native `AsyncLocalStorage` interface.
2. Isolates the initialization of each store within it's own bound context to prevent leakage.
3. Adds immutibility flags to variables that should only be initlaized once upon store creation to prevent errors. This actually revealed some bugs in the implementation that were corrected.

In the event that this was in error, the following adapter could be used to simulate the previous beheviour with minor changes to the present implementation ([see this gist](https://gist.github.com/wyattjoh/6f3dffb99a4ac1a8254c90284f05026a)).

[Slack Reference](https://vercel.slack.com/archives/C04DUD7EB1B/p1673030967568029)
parent bc90465f
import { AsyncLocalStorage } from 'async_hooks'
class FakeAsyncLocalStorage<Store extends {}>
implements AsyncLocalStorage<Store>
{
disable(): void {
throw new Error(
'Invariant: AsyncLocalStorage accessed in runtime where it is not available'
)
}
getStore(): Store | undefined {
// This fake implementation of AsyncLocalStorage always returns `undefined`.
return undefined
}
run<R>(): R {
throw new Error(
'Invariant: AsyncLocalStorage accessed in runtime where it is not available'
)
}
exit<R>(): R {
throw new Error(
'Invariant: AsyncLocalStorage accessed in runtime where it is not available'
)
}
enterWith(): void {
throw new Error(
'Invariant: AsyncLocalStorage accessed in runtime where it is not available'
)
}
}
export function createAsyncLocalStorage<
Store extends {}
>(): AsyncLocalStorage<Store> {
if ((globalThis as any).AsyncLocalStorage) {
return new (globalThis as any).AsyncLocalStorage()
}
return new FakeAsyncLocalStorage()
}
......@@ -2,10 +2,7 @@ import { suspense } from '../../shared/lib/dynamic-no-ssr'
import { staticGenerationAsyncStorage } from './static-generation-async-storage'
export function bailoutToClientRendering(): boolean | never {
const staticGenerationStore =
staticGenerationAsyncStorage && 'getStore' in staticGenerationAsyncStorage
? staticGenerationAsyncStorage?.getStore()
: staticGenerationAsyncStorage
const staticGenerationStore = staticGenerationAsyncStorage.getStore()
if (staticGenerationStore?.forceStatic) {
return true
......
......@@ -7,19 +7,24 @@ export function headers() {
return new Headers({})
}
const requestStore =
requestAsyncStorage && 'getStore' in requestAsyncStorage
? requestAsyncStorage.getStore()!
: requestAsyncStorage
const requestStore = requestAsyncStorage.getStore()
if (!requestStore) {
throw new Error(
`Invariant: Method expects to have requestAsyncStorage, none available`
)
}
return requestStore.headers
}
export function previewData() {
const requestStore =
requestAsyncStorage && 'getStore' in requestAsyncStorage
? requestAsyncStorage.getStore()!
: requestAsyncStorage
const requestStore = requestAsyncStorage.getStore()
if (!requestStore) {
throw new Error(
`Invariant: Method expects to have requestAsyncStorage, none available`
)
}
return requestStore.previewData
}
......@@ -27,10 +32,13 @@ export function cookies() {
if (staticGenerationBailout('cookies')) {
return new RequestCookies(new Headers({}))
}
const requestStore =
requestAsyncStorage && 'getStore' in requestAsyncStorage
? requestAsyncStorage.getStore()!
: requestAsyncStorage
const requestStore = requestAsyncStorage.getStore()
if (!requestStore) {
throw new Error(
`Invariant: Method expects to have requestAsyncStorage, none available`
)
}
return requestStore.cookies
}
import type { AsyncLocalStorage } from 'async_hooks'
import { PreviewData } from '../../../types'
import type {
ReadonlyHeaders,
ReadonlyRequestCookies,
} from '../../server/app-render'
import { createAsyncLocalStorage } from './async-local-storage'
export interface RequestStore {
headers: ReadonlyHeaders
cookies: ReadonlyRequestCookies
previewData: any
readonly headers: ReadonlyHeaders
readonly cookies: ReadonlyRequestCookies
readonly previewData: PreviewData
}
export let requestAsyncStorage: AsyncLocalStorage<RequestStore> | RequestStore =
{} as any
export type RequestAsyncStorage = AsyncLocalStorage<RequestStore>
// @ts-expect-error we provide this on globalThis in
// the edge and node runtime
if (globalThis.AsyncLocalStorage) {
requestAsyncStorage = new (globalThis as any).AsyncLocalStorage()
}
export const requestAsyncStorage: RequestAsyncStorage =
createAsyncLocalStorage()
import type { AsyncLocalStorage } from 'async_hooks'
import { createAsyncLocalStorage } from './async-local-storage'
export interface StaticGenerationStore {
inUse?: boolean
pathname?: string
readonly isStaticGeneration: boolean
readonly pathname: string
readonly incrementalCache?: import('../../server/lib/incremental-cache').IncrementalCache
readonly isRevalidate?: boolean
revalidate?: number
fetchRevalidate?: number
isStaticGeneration?: boolean
forceDynamic?: boolean
fetchRevalidate?: boolean | number
forceStatic?: boolean
incrementalCache?: import('../../server/lib/incremental-cache').IncrementalCache
pendingRevalidates?: Promise<any>[]
isRevalidate?: boolean
}
export let staticGenerationAsyncStorage:
| AsyncLocalStorage<StaticGenerationStore>
| StaticGenerationStore = {}
export type StaticGenerationAsyncStorage =
AsyncLocalStorage<StaticGenerationStore>
// @ts-expect-error we provide this on globalThis in
// the edge and node runtime
if (globalThis.AsyncLocalStorage) {
staticGenerationAsyncStorage = new (globalThis as any).AsyncLocalStorage()
}
export const staticGenerationAsyncStorage: StaticGenerationAsyncStorage =
createAsyncLocalStorage()
......@@ -2,10 +2,7 @@ import { DynamicServerError } from './hooks-server-context'
import { staticGenerationAsyncStorage } from './static-generation-async-storage'
export function staticGenerationBailout(reason: string): boolean | never {
const staticGenerationStore =
staticGenerationAsyncStorage && 'getStore' in staticGenerationAsyncStorage
? staticGenerationAsyncStorage?.getStore()
: staticGenerationAsyncStorage
const staticGenerationStore = staticGenerationAsyncStorage.getStore()
if (staticGenerationStore?.forceStatic) {
return true
......
This diff is collapsed.
import type { IncomingHttpHeaders, IncomingMessage, ServerResponse } from 'http'
import type { PreviewData } from 'next/types'
import { FLIGHT_PARAMETERS } from '../client/components/app-router-headers'
import type {
RequestStore,
RequestAsyncStorage,
} from '../client/components/request-async-storage'
import {
ReadonlyHeaders,
ReadonlyRequestCookies,
type RenderOpts,
} from './app-render'
function headersWithoutFlight(headers: IncomingHttpHeaders) {
const newHeaders = { ...headers }
for (const param of FLIGHT_PARAMETERS) {
delete newHeaders[param.toString().toLowerCase()]
}
return newHeaders
}
type RunWithRequestAsyncStorageContext = {
req: IncomingMessage
res: ServerResponse
renderOpts?: RenderOpts
}
export function runWithRequestAsyncStorage<Result>(
requestAsyncStorage: RequestAsyncStorage,
{ req, res, renderOpts }: RunWithRequestAsyncStorageContext,
callback: () => Promise<Result>
): Promise<Result> {
const tryGetPreviewData =
process.env.NEXT_RUNTIME === 'edge'
? () => false
: require('./api-utils/node').tryGetPreviewData
// Reads of this are cached on the `req` object, so this should resolve
// instantly. There's no need to pass this data down from a previous
// invoke, where we'd have to consider server & serverless.
const previewData: PreviewData = renderOpts
? // TODO: investigate why previewProps isn't on RenderOpts
tryGetPreviewData(req, res, (renderOpts as any).previewProps)
: false
let cachedHeadersInstance: ReadonlyHeaders
let cachedCookiesInstance: ReadonlyRequestCookies
const store: RequestStore = {
get headers() {
if (!cachedHeadersInstance) {
cachedHeadersInstance = new ReadonlyHeaders(
headersWithoutFlight(req.headers)
)
}
return cachedHeadersInstance
},
get cookies() {
if (!cachedCookiesInstance) {
cachedCookiesInstance = new ReadonlyRequestCookies({
headers: {
get: (key) => {
if (key !== 'cookie') {
throw new Error('Only cookie header is supported')
}
return req.headers.cookie
},
},
})
}
return cachedCookiesInstance
},
previewData,
}
return requestAsyncStorage.run(store, callback)
}
import type {
StaticGenerationStore,
StaticGenerationAsyncStorage,
} from '../client/components/static-generation-async-storage'
import type { RenderOpts } from './app-render'
type RunWithStaticGenerationAsyncStorageContext = {
pathname: string
renderOpts: RenderOpts
}
export function runWithStaticGenerationAsyncStorage<Result>(
staticGenerationAsyncStorage: StaticGenerationAsyncStorage,
{ pathname, renderOpts }: RunWithStaticGenerationAsyncStorageContext,
callback: () => Promise<Result>
): Promise<Result> {
/**
* Rules of Static & Dynamic HTML:
*
* 1.) We must generate static HTML unless the caller explicitly opts
* in to dynamic HTML support.
*
* 2.) If dynamic HTML support is requested, we must honor that request
* or throw an error. It is the sole responsibility of the caller to
* ensure they aren't e.g. requesting dynamic HTML for an AMP page.
*
* These rules help ensure that other existing features like request caching,
* coalescing, and ISR continue working as intended.
*/
const isStaticGeneration =
renderOpts.supportsDynamicHTML !== true && !renderOpts.isBot
const store: StaticGenerationStore = {
isStaticGeneration,
pathname,
incrementalCache: renderOpts.incrementalCache,
isRevalidate: renderOpts.isRevalidate,
}
return staticGenerationAsyncStorage.run(store, callback)
}
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