Unverified Commit eab1fe83 authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

Enable PPR for `dynamic = "force-dynamic"` (#58779)

This makes some critical modifications to the app render pipeline when
PPR has been enabled for pages with segments defining:

```js
export const dynamic = "force-dynamic"
```

Importantly, it no longer modifies the revalidation time to zero for
those pages, and now falls back to the provided default revalidation
time. When static render occurs, if the page being rendered has a
segment config defining `dynamic === "force-dynamic"`, then it will
postpone at the root of the component tree. This ensures that no render
code is executed for the page, as the entirety of the tree will have
postponed. This fixes the bug where the flight prefetch wasn't generated
correctly as well.
parent 6387c9cb
......@@ -163,10 +163,10 @@ jobs:
strategy:
fail-fast: false
matrix:
group: [1, 2, 3, 4, 5]
group: [1/5, 2/5, 3/5, 4/5, 5/5]
uses: ./.github/workflows/build_reusable.yml
with:
afterBuild: RUST_BACKTRACE=0 NEXT_EXTERNAL_TESTS_FILTERS="$(pwd)/test/turbopack-tests-manifest.json" TURBOPACK=1 NEXT_E2E_TEST_TIMEOUT=240000 NEXT_TEST_MODE=dev node run-tests.js --test-pattern '^(test\/(development|e2e))/.*\.test\.(js|jsx|ts|tsx)$' --timings -g ${{ matrix.group }}/5 -c ${TEST_CONCURRENCY}
afterBuild: RUST_BACKTRACE=0 NEXT_EXTERNAL_TESTS_FILTERS="$(pwd)/test/turbopack-tests-manifest.json" TURBOPACK=1 NEXT_E2E_TEST_TIMEOUT=240000 NEXT_TEST_MODE=dev node run-tests.js --test-pattern '^(test\/(development|e2e))/.*\.test\.(js|jsx|ts|tsx)$' --timings -g ${{ matrix.group }} -c ${TEST_CONCURRENCY}
secrets: inherit
test-turbopack-integration:
......@@ -177,11 +177,11 @@ jobs:
strategy:
fail-fast: false
matrix:
group: [1, 2, 3, 4, 5]
group: [1/5, 2/5, 3/5, 4/5, 5/5]
uses: ./.github/workflows/build_reusable.yml
with:
nodeVersion: 18.17.0
afterBuild: RUST_BACKTRACE=0 NEXT_EXTERNAL_TESTS_FILTERS="$(pwd)/test/turbopack-tests-manifest.json" TURBOPACK=1 node run-tests.js --timings -g ${{ matrix.group }}/5 -c ${TEST_CONCURRENCY} --type integration
afterBuild: RUST_BACKTRACE=0 NEXT_EXTERNAL_TESTS_FILTERS="$(pwd)/test/turbopack-tests-manifest.json" TURBOPACK=1 node run-tests.js --timings -g ${{ matrix.group }} -c ${TEST_CONCURRENCY} --type integration
secrets: inherit
test-next-swc-wasm:
......@@ -219,10 +219,10 @@ jobs:
strategy:
fail-fast: false
matrix:
group: [1, 2, 3]
group: [1/4, 2/4, 3/4, 4/4]
uses: ./.github/workflows/build_reusable.yml
with:
afterBuild: NEXT_TEST_MODE=dev node run-tests.js --timings -g ${{ matrix.group }}/3 -c ${TEST_CONCURRENCY} --type development
afterBuild: NEXT_TEST_MODE=dev node run-tests.js --timings -g ${{ matrix.group }} -c ${TEST_CONCURRENCY} --type development
secrets: inherit
test-prod:
......@@ -233,10 +233,10 @@ jobs:
strategy:
fail-fast: false
matrix:
group: [1, 2, 3, 4, 5]
group: [1/5, 2/5, 3/5, 4/5, 5/5]
uses: ./.github/workflows/build_reusable.yml
with:
afterBuild: NEXT_TEST_MODE=start node run-tests.js --timings -g ${{ matrix.group }}/5 -c ${TEST_CONCURRENCY} --type production
afterBuild: NEXT_TEST_MODE=start node run-tests.js --timings -g ${{ matrix.group }} -c ${TEST_CONCURRENCY} --type production
secrets: inherit
test-integration:
......@@ -247,11 +247,23 @@ jobs:
strategy:
fail-fast: false
matrix:
group: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
group:
- 1/12
- 2/12
- 3/12
- 4/12
- 5/12
- 6/12
- 7/12
- 8/12
- 9/12
- 10/12
- 11/12
- 12/12
uses: ./.github/workflows/build_reusable.yml
with:
nodeVersion: 18.17.0
afterBuild: node run-tests.js --timings -g ${{ matrix.group }}/12 -c ${TEST_CONCURRENCY} --type integration
afterBuild: node run-tests.js --timings -g ${{ matrix.group }}2 -c ${TEST_CONCURRENCY} --type integration
secrets: inherit
test-firefox-safari:
......@@ -273,12 +285,10 @@ jobs:
strategy:
fail-fast: false
matrix:
group: [1, 2, 3]
uses: ./.github/workflows/build_reusable.yml
with:
nodeVersion: 18.17.0
afterBuild: __NEXT_EXPERIMENTAL_PPR=true NEXT_EXTERNAL_TESTS_FILTERS="test/ppr-tests-manifest.json" node run-tests.js --timings -g ${{ matrix.group }}/3 -c ${TEST_CONCURRENCY} --type integration
afterBuild: __NEXT_EXPERIMENTAL_PPR=true NEXT_EXTERNAL_TESTS_FILTERS="test/ppr-tests-manifest.json" node run-tests.js --timings -c ${TEST_CONCURRENCY} --type integration
secrets: inherit
test-ppr-dev:
......@@ -289,10 +299,10 @@ jobs:
strategy:
fail-fast: false
matrix:
group: [1, 2, 3]
group: [1/4, 2/4, 3/4, 4/4]
uses: ./.github/workflows/build_reusable.yml
with:
afterBuild: __NEXT_EXPERIMENTAL_PPR=true NEXT_EXTERNAL_TESTS_FILTERS="test/ppr-tests-manifest.json" NEXT_TEST_MODE=dev node run-tests.js --timings -g ${{ matrix.group }}/3 -c ${TEST_CONCURRENCY} --type development
afterBuild: __NEXT_EXPERIMENTAL_PPR=true NEXT_EXTERNAL_TESTS_FILTERS="test/ppr-tests-manifest.json" NEXT_TEST_MODE=dev node run-tests.js --timings -g ${{ matrix.group }} -c ${TEST_CONCURRENCY} --type development
secrets: inherit
test-ppr-prod:
......@@ -303,10 +313,10 @@ jobs:
strategy:
fail-fast: false
matrix:
group: [1, 2, 3]
group: [1/4, 2/4, 3/4, 4/4]
uses: ./.github/workflows/build_reusable.yml
with:
afterBuild: __NEXT_EXPERIMENTAL_PPR=true NEXT_EXTERNAL_TESTS_FILTERS="test/ppr-tests-manifest.json" NEXT_TEST_MODE=start node run-tests.js --timings -g ${{ matrix.group }}/3 -c ${TEST_CONCURRENCY} --type production
afterBuild: __NEXT_EXPERIMENTAL_PPR=true NEXT_EXTERNAL_TESTS_FILTERS="test/ppr-tests-manifest.json" NEXT_TEST_MODE=start node run-tests.js --timings -g ${{ matrix.group }} -c ${TEST_CONCURRENCY} --type production
secrets: inherit
report-test-results:
......
......@@ -1572,7 +1572,10 @@ export async function isPageStatic({
)
}
if (appConfig.dynamic === 'force-dynamic') {
// If force dynamic was set and we don't have PPR enabled, then set the
// revalidate to 0.
// TODO: (PPR) remove this once PPR is enabled by default
if (appConfig.dynamic === 'force-dynamic' && !ppr) {
appConfig.revalidate = 0
}
......
import type { StaticGenerationStore } from './static-generation-async-storage.external'
export function maybePostpone(
staticGenerationStore: StaticGenerationStore,
reason: string
) {
// If we aren't performing a static generation or we aren't using PPR then
// we don't need to postpone.
if (
!staticGenerationStore.isStaticGeneration ||
!staticGenerationStore.experimental.ppr
) {
return
}
if (!staticGenerationStore.postpone) {
throw new Error(
'Invariant: PPR is enabled but the postpone API is unavailable'
)
}
// Keep track of if the postpone API has been called.
staticGenerationStore.postponeWasTriggered = true
staticGenerationStore.postpone(
`This page needs to bail out of prerendering at this point because it used ${reason}. ` +
`React throws this special object to indicate where. It should not be caught by ` +
`your own try/catch. Learn more: https://nextjs.org/docs/messages/ppr-caught-error`
)
}
......@@ -16,6 +16,13 @@ export interface StaticGenerationStore {
readonly isRevalidate?: boolean
readonly isUnstableCacheCallback?: boolean
/**
* If defined, this function when called will throw an error postponing
* rendering during the React render phase. This should not be invoked outside
* of the React render phase as it'll throw an error.
*/
readonly postpone: ((reason: string) => never) | undefined
forceDynamic?: boolean
fetchCache?:
| 'only-cache'
......@@ -30,7 +37,6 @@ export interface StaticGenerationStore {
dynamicShouldError?: boolean
pendingRevalidates?: Record<string, Promise<any>>
postponeWasTriggered?: boolean
postpone?: (reason: string) => never
dynamicUsageDescription?: string
dynamicUsageStack?: string
......@@ -46,10 +52,6 @@ export interface StaticGenerationStore {
fetchMetrics?: FetchMetrics
isDraftMode?: boolean
readonly experimental: {
readonly ppr: boolean
}
}
export type StaticGenerationAsyncStorage =
......
import type { AppConfigDynamic } from '../../build/utils'
import { DynamicServerError } from './hooks-server-context'
import { maybePostpone } from './maybe-postpone'
import { staticGenerationAsyncStorage } from './static-generation-async-storage.external'
class StaticGenBailoutError extends Error {
......@@ -47,7 +46,8 @@ export const staticGenerationBailout: StaticGenerationBailout = (
link: 'https://nextjs.org/docs/messages/dynamic-server-error',
})
maybePostpone(staticGenerationStore, reason)
// If postpone is available, we should postpone the render.
staticGenerationStore.postpone?.(reason)
// As this is a bailout, we don't want to revalidate, so set the revalidate
// to 0.
......
......@@ -1119,7 +1119,7 @@ async function renderToHTMLOrFlightImpl(
if (
// if PPR is enabled
renderOpts.experimental.ppr &&
// and a call to `maybePostpone` happened
// and a call to `postpone` happened
staticGenerationStore.postponeWasTriggered &&
// but there's no postpone state
!metadata.postponed
......
import type { FlightSegmentPath, CacheNodeSeedData } from './types'
import React from 'react'
import React, { type ReactNode } from 'react'
import { isClientReference } from '../../lib/client-reference'
import { getLayoutOrPageModule } from '../lib/app-dir-module'
import type { LoaderTree } from '../lib/app-dir-module'
......@@ -10,10 +10,26 @@ import { createComponentStylesAndScripts } from './create-component-styles-and-s
import { getLayerAssets } from './get-layer-assets'
import { hasLoadingComponentInTree } from './has-loading-component-in-tree'
type ComponentTree = {
seedData: CacheNodeSeedData
styles: ReactNode
}
/**
* Use the provided loader tree to create the React Component tree.
* This component will call `React.postpone` that throws the postponed error.
*/
export const Postpone = ({
postpone,
}: {
postpone: (reason: string) => never
}): never => {
// Call the postpone API now with the reason set to "force-dynamic".
return postpone('dynamic = "force-dynamic" was used')
}
/**
* Use the provided loader tree to create the React Component tree.
*/
export async function createComponentTree({
createSegmentPath,
loaderTree: tree,
......@@ -38,10 +54,7 @@ export async function createComponentTree({
asNotFound?: boolean
metadataOutlet?: React.ReactNode
ctx: AppRenderContext
}): Promise<{
seedData: CacheNodeSeedData
styles: React.ReactNode
}> {
}): Promise<ComponentTree> {
const {
renderOpts: { nextConfigOutput },
staticGenerationStore,
......@@ -155,7 +168,13 @@ export async function createComponentTree({
staticGenerationStore.dynamicShouldError = true
} else if (dynamic === 'force-dynamic') {
staticGenerationStore.forceDynamic = true
staticGenerationBailout(`force-dynamic`, { dynamic })
// TODO: (PPR) remove this bailout once PPR is the default
if (!staticGenerationStore.postpone) {
// If the postpone API isn't available, we can't postpone the render and
// therefore we can't use the dynamic API.
staticGenerationBailout(`force-dynamic`, { dynamic })
}
} else {
staticGenerationStore.dynamicShouldError = false
if (dynamic === 'force-static') {
......@@ -183,7 +202,10 @@ export async function createComponentTree({
if (
staticGenerationStore.isStaticGeneration &&
ctx.defaultRevalidate === 0
ctx.defaultRevalidate === 0 &&
// If the postpone API isn't available, we can't postpone the render and
// therefore we can't use the dynamic API.
!staticGenerationStore.postpone
) {
const dynamicUsageDescription = `revalidate: 0 configured ${segment}`
staticGenerationStore.dynamicUsageDescription = dynamicUsageDescription
......@@ -192,10 +214,8 @@ export async function createComponentTree({
}
}
if (
staticGenerationStore?.dynamicUsageErr &&
!staticGenerationStore.experimental.ppr
) {
// If there's a dynamic usage error attached to the store, throw it.
if (staticGenerationStore.dynamicUsageErr) {
throw staticGenerationStore.dynamicUsageErr
}
......@@ -391,6 +411,21 @@ export async function createComponentTree({
}
}
// If force-dynamic is used and the current render supports postponing, we
// replace it with a node that will postpone the render. This ensures that the
// postpone is invoked during the react render phase and not during the next
// render phase.
if (staticGenerationStore.forceDynamic && staticGenerationStore.postpone) {
return {
seedData: [
actualSegment,
parallelRouteCacheNodeSeedData,
<Postpone postpone={staticGenerationStore.postpone} />,
],
styles: layerAssets,
}
}
const isClientComponent = isClientReference(layoutOrPageMod)
// If it's a not found route, and we don't have any matched parallel
......@@ -453,13 +488,13 @@ export async function createComponentTree({
<Component {...props} />
)}
{/* This null is currently critical. The wrapped Component can render null and if there was not fragment
surrounding it this would look like a pending tree data state on the client which will cause an errror
and break the app. Long-term we need to move away from using null as a partial tree identifier since it
is a valid return type for the components we wrap. Once we make this change we can safely remove the
fragment. The reason the extra null here is required is that fragments which only have 1 child are elided.
If the Component above renders null the actual treedata will look like `[null, null]`. If we remove the extra
null it will look like `null` (the array is elided) and this is what confuses the client router.
TODO-APP update router to use a Symbol for partial tree detection */}
surrounding it this would look like a pending tree data state on the client which will cause an error
and break the app. Long-term we need to move away from using null as a partial tree identifier since it
is a valid return type for the components we wrap. Once we make this change we can safely remove the
fragment. The reason the extra null here is required is that fragments which only have 1 child are elided.
If the Component above renders null the actual tree data will look like `[null, null]`. If we remove the extra
null it will look like `null` (the array is elided) and this is what confuses the client router.
TODO-APP update router to use a Symbol for partial tree detection */}
{null}
</>,
],
......
......@@ -77,8 +77,22 @@ export const StaticGenerationAsyncStorageWrapper: AsyncStorageWrapper<
isOnDemandRevalidate: renderOpts.isOnDemandRevalidate,
isDraftMode: renderOpts.isDraftMode,
experimental: renderOpts.experimental,
postpone,
postpone:
// If we aren't performing a static generation or we aren't using PPR then
// we don't need to postpone.
isStaticGeneration && renderOpts.experimental.ppr && postpone
? (reason: string) => {
// Keep track of if the postpone API has been called.
store.postponeWasTriggered = true
return postpone(
`This page needs to bail out of prerendering at this point because it used ${reason}. ` +
`React throws this special object to indicate where. It should not be caught by ` +
`your own try/catch. Learn more: https://nextjs.org/docs/messages/ppr-caught-error`
)
}
: undefined,
}
// TODO: remove this when we resolve accessing the store outside the execution context
......
......@@ -1886,13 +1886,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
// If we're in minimal mode, then try to get the postponed information from
// the request metadata. If available, use it for resuming the postponed
// render.
let resumed: { postponed: string } | null = null
if (this.minimalMode) {
const postponed = getRequestMeta(req, 'postponed')
if (postponed) {
resumed = { postponed }
}
}
const minimalPostponed = getRequestMeta(req, 'postponed')
// If PPR is enabled, and this is a RSC request (but not a prefetch), then
// we can use this fact to only generate the flight data for the request
......@@ -1921,7 +1915,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
// Server actions can use non-GET/HEAD methods.
!isServerAction &&
// Resume can use non-GET/HEAD methods.
!resumed &&
!minimalPostponed &&
!is404Page &&
!is500Page &&
pathname !== '/_error' &&
......@@ -2079,7 +2073,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
isSSG &&
!opts.supportsDynamicHTML &&
!isServerAction &&
!resumed &&
!minimalPostponed &&
!isDynamicRSCRequest
) {
ssgCacheKey = `${locale ? `/${locale}` : ''}${
......@@ -2142,11 +2136,15 @@ export default abstract class Server<ServerOptions extends Options = Options> {
const { routeModule } = components
type Renderer = (
type Renderer = (context: {
/**
* The postponed data for this render. This is only provided when resuming
* a render that has been postponed.
*/
postponed: string | undefined
) => Promise<ResponseCacheEntry | null>
}) => Promise<ResponseCacheEntry | null>
const doRender: Renderer = async (postponed) => {
const doRender: Renderer = async ({ postponed }) => {
// In development, we always want to generate dynamic HTML.
const supportsDynamicHTML: boolean =
// If this isn't a data request and we're not in development, then we
......@@ -2157,7 +2155,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
(!isSSG && !hasStaticPaths) ||
// If this request has provided postponed data, it supports dynamic
// HTML.
!!postponed ||
typeof postponed === 'string' ||
// If this is a dynamic RSC request, then this render supports dynamic
// HTML (it's dynamic).
isDynamicRSCRequest
......@@ -2186,7 +2184,11 @@ export default abstract class Server<ServerOptions extends Options = Options> {
...(isAppPath
? {
incrementalCache,
isRevalidate: isSSG,
// This is a revalidation request if the request is for a static
// page and it is not being resumed from a postponed render and
// it is not a dynamic RSC request then it is a revalidation
// request.
isRevalidate: isSSG && !postponed && !isDynamicRSCRequest,
originalPathname: components.ComponentMod.originalPathname,
serverActions: this.nextConfig.experimental.serverActions,
}
......@@ -2386,7 +2388,8 @@ export default abstract class Server<ServerOptions extends Options = Options> {
isAppPath &&
isSSG &&
metadata.revalidate === 0 &&
!this.renderOpts.dev
!this.renderOpts.dev &&
!renderOpts.experimental.ppr
) {
const staticBailoutInfo = metadata.staticBailoutInfo
......@@ -2489,12 +2492,6 @@ export default abstract class Server<ServerOptions extends Options = Options> {
isOnDemandRevalidate = true
}
// Only requests that aren't revalidating can be resumed.
const postponed =
!isOnDemandRevalidate && !isRevalidating && resumed
? resumed.postponed
: undefined
// only allow on-demand revalidate for fallback: true/blocking
// or for prerendered fallback: false paths
if (
......@@ -2577,7 +2574,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
// We pass `undefined` as there cannot be a postponed state in
// development.
const result = await doRender(undefined)
const result = await doRender({ postponed: undefined })
if (!result) {
return null
}
......@@ -2588,7 +2585,14 @@ export default abstract class Server<ServerOptions extends Options = Options> {
}
}
const result = await doRender(postponed)
const result = await doRender({
// Only requests that aren't revalidating can be resumed. If we have the
// minimal postponed data, then we should resume the render with it.
postponed:
!isOnDemandRevalidate && !isRevalidating && minimalPostponed
? minimalPostponed
: undefined,
})
if (!result) {
return null
}
......@@ -2648,7 +2652,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
// If this is a resume request in minimal mode it is streamed with dynamic
// content and should not be cached.
if (this.minimalMode && resumed?.postponed) {
if (minimalPostponed) {
revalidate = 0
}
......@@ -2765,7 +2769,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
} else if (isAppPath) {
// If the request has a postponed state and it's a resume request we
// should error.
if (cachedData.postponed && resumed) {
if (cachedData.postponed && minimalPostponed) {
throw new Error(
'Invariant: postponed state should not be present on a resume request'
)
......@@ -2875,7 +2879,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
// Perform the render again, but this time, provide the postponed state.
// We don't await because we want the result to start streaming now, and
// we've already chained the transformer's readable to the render result.
doRender(cachedData.postponed)
doRender({ postponed: cachedData.postponed })
.then(async (result) => {
if (!result) {
throw new Error('Invariant: expected a result to be returned')
......
......@@ -12,7 +12,6 @@ import {
NEXT_CACHE_TAG_MAX_LENGTH,
} from '../../lib/constants'
import * as Log from '../../build/output/log'
import { maybePostpone } from '../../client/components/maybe-postpone'
const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge'
......@@ -372,15 +371,19 @@ export function patchFetch({
// we don't consider autoNoCache to switch to dynamic during
// revalidate although if it occurs during build we do
!autoNoCache &&
// If the revalidate value isn't currently set or the value is less
// than the current revalidate value, we should update the revalidate
// value.
(typeof staticGenerationStore.revalidate === 'undefined' ||
(typeof revalidate === 'number' &&
(staticGenerationStore.revalidate === false ||
(typeof staticGenerationStore.revalidate === 'number' &&
revalidate < staticGenerationStore.revalidate))))
) {
// If enabled, we should bail out of static generation.
// If we were setting the revalidate value to 0, we should try to
// postpone instead first.
if (revalidate === 0) {
maybePostpone(staticGenerationStore, 'revalidate: 0')
staticGenerationStore.postpone?.('revalidate: 0')
}
staticGenerationStore.revalidate = revalidate
......@@ -594,17 +597,17 @@ export function patchFetch({
? ` ${staticGenerationStore.urlPathname}`
: ''
}`
const err = new DynamicServerError(dynamicUsageReason)
staticGenerationStore.dynamicUsageErr = err
staticGenerationStore.dynamicUsageStack = err.stack
staticGenerationStore.dynamicUsageDescription = dynamicUsageReason
// If enabled, we should bail out of static generation.
maybePostpone(staticGenerationStore, dynamicUsageReason)
staticGenerationStore.postpone?.(dynamicUsageReason)
// PPR is not enabled, or React postpone is not available, we
// should set the revalidate to 0.
staticGenerationStore.revalidate = 0
const err = new DynamicServerError(dynamicUsageReason)
staticGenerationStore.dynamicUsageErr = err
staticGenerationStore.dynamicUsageDescription = dynamicUsageReason
}
const hasNextConfig = 'next' in init
......@@ -623,13 +626,13 @@ export function patchFetch({
? ` ${staticGenerationStore.urlPathname}`
: ''
}`
// If enabled, we should bail out of static generation.
staticGenerationStore.postpone?.(dynamicUsageReason)
const err = new DynamicServerError(dynamicUsageReason)
staticGenerationStore.dynamicUsageErr = err
staticGenerationStore.dynamicUsageStack = err.stack
staticGenerationStore.dynamicUsageDescription = dynamicUsageReason
// If enabled, we should bail out of static generation.
maybePostpone(staticGenerationStore, dynamicUsageReason)
}
if (!forceDynamic || next.revalidate !== 0) {
......
......@@ -204,22 +204,42 @@ export default class RenderResult<
/**
* Pipes the response to a writable stream. This will close/cancel the
* writable stream if an error is encountered.
* writable stream if an error is encountered. If this doesn't throw, then
* the writable stream will be closed or aborted.
*
* @param writable Writable stream to pipe the response to
*/
public async pipeTo(writable: WritableStream<Uint8Array>): Promise<void> {
try {
await this.readable.pipeTo(writable)
await this.readable.pipeTo(writable, {
// We want to close the writable stream ourselves so that we can wait
// for the waitUntil promise to resolve before closing it. If an error
// is encountered, we'll abort the writable stream if we swallowed the
// error.
preventClose: true,
})
// If there is a waitUntil promise, wait for it to resolve before
// closing the writable stream.
if (this.waitUntil) await this.waitUntil
// Close the writable stream.
await writable.close()
} catch (err) {
// If this isn't a client abort, then re-throw the error.
if (!isAbortError(err)) {
throw err
}
} finally {
if (this.waitUntil) {
await this.waitUntil
// If this is an abort error, we should abort the writable stream (as we
// took ownership of it when we started piping). We don't need to re-throw
// because we handled the error.
if (isAbortError(err)) {
// Abort the writable stream if an error is encountered.
await writable.abort(err)
return
}
// We're not aborting the writer here as when this method throws it's not
// clear as to how so the caller should assume it's their responsibility
// to clean up the writer.
throw err
}
}
......
import { maybePostpone } from '../../../client/components/maybe-postpone'
import type {
StaticGenerationStore,
StaticGenerationAsyncStorage,
} from '../../../client/components/static-generation-async-storage.external'
import { staticGenerationAsyncStorage as _staticGenerationAsyncStorage } from '../../../client/components/static-generation-async-storage.external'
import { CACHE_ONE_YEAR } from '../../../lib/constants'
import { addImplicitTags, validateTags } from '../../lib/patch-fetch'
......@@ -33,7 +33,9 @@ export function unstable_cache<T extends Callback>(
if (store && typeof options.revalidate === 'number') {
// Revalidate 0 is a special case, it means opt-out of static generation.
if (options.revalidate === 0) {
maybePostpone(store, 'revalidate: 0')
// If postpone is supported we should postpone the render.
store.postpone?.('revalidate: 0')
// Set during dynamic rendering
store.revalidate = 0
// If revalidate was already set in the store before we should check if the new value is lower, set it to the lowest of the two.
......@@ -75,9 +77,7 @@ export function unstable_cache<T extends Callback>(
urlPathname: store?.urlPathname || '/',
isUnstableCacheCallback: true,
isStaticGeneration: store?.isStaticGeneration === true,
experimental: {
ppr: store?.experimental?.ppr === true,
},
postpone: store?.postpone,
},
async () => {
const tags = validateTags(
......
......@@ -1705,35 +1705,38 @@ createNextDescribe(
{ path: '/stale-cache-serving-edge/app-page' },
{ path: '/stale-cache-serving-edge/route-handler' },
])('should stream properly for $path', async ({ path }) => {
// prime cache initially
await next.fetch(path)
// Prime the cache.
let res = await next.fetch(path)
expect(res.status).toBe(200)
// Consume the cache, the revalidations are completed on the end of the
// stream so we need to wait for that to complete.
await res.text()
for (let i = 0; i < 6; i++) {
await waitFor(1000)
const start = Date.now()
let streamStart = 0
const res = await next.fetch(path)
const chunks: any[] = []
await new Promise<void>((bodyResolve) => {
res.body.on('data', (chunk) => {
if (!streamStart) {
streamStart = Date.now()
const timings = {
start: Date.now(),
startedStreaming: 0,
}
res = await next.fetch(path)
// eslint-disable-next-line no-loop-func
await new Promise<void>((resolve) => {
res.body.on('data', () => {
if (!timings.startedStreaming) {
timings.startedStreaming = Date.now()
}
chunks.push(chunk)
})
res.body.on('end', () => {
bodyResolve()
resolve()
})
})
require('console').log({
start,
duration: Date.now() - start,
streamStart,
startDuration: streamStart - start,
})
expect(streamStart - start).toBeLessThan(3000)
expect(timings.startedStreaming - timings.start).toBeLessThan(3000)
}
})
......
import { fetchRetry } from '../../../lib/fetch-retry'
export const revalidate = 3
export async function generateStaticParams() {
return [{ slug: 'one' }]
}
const fetchRetry = async (url, init) => {
for (let i = 0; i < 5; i++) {
try {
return await fetch(url, init)
} catch (err) {
if (i === 4) {
throw err
}
console.log(`Failed to fetch`, err, `retrying...`)
}
}
}
export default async function page({ params }) {
const { slug } = params
let data
......
import { fetchRetry } from '../../../lib/fetch-retry'
export async function generateStaticParams() {
return [{ slug: 'one' }]
}
const fetchRetry = async (url, init) => {
for (let i = 0; i < 5; i++) {
try {
return await fetch(url, init)
} catch (err) {
if (i === 4) {
throw err
}
console.log(`Failed to fetch`, err, `retrying...`)
}
}
}
export default async function page({ params }) {
const { slug } = params
const data = await fetchRetry(
......
export const runtime = 'experimental-edge'
import { fetchRetry } from '../../../lib/fetch-retry'
const fetchRetry = async (url, init) => {
for (let i = 0; i < 5; i++) {
try {
return await fetch(url, init)
} catch (err) {
if (i === 4) {
throw err
}
console.log(`Failed to fetch`, err, `retrying...`)
}
}
}
export const runtime = 'experimental-edge'
export default async function Page() {
const data = await fetchRetry(
......
const fetchRetry = async (url, init) => {
for (let i = 0; i < 5; i++) {
try {
return await fetch(url, init)
} catch (err) {
if (i === 4) {
throw err
}
console.log(`Failed to fetch`, err, `retrying...`)
}
}
}
import { fetchRetry } from '../../../lib/fetch-retry'
export default async function Page() {
const data = await fetchRetry(
......
const fetchRetry = async (url, init) => {
for (let i = 0; i < 5; i++) {
try {
return await fetch(url, init)
} catch (err) {
if (i === 4) {
throw err
}
console.log(`Failed to fetch`, err, `retrying...`)
}
}
}
import { fetchRetry } from '../../../lib/fetch-retry'
export default async function Page() {
const data = await fetchRetry(
......
export const revalidate = 0
import { fetchRetry } from '../../../lib/fetch-retry'
const fetchRetry = async (url, init) => {
for (let i = 0; i < 5; i++) {
try {
return await fetch(url, init)
} catch (err) {
if (i === 4) {
throw err
}
console.log(`Failed to fetch`, err, `retrying...`)
}
}
}
export const revalidate = 0
export default async function Page() {
const data = await fetchRetry(
......
const MAX_ATTEMPTS = 5
export const fetchRetry = async (url, init) => {
for (let i = 0; i < MAX_ATTEMPTS; i++) {
try {
return await fetch(url, init)
} catch (err) {
// FIXME: (PPR) this is a workaround until we have a way to detect postpone errors
// For PPR, we need to detect if this is a postpone error so we can
// re-throw it.
if (err.$$typeof === Symbol.for('react.postpone')) {
throw err
}
if (i === MAX_ATTEMPTS - 1) {
throw err
}
console.log(`Failed to fetch`, err, `retrying...`)
}
}
}
{
"version": 2,
"suites": {
"test/e2e/app-dir/app-static/app-static.test.ts": {
"failed": [
"app-dir static/dynamic handling usePathname should have values from canonical url on rewrite",
"app-dir static/dynamic handling should have correct prerender-manifest entries",
"app-dir static/dynamic handling should output HTML/RSC files for static paths",
"app-dir static/dynamic handling should output debug info for static bailouts"
]
},
"test/e2e/app-dir/app-client-cache/client-cache.test.ts": {
"failed": [
"app dir client cache semantics prefetch={undefined} - default should re-use the full cache for only 30 seconds",
......@@ -37,6 +45,22 @@
"app dir - rsc basics should render server components correctly"
]
},
"test/e2e/app-dir/navigation/navigation.test.ts": {
"failed": [
"app dir - navigation redirect status code should respond with 308 status code if permanent flag is set",
"app dir - navigation redirect status code should respond with 307 status code in client component",
"app dir - navigation redirect status code should respond with 307 status code in server component",
"app dir - navigation bots should block rendering for bots and return 404 status",
"app dir - navigation navigation between pages and app should not continously initiate a mpa navigation to the same URL when router state changes"
]
},
"test/e2e/app-dir/app-static/app-static-custom-handler.test.ts": {
"failed": [
"app-dir static/dynamic handling should output debug info for static bailouts",
"app-dir static/dynamic handling should have correct prerender-manifest entries",
"app-dir static/dynamic handling should output HTML/RSC files for static paths"
]
},
"test/production/app-dir/unexpected-error/unexpected-error.test.ts": {
"failed": [
"unexpected-error should set response status to 500 for unexpected errors in ssr app route",
......@@ -57,11 +81,7 @@
"test/e2e/app-dir/ppr/**/*",
"test/e2e/app-dir/app-prefetch*/**/*",
"test/e2e/app-dir/interception-middleware-rewrite/interception-middleware-rewrite.test.ts",
"test/e2e/app-dir/searchparams-static-bailout/searchparams-static-bailout.test.ts",
"test/e2e/app-dir/app-static/app-static-custom-handler.test.ts",
"test/e2e/app-dir/app-static/app-static.test.ts",
"test/e2e/app-dir/app-static/app-static-custom-cache-handler-esm.test.ts",
"test/e2e/app-dir/navigation/navigation.test.ts"
"test/e2e/app-dir/searchparams-static-bailout/searchparams-static-bailout.test.ts"
]
}
}
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