Unverified Commit 5136f8ec authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

[dynamicIO] Avoid timeout errors with dynamic params in `"use cache"` (#78882)

parent 6440778d
---
"next": patch
---
[dynamicIO] Avoid timeout errors with dynamic params in `"use cache"`
......@@ -59,6 +59,7 @@
// singleton modules should always use "*.external" instead of "*-instance"
"packages/next/src/server/app-render/action-async-storage-instance.ts",
"packages/next/src/server/app-render/after-task-async-storage-instance.ts",
"packages/next/src/server/app-render/dynamic-access-async-storage-instance.ts",
"packages/next/src/server/app-render/work-async-storage-instance.ts",
"packages/next/src/server/app-render/work-unit-async-storage-instance.ts",
"packages/next/src/client/components/segment-cache-impl/*"
......
......@@ -682,5 +682,7 @@
"681": "Dynamic imports should not be instrumented in the edge runtime, because `dynamicIO` doesn't support it",
"682": "\\`experimental.ppr\\` can not be \\`%s\\` when \\`experimental.dynamicIO\\` is \\`true\\`. PPR is implicitly enabled when Dynamic IO is enabled.",
"683": "The \\`compiler.define\\` option is configured to replace the \\`%s\\` variable. This variable is either part of a Next.js built-in or is already configured.",
"684": "The \\`compiler.defineServer\\` option is configured to replace the \\`%s\\` variable. This variable is either part of a Next.js built-in or is already configured."
"684": "The \\`compiler.defineServer\\` option is configured to replace the \\`%s\\` variable. This variable is either part of a Next.js built-in or is already configured.",
"685": "Accessed fallback \\`params\\` during prerendering.",
"686": "Expected clientReferenceManifest to be defined."
}
......@@ -2683,8 +2683,7 @@ export default async function build(
_isDynamicError: isDynamicError,
_isAppDir: true,
_isRoutePPREnabled: isRoutePPREnabled,
_doNotThrowOnEmptyStaticShell:
!route.throwOnEmptyStaticShell,
_allowEmptyStaticShell: !route.throwOnEmptyStaticShell,
}
})
})
......
......@@ -133,7 +133,7 @@ const browserNonTranspileModules = [
const precompileRegex = /[\\/]next[\\/]dist[\\/]compiled[\\/]/
const asyncStoragesRegex =
/next[\\/]dist[\\/](esm[\\/])?server[\\/]app-render[\\/](work-async-storage|action-async-storage|work-unit-async-storage)/
/next[\\/]dist[\\/](esm[\\/])?server[\\/]app-render[\\/](work-async-storage|action-async-storage|dynamic-access-async-storage|work-unit-async-storage)/
// Support for NODE_PATH
const nodePathList = (process.env.NODE_PATH || '')
......
......@@ -104,9 +104,9 @@ async function exportPageImpl(
// the renderOpts.
_isRoutePPREnabled: isRoutePPREnabled,
// Configure the rendering of the page not to throw if an empty static shell
// is generated while rendering using PPR.
_doNotThrowOnEmptyStaticShell: doNotThrowOnEmptyStaticShell = false,
// Configure the rendering of the page to allow that an empty static shell
// is generated while rendering using PPR and Dynamic IO.
_allowEmptyStaticShell: allowEmptyStaticShell = false,
// Pull the original query out.
query: originalQuery = {},
......@@ -266,7 +266,7 @@ async function exportPageImpl(
// If it's static, then it won't affect anything.
// If it's dynamic, then it can be handled when request hits the route.
serveStreamingMetadata: true,
doNotThrowOnEmptyStaticShell,
allowEmptyStaticShell,
experimental: {
...input.renderOpts.experimental,
isRoutePPREnabled,
......
......@@ -21,7 +21,10 @@ import type { LoadingModuleData } from '../../shared/lib/app-router-context.shar
import type { Params } from '../request/params'
import { workUnitAsyncStorage } from './work-unit-async-storage.external'
import { OUTLET_BOUNDARY_NAME } from '../../lib/metadata/metadata-constants'
import type { UseCachePageComponentProps } from '../use-cache/use-cache-wrapper'
import type {
UseCacheLayoutComponentProps,
UseCachePageComponentProps,
} from '../use-cache/use-cache-wrapper'
/**
* Use the provided loader tree to create the React Component tree.
......@@ -677,16 +680,21 @@ async function createComponentTreeInternal({
workStore
)
// TODO(useCache): Should we use this trick also if dynamicIO is enabled,
// instead of relying on the searchParams being a hanging promise?
if (!experimental.dynamicIO && isUseCacheFunction(PageComponent)) {
// If we are passing searchParams to a server component Page we need to
// track their usage in case the current render mode tracks dynamic API
// usage.
let searchParams = createServerSearchParamsForServerPage(query, workStore)
if (isUseCacheFunction(PageComponent)) {
const UseCachePageComponent: React.ComponentType<UseCachePageComponentProps> =
PageComponent
// The "use cache" wrapper takes care of converting this into an
// erroring search params promise when passing it to the original
// function.
const searchParams = Promise.resolve({})
if (!experimental.dynamicIO) {
// The "use cache" wrapper takes care of converting this into an
// erroring search params promise when passing it to the original
// function.
searchParams = Promise.resolve({})
}
pageElement = (
<UseCachePageComponent
......@@ -696,14 +704,6 @@ async function createComponentTreeInternal({
/>
)
} else {
// If we are passing searchParams to a server component Page we need to
// track their usage in case the current render mode tracks dynamic API
// usage.
const searchParams = createServerSearchParamsForServerPage(
query,
workStore
)
pageElement = (
<PageComponent params={params} searchParams={searchParams} />
)
......@@ -845,9 +845,24 @@ async function createComponentTreeInternal({
workStore
)
let serverSegment = (
<SegmentComponent {...parallelRouteProps} params={params} />
)
let serverSegment: React.ReactNode
if (isUseCacheFunction(SegmentComponent)) {
const UseCacheLayoutComponent: React.ComponentType<UseCacheLayoutComponentProps> =
SegmentComponent
serverSegment = (
<UseCacheLayoutComponent
{...parallelRouteProps}
params={params}
$$isLayoutComponent
/>
)
} else {
serverSegment = (
<SegmentComponent {...parallelRouteProps} params={params} />
)
}
if (isRootLayoutWithChildrenSlotAndAtLeastOneMoreSlot) {
// TODO-APP: This is a hack to support unmatched parallel routes, which will throw `notFound()`.
......
import { createAsyncLocalStorage } from './async-local-storage'
import type { DynamicAccessStorage } from './dynamic-access-async-storage.external'
export const dynamicAccessAsyncStorageInstance: DynamicAccessStorage =
createAsyncLocalStorage()
import type { AsyncLocalStorage } from 'async_hooks'
// Share the instance module in the next-shared layer
import { dynamicAccessAsyncStorageInstance } from './dynamic-access-async-storage-instance' with { 'turbopack-transition': 'next-shared' }
export interface DynamicAccessAsyncStore {
readonly abortController: AbortController
}
export type DynamicAccessStorage = AsyncLocalStorage<DynamicAccessAsyncStore>
export { dynamicAccessAsyncStorageInstance as dynamicAccessAsyncStorage }
......@@ -269,11 +269,12 @@ export interface RenderOptsPartial {
isStaticGeneration?: boolean
/**
* When true, the page will be rendered using the static rendering to detect
* any dynamic API's that would have stopped the page from being fully
* statically generated.
* When true, the page is prerendered as a fallback shell, while allowing any
* dynamic accesses to result in an empty shell. This is the case when there
* are also routes prerendered with a more complete set of params.
* Prerendering those routes would catch any invalid dynamic accesses.
*/
doNotThrowOnEmptyStaticShell?: boolean
allowEmptyStaticShell?: boolean
/**
* next config experimental.devtoolSegmentExplorer
......
......@@ -163,6 +163,11 @@ export interface CommonCacheStore
* from which implicit tags could be inherited.
*/
readonly implicitTags: ImplicitTags | undefined
/**
* Draft mode is only available if the outer work unit store is a request
* store and draft mode is enabled.
*/
readonly draftMode: DraftModeProvider | undefined
}
export interface UseCacheStore extends CommonCacheStore {
......@@ -179,21 +184,20 @@ export interface UseCacheStore extends CommonCacheStore {
readonly isHmrRefresh: boolean
readonly serverComponentsHmrCache: ServerComponentsHmrCache | undefined
readonly forceRevalidate: boolean
// Draft mode is only available if the outer work unit store is a request
// store and draft mode is enabled.
readonly draftMode: DraftModeProvider | undefined
}
export interface UnstableCacheStore extends CommonCacheStore {
type: 'unstable-cache'
// Draft mode is only available if the outer work unit store is a request
// store and draft mode is enabled.
readonly draftMode: DraftModeProvider | undefined
}
/**
* The Cache store is for tracking information inside a "use cache" or unstable_cache context.
* Inside this context we should never expose any request or page specific information.
* The Cache store is for tracking information inside a "use cache" or
* unstable_cache context. A cache store shadows an outer request store (if
* present) as a work unit, so that we never accidentally expose any request or
* page specific information to cache functions, unless it's explicitly desired.
* For those exceptions, the data is copied over from the request store to the
* cache store, instead of generally making the request store available to cache
* functions.
*/
export type CacheStore = UseCacheStore | UnstableCacheStore
......
......@@ -41,7 +41,7 @@ const zExportMap: zod.ZodType<ExportPathMap> = z.record(
_isAppDir: z.boolean().optional(),
_isDynamicError: z.boolean().optional(),
_isRoutePPREnabled: z.boolean().optional(),
_doNotThrowOnEmptyStaticShell: z.boolean().optional(),
_allowEmptyStaticShell: z.boolean().optional(),
})
)
......
......@@ -750,13 +750,15 @@ export type ExportPathMap = {
_isRoutePPREnabled?: boolean
/**
* When true, it indicates that the diagnostic render for this page is
* disabled. This is only used when the app has `experimental.ppr` and
* `experimental.dynamicIO` enabled.
* When true, the page is prerendered as a fallback shell, while allowing
* any dynamic accesses to result in an empty shell. This is the case when
* the app has `experimental.ppr` and `experimental.dynamicIO` enabled, and
* there are also routes prerendered with a more complete set of params.
* Prerendering those routes would catch any invalid dynamic accesses.
*
* @internal
*/
_doNotThrowOnEmptyStaticShell?: boolean
_allowEmptyStaticShell?: boolean
}
}
......
......@@ -24,6 +24,7 @@ import {
import { makeHangingPromise } from '../dynamic-rendering-utils'
import { createDedupedByCallsiteServerErrorLoggerDev } from '../create-deduped-by-callsite-server-error-logger'
import { scheduleImmediate } from '../../lib/scheduler'
import { dynamicAccessAsyncStorage } from '../app-render/dynamic-access-async-storage.external'
export type ParamValue = string | Array<string> | undefined
export type Params = Record<string, ParamValue>
......@@ -198,6 +199,33 @@ function createRenderParams(
interface CacheLifetime {}
const CachedParams = new WeakMap<CacheLifetime, Promise<Params>>()
const fallbackParamsProxyHandler: ProxyHandler<Promise<Params>> = {
get: function get(target, prop, receiver) {
if (prop === 'then' || prop === 'catch' || prop === 'finally') {
const originalMethod = ReflectAdapter.get(target, prop, receiver)
return {
[prop]: (...args: unknown[]) => {
const store = dynamicAccessAsyncStorage.getStore()
if (store) {
store.abortController.abort(
new Error(`Accessed fallback \`params\` during prerendering.`)
)
}
return new Proxy(
originalMethod.apply(target, args),
fallbackParamsProxyHandler
)
},
}[prop]
}
return ReflectAdapter.get(target, prop, receiver)
},
}
function makeAbortingExoticParams(
underlyingParams: Params,
route: string,
......@@ -208,10 +236,11 @@ function makeAbortingExoticParams(
return cachedParams
}
const promise = makeHangingPromise<Params>(
prerenderStore.renderSignal,
'`params`'
const promise = new Proxy(
makeHangingPromise<Params>(prerenderStore.renderSignal, '`params`'),
fallbackParamsProxyHandler
)
CachedParams.set(underlyingParams, promise)
Object.keys(underlyingParams).forEach((prop) => {
......
export async function LastModified({ params }) {
const { slug } = await params
return (
<p data-testid={`page-${slug}`}>
Page /{slug} last modified: {new Date().toISOString()}
</p>
)
}
export async function CachedLastModified({ params }) {
'use cache'
return <LastModified params={params} />
}
const { PHASE_PRODUCTION_BUILD } = require('next/constants')
export function getSentinelValue() {
return process.env.NEXT_PHASE === PHASE_PRODUCTION_BUILD
? 'buildtime'
: 'runtime'
}
'use cache'
import { Suspense } from 'react'
import { getSentinelValue } from '../sentinel'
export default async function Layout({ children }) {
return (
<html>
<body>
<div data-testid={`layout-${getSentinelValue()}`}>
Layout: {new Date().toISOString()}
</div>
<Suspense fallback={<p>Loading...</p>}>{children}</Suspense>
</body>
</html>
)
}
'use cache'
import { LastModified } from '../../../last-modified'
export default async function Page({ params }) {
return <LastModified params={params} />
}
export async function generateStaticParams() {
return [{ slug: 'foo' }]
}
import { CachedLastModified } from '../../../last-modified'
export default async function Page({ params }) {
return <CachedLastModified params={params} />
}
export async function generateStaticParams() {
return [{ slug: 'foo' }]
}
import { CachedLastModified } from '../../../last-modified'
export default async function Page({ params }) {
return (
<>
<CachedLastModified params={params.then((p) => ({ slug: p.slug }))} />
<CachedLastModified params={params.catch(() => {})} />
<CachedLastModified params={params.finally(() => {})} />
<CachedLastModified params={params.finally().catch()} />
</>
)
}
export async function generateStaticParams() {
return [{ slug: 'foo' }]
}
'use cache'
import { getSentinelValue } from '../sentinel'
export default async function Layout({ children }) {
return (
<html>
<body>
<div data-testid={`layout-${getSentinelValue()}`}>
Layout: {new Date().toISOString()}
</div>
{children}
</body>
</html>
)
}
'use cache'
import { LastModified } from '../../../last-modified'
export default async function Page({ params }) {
return <LastModified params={params} />
}
export async function generateStaticParams() {
return [{ slug: 'foo' }]
}
import { CachedLastModified } from '../../../last-modified'
export default async function Page({ params }) {
return <CachedLastModified params={params} />
}
export async function generateStaticParams() {
return [{ slug: 'foo' }]
}
import { CachedLastModified } from '../../../last-modified'
export default async function Page({ params }) {
return (
<>
<CachedLastModified params={params.then((p) => ({ slug: p.slug }))} />
<CachedLastModified params={params.catch(() => {})} />
<CachedLastModified params={params.finally(() => {})} />
<CachedLastModified params={params.finally().catch()} />
</>
)
}
export async function generateStaticParams() {
return [{ slug: 'foo' }]
}
import { nextTestSetup } from 'e2e-utils'
describe('empty-fallback-shells', () => {
const { next, isNextDeploy } = nextTestSetup({
const { next, isNextDev, isNextDeploy, isNextStart } = nextTestSetup({
files: __dirname,
})
it('should start and not postpone the response', async () => {
const res = await next.fetch('/world')
const html = await res.text()
expect(html).toContain('hello-world')
describe('without IO', () => {
it('should start and not postpone the response', async () => {
const res = await next.fetch('/without-io/world')
const html = await res.text()
expect(html).toContain('hello-world')
if (isNextDeploy) {
expect(res.headers.get('x-matched-path')).toBe('/[slug]')
}
if (isNextDeploy) {
expect(res.headers.get('x-matched-path')).toBe('/without-io/[slug]')
}
// If we didn't use the fallback shell, then we didn't postpone the response
// and therefore shouldn't have sent the postponed header.
expect(res.headers.get('x-nextjs-postponed')).not.toBe('1')
// If we didn't use the fallback shell, then we didn't postpone the
// response and therefore shouldn't have sent the postponed header.
expect(res.headers.get('x-nextjs-postponed')).not.toBe('1')
})
})
describe('with cached IO', () => {
describe('and the page wrapped in Suspense', () => {
describe('and the params accessed in the cached page', () => {
it('resumes a postponed fallback shell', async () => {
const res = await next.fetch(
'/with-cached-io/with-suspense/params-in-page/bar'
)
const html = await res.text()
expect(html).toContain('page-bar')
if (isNextDev) {
expect(html).toContain('layout-runtime')
} else {
expect(html).toContain('layout-buildtime')
}
if (isNextDeploy) {
expect(res.headers.get('x-matched-path')).toBe(
'/with-cached-io/with-suspense/params-in-page/[slug]'
)
} else if (isNextStart) {
expect(res.headers.get('x-nextjs-postponed')).toBe('1')
}
})
})
describe('and the params accessed in cached non-page function', () => {
it('resumes a postponed fallback shell', async () => {
const res = await next.fetch(
'/with-cached-io/with-suspense/params-not-in-page/bar'
)
const html = await res.text()
expect(html).toContain('page-bar')
if (isNextDev) {
expect(html).toContain('layout-runtime')
} else {
expect(html).toContain('layout-buildtime')
}
if (isNextDeploy) {
expect(res.headers.get('x-matched-path')).toBe(
'/with-cached-io/with-suspense/params-not-in-page/[slug]'
)
} else if (isNextStart) {
expect(res.headers.get('x-nextjs-postponed')).toBe('1')
}
})
})
describe('and params.then/catch/finally passed to a cached function', () => {
it('resumes a postponed fallback shell', async () => {
const res = await next.fetch(
'/with-cached-io/with-suspense/params-then-in-page/bar'
)
const html = await res.text()
expect(html).toIncludeRepeated('data-testid="page-bar"', 4)
if (isNextDev) {
expect(html).toContain('layout-runtime')
} else {
expect(html).toContain('layout-buildtime')
}
if (isNextDeploy) {
expect(res.headers.get('x-matched-path')).toBe(
'/with-cached-io/with-suspense/params-then-in-page/[slug]'
)
} else if (isNextStart) {
expect(res.headers.get('x-nextjs-postponed')).toBe('1')
}
})
})
})
describe('and the page not wrapped in Suspense', () => {
describe('and the params accessed in the cached page', () => {
it('does not resume a postponed fallback shell', async () => {
const res = await next.fetch(
'/with-cached-io/without-suspense/params-in-page/bar'
)
const html = await res.text()
expect(html).toContain('page-bar')
expect(html).toContain('layout-runtime')
if (isNextDeploy) {
expect(res.headers.get('x-matched-path')).toBe(
'/with-cached-io/without-suspense/params-in-page/[slug]'
)
} else {
expect(res.headers.get('x-nextjs-postponed')).not.toBe('1')
}
})
})
describe('and the params accessed in a cached non-page function', () => {
it('does not resume a postponed fallback shell', async () => {
const res = await next.fetch(
'/with-cached-io/without-suspense/params-not-in-page/bar'
)
const html = await res.text()
expect(html).toContain('page-bar')
expect(html).toContain('layout-runtime')
if (isNextDeploy) {
expect(res.headers.get('x-matched-path')).toBe(
'/with-cached-io/without-suspense/params-not-in-page/[slug]'
)
} else {
expect(res.headers.get('x-nextjs-postponed')).not.toBe('1')
}
})
})
describe('and params.then/catch/finally passed to a cached function', () => {
it('does not resume a postponed fallback shell', async () => {
const res = await next.fetch(
'/with-cached-io/without-suspense/params-then-in-page/bar'
)
const html = await res.text()
expect(html).toIncludeRepeated('data-testid="page-bar"', 4)
expect(html).toContain('layout-runtime')
if (isNextDeploy) {
expect(res.headers.get('x-matched-path')).toBe(
'/with-cached-io/without-suspense/params-then-in-page/[slug]'
)
} else {
expect(res.headers.get('x-nextjs-postponed')).not.toBe('1')
}
})
})
})
})
if (isNextStart) {
it('should not log a HANGING_PROMISE_REJECTION error', async () => {
expect(next.cliOutput).not.toContain('HANGING_PROMISE_REJECTION')
})
}
})
......@@ -4,6 +4,7 @@
const nextConfig = {
experimental: {
dynamicIO: true,
prerenderEarlyExit: false,
},
}
......
'use cache'
export default async function Page({
params,
}: {
params: Promise<{ slug: string }>
}) {
const { slug } = await params
return <p>slug: {slug}</p>
}
// If generateStaticParams would be used here to define at least one set of
// complete params, we would not yield a timeout error.
async function LastModified({ params }) {
'use cache'
const { slug } = await params
return (
<p data-testid={`page-${slug}`}>
Page /{slug} last modified: {new Date().toISOString()}
</p>
)
}
async function transformParams(params) {
const { slug } = await params
return { slug }
}
export default async function Page({ params }) {
return <LastModified params={transformParams(params)} />
}
export async function generateStaticParams() {
return [{ slug: 'foo' }]
}
......@@ -335,15 +335,15 @@ describe('use-cache-hanging-inputs', () => {
)
expect(cliOutput).toInclude(
createExpectedBuildErrorMessage('/fallback-params/[slug]')
createExpectedBuildErrorMessage('/search-params')
)
expect(cliOutput).toInclude(
createExpectedBuildErrorMessage('/search-params')
createExpectedBuildErrorMessage('/search-params-caught')
)
expect(cliOutput).toInclude(
createExpectedBuildErrorMessage('/search-params-caught')
createExpectedBuildErrorMessage('/transformed-params/[slug]')
)
expect(cliOutput).toInclude(
......
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