Download nextjs-cache-after-prerender-abort/solution/gold.patch from dari-ai/nextjs-selfbench: direct link, hf CLI and curl.
- Browser
- Download file 4.61 kB
-
https://huggingface.co/datasets/dari-ai/nextjs-selfbench/resolve/main/nextjs-cache-after-prerender-abort/solution/gold.patch
- Command line
-
hf download hf://datasets/dari-ai/nextjs-selfbench/nextjs-cache-after-prerender-abort/solution/gold.patch
-
curl -L -o gold.patch https://huggingface.co/datasets/dari-ai/nextjs-selfbench/resolve/main/nextjs-cache-after-prerender-abort/solution/gold.patch
4.61 kB
| diff --git a/packages/next/src/server/dynamic-rendering-utils.ts b/packages/next/src/server/dynamic-rendering-utils.ts | |
| index abc5e7ae92a7752c9124896121450fb1a601b57b..f5b086cdeabecf1828d49a7812db93b76efce7bc 100644 | |
| --- a/packages/next/src/server/dynamic-rendering-utils.ts | |
| +++ b/packages/next/src/server/dynamic-rendering-utils.ts | |
| export function makeDynamicHangingPromise<T>( | |
| ) | |
| } | |
| +export function makeUntrackedHangingPromise<T>( | |
| + signal: AbortSignal, | |
| + route: string, | |
| + expression: string | |
| +): Promise<T> { | |
| + return makeHangingPromiseWithError( | |
| + signal, | |
| + new HangingPromiseRejectionError(route, expression) | |
| + ) | |
| +} | |
| + | |
| /** | |
| * Constructs a promise that never resolves, standing in for *runtime* data: | |
| * data that hangs during a static prerender but is available during a runtime | |
| diff --git a/packages/next/src/server/use-cache/use-cache-wrapper.ts b/packages/next/src/server/use-cache/use-cache-wrapper.ts | |
| index 71935aab3d6e8272f90231e9153e5cc4caa2c7b1..7ee8be8c3babac0f50277f8ce790ab205566740e 100644 | |
| --- a/packages/next/src/server/use-cache/use-cache-wrapper.ts | |
| +++ b/packages/next/src/server/use-cache/use-cache-wrapper.ts | |
| import { | |
| makeDynamicHangingPromise, | |
| makeRuntimeHangingPromise, | |
| makeStageHangingPromise, | |
| + makeUntrackedHangingPromise, | |
| RENDER_STAGES_BY_DATA_KIND, | |
| } from '../dynamic-rendering-utils' | |
| async function generateCacheEntryImpl( | |
| switch (outerWorkUnitStore.type) { | |
| case 'prerender-runtime': | |
| - case 'prerender': | |
| + case 'prerender': { | |
| const timeoutAbortController = new AbortController() | |
| const timer = setTimeout( | |
| () => { | |
| async function generateCacheEntryImpl( | |
| const abortSignal = dynamicAccessAbortSignal | |
| ? AbortSignal.any([ | |
| dynamicAccessAbortSignal, | |
| - outerWorkUnitStore.renderSignal, | |
| timeoutAbortController.signal, | |
| ]) | |
| : timeoutAbortController.signal | |
| async function generateCacheEntryImpl( | |
| stream = prelude | |
| } | |
| break | |
| + } | |
| case 'request': | |
| // TODO: We should just check if the render is abandonable. This is | |
| // relevant in restart-on-cache-miss in general, so when we implement that | |
| export async function cache( | |
| ) | |
| } | |
| + const workUnitStore = workUnitAsyncStorage.getStore() | |
| + if (workUnitStore === undefined) { | |
| + throw new InvariantError( | |
| + '"use cache" cannot be used outside of App Router. Expected a WorkUnitStore.' | |
| + ) | |
| + } | |
| + | |
| + // In a prerender, tasky IO may result in cache reads that start | |
| + // after the prerender has already been aborted: | |
| + // | |
| + // await setTimeout(100) // we can't abort this uncached IO... | |
| + // await cachedData() // ...so we'll still get here even though the prerender aborted | |
| + // | |
| + // The prerender is over, so we should just return an erroring promise. | |
| + // (NOTE: we also shouldn't fill this cache, because it's behind uncached IO, | |
| + // so semantically it is not part of the prerender) | |
| + switch (workUnitStore.type) { | |
| + case 'prerender': | |
| + case 'prerender-runtime': { | |
| + if (workUnitStore.renderSignal.aborted) { | |
| + // We don't know if the cache itself is dynamic or runtime data, | |
| + // but the prerender is over, so it doesn't need to participate | |
| + // in runtime data tracking at all. | |
| + return makeUntrackedHangingPromise<never>( | |
| + workUnitStore.renderSignal, | |
| + workStore.route, | |
| + '"use cache" called after prerender ended' | |
| + ) | |
| + } | |
| + break | |
| + } | |
| + case 'prerender-ppr': | |
| + case 'prerender-legacy': | |
| + case 'prerender-client': | |
| + case 'validation-client': | |
| + case 'request': | |
| + case 'cache': | |
| + case 'private-cache': | |
| + case 'unstable-cache': | |
| + case 'generate-static-params': | |
| + break | |
| + default: | |
| + workUnitStore satisfies never | |
| + } | |
| + | |
| // Probe re-executions (the dev-server's hang-detection worker) short-circuit | |
| // further down before any handler is consulted, so we skip handler selection | |
| // entirely and the worker can boot without registering handlers at all. | |
| export async function cache( | |
| return error | |
| } | |
| - const workUnitStore = workUnitAsyncStorage.getStore() | |
| - if (workUnitStore === undefined) { | |
| - throw new InvariantError( | |
| - '"use cache" cannot be used outside of App Router. Expected a WorkUnitStore.' | |
| - ) | |
| - } | |
| - | |
| const outerOwnerStack = | |
| process.env.NODE_ENV !== 'production' | |
| ? captureOuterOwnerStack(workUnitStore) | |