Unverified Commit 8de523ae authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Fix streaming server actions (#86148)

By accident, the responses for server actions that fetch something and
don't revalidate are currently not streamed to the browser until the
`fetch` calls are fully resolved. In the simplest case this happens when
a server action returns the response body of a `fetch` call. This is
especially critical if that response is itself streaming, for example
for a chat app. In that case the client didn't see any updates until the
whole stream was resolved. It affected mostly dev mode (due to the HMR
fetch cache), but also prod if the fetch was explicitly marked as
cacheable.

The cause of the issue is that we are awaiting `pendingRevalidates` (in
which the cache-writing promise is stored) after executing the action
and before rendering. However, when we skip rendering and return the
action directly this is unnecessary and blocks the response. Instead, we
can defer awaiting the `pendingRevalidates` using `waitUntil`, as we do
after rendering. This is executed at the very end, before the response
is closed.
parent bf27e24a
...@@ -617,8 +617,9 @@ export async function handleAction({ ...@@ -617,8 +617,9 @@ export async function handleAction({
type: 'done', type: 'done',
result: await generateFlight(req, ctx, requestStore, { result: await generateFlight(req, ctx, requestStore, {
actionResult: promise, actionResult: promise,
// We didn't execute an action, so no revalidations could have occurred. We can skip rendering the page. // We didn't execute an action, so no revalidations could have
skipFlight: true, // occurred. We can skip rendering the page.
skipPageRendering: true,
temporaryReferences, temporaryReferences,
}), }),
} }
...@@ -729,16 +730,16 @@ export async function handleAction({ ...@@ -729,16 +730,16 @@ export async function handleAction({
// Only warn if it's a server action, otherwise skip for other post requests // Only warn if it's a server action, otherwise skip for other post requests
warnBadServerActionRequest() warnBadServerActionRequest()
const actionReturnedState = const { actionResult } = await executeActionAndPrepareForRender(
await executeActionAndPrepareForRender( action as () => Promise<unknown>,
action as () => Promise<unknown>, [],
[], workStore,
workStore, requestStore,
requestStore actionWasForwarded
) )
const formState = await decodeFormState( const formState = await decodeFormState(
actionReturnedState, actionResult,
formData, formData,
serverModuleMap serverModuleMap
) )
...@@ -923,16 +924,16 @@ export async function handleAction({ ...@@ -923,16 +924,16 @@ export async function handleAction({
// Only warn if it's a server action, otherwise skip for other post requests // Only warn if it's a server action, otherwise skip for other post requests
warnBadServerActionRequest() warnBadServerActionRequest()
const actionReturnedState = const { actionResult } = await executeActionAndPrepareForRender(
await executeActionAndPrepareForRender( action as () => Promise<unknown>,
action as () => Promise<unknown>, [],
[], workStore,
workStore, requestStore,
requestStore actionWasForwarded
) )
const formState = await decodeFormState( const formState = await decodeFormState(
actionReturnedState, actionResult,
formData, formData,
serverModuleMap serverModuleMap
) )
...@@ -1022,27 +1023,32 @@ export async function handleAction({ ...@@ -1022,27 +1023,32 @@ export async function handleAction({
actionId! actionId!
] ]
const returnVal = await executeActionAndPrepareForRender( const { actionResult, skipPageRendering } =
actionHandler, await executeActionAndPrepareForRender(
boundActionArguments, actionHandler,
workStore, boundActionArguments,
requestStore workStore,
).finally(() => { requestStore,
addRevalidationHeader(res, { workStore, requestStore }) actionWasForwarded
}) ).finally(() => {
addRevalidationHeader(res, { workStore, requestStore })
})
// For form actions, we need to continue rendering the page. // For form actions, we need to continue rendering the page.
if (isFetchAction) { if (isFetchAction) {
const actionResult = await generateFlight(req, ctx, requestStore, {
actionResult: Promise.resolve(returnVal),
// if the page was not revalidated, or if the action was forwarded from another worker, we can skip the rendering the flight tree
skipFlight: !workStore.pathWasRevalidated || actionWasForwarded,
temporaryReferences,
})
return { return {
type: 'done', type: 'done',
result: actionResult, result: await generateFlight(req, ctx, requestStore, {
actionResult: Promise.resolve(actionResult),
skipPageRendering,
temporaryReferences,
// If we skip page rendering, we need to ensure pending
// revalidates are awaited before closing the response. Otherwise,
// this will be done after rendering the page.
waitUntil: skipPageRendering
? executeRevalidates(workStore)
: undefined,
}),
} }
} else { } else {
// TODO: this shouldn't be reachable, because all non-fetch codepaths return early. // TODO: this shouldn't be reachable, because all non-fetch codepaths return early.
...@@ -1101,7 +1107,7 @@ export async function handleAction({ ...@@ -1101,7 +1107,7 @@ export async function handleAction({
return { return {
type: 'done', type: 'done',
result: await generateFlight(req, ctx, requestStore, { result: await generateFlight(req, ctx, requestStore, {
skipFlight: false, skipPageRendering: false,
actionResult: promise, actionResult: promise,
temporaryReferences, temporaryReferences,
}), }),
...@@ -1138,8 +1144,10 @@ export async function handleAction({ ...@@ -1138,8 +1144,10 @@ export async function handleAction({
type: 'done', type: 'done',
result: await generateFlight(req, ctx, requestStore, { result: await generateFlight(req, ctx, requestStore, {
actionResult: promise, actionResult: promise,
// if the page was not revalidated, or if the action was forwarded from another worker, we can skip the rendering the flight tree // If the page was not revalidated, or if the action was forwarded
skipFlight: !workStore.pathWasRevalidated || actionWasForwarded, // from another worker, we can skip rendering the page.
skipPageRendering:
!workStore.pathWasRevalidated || actionWasForwarded,
temporaryReferences, temporaryReferences,
}), }),
} }
...@@ -1156,29 +1164,45 @@ async function executeActionAndPrepareForRender< ...@@ -1156,29 +1164,45 @@ async function executeActionAndPrepareForRender<
action: TFn, action: TFn,
args: Parameters<TFn>, args: Parameters<TFn>,
workStore: WorkStore, workStore: WorkStore,
requestStore: RequestStore requestStore: RequestStore,
): Promise<Awaited<ReturnType<TFn>>> { actionWasForwarded: boolean
): Promise<{
actionResult: Awaited<ReturnType<TFn>>
skipPageRendering: boolean
}> {
requestStore.phase = 'action' requestStore.phase = 'action'
let skipPageRendering = actionWasForwarded
try { try {
return await workUnitAsyncStorage.run(requestStore, () => const actionResult = await workUnitAsyncStorage.run(requestStore, () =>
action.apply(null, args) action.apply(null, args)
) )
} finally {
requestStore.phase = 'render'
// When we switch to the render phase, cookies() will return // If the page was not revalidated, or if the action was forwarded from
// `workUnitStore.cookies` instead of `workUnitStore.userspaceMutableCookies`. // another worker, we can skip rendering the page.
// We want the render to see any cookie writes that we performed during the action, skipPageRendering ||= !workStore.pathWasRevalidated
// so we need to update the immutable cookies to reflect the changes.
synchronizeMutableCookies(requestStore)
// The server action might have toggled draft mode, so we need to reflect return { actionResult, skipPageRendering }
// that in the work store to be up-to-date for subsequent rendering. } finally {
workStore.isDraftMode = requestStore.draftMode.isEnabled if (!skipPageRendering) {
requestStore.phase = 'render'
// If the action called revalidateTag/revalidatePath, then that might affect data used by the subsequent render,
// so we need to make sure all revalidations are applied before that // When we switch to the render phase, cookies() will return
await executeRevalidates(workStore) // `workUnitStore.cookies` instead of
// `workUnitStore.userspaceMutableCookies`. We want the render to see any
// cookie writes that we performed during the action, so we need to update
// the immutable cookies to reflect the changes.
synchronizeMutableCookies(requestStore)
// The server action might have toggled draft mode, so we need to reflect
// that in the work store to be up-to-date for subsequent rendering.
workStore.isDraftMode = requestStore.draftMode.isEnabled
// If the action called revalidateTag/revalidatePath, then that might
// affect data used by the subsequent render, so we need to make sure all
// revalidations are applied before that.
await executeRevalidates(workStore)
}
} }
} }
......
...@@ -453,7 +453,7 @@ async function generateDynamicRSCPayload( ...@@ -453,7 +453,7 @@ async function generateDynamicRSCPayload(
ctx: AppRenderContext, ctx: AppRenderContext,
options?: { options?: {
actionResult?: ActionResult actionResult?: ActionResult
skipFlight?: boolean skipPageRendering?: boolean
runtimePrefetchSentinel?: number runtimePrefetchSentinel?: number
} }
): Promise<RSCPayload> { ): Promise<RSCPayload> {
...@@ -485,7 +485,7 @@ async function generateDynamicRSCPayload( ...@@ -485,7 +485,7 @@ async function generateDynamicRSCPayload(
const serveStreamingMetadata = !!ctx.renderOpts.serveStreamingMetadata const serveStreamingMetadata = !!ctx.renderOpts.serveStreamingMetadata
if (!options?.skipFlight) { if (!options?.skipPageRendering) {
const preloadCallbacks: PreloadCallbacks = [] const preloadCallbacks: PreloadCallbacks = []
const { Viewport, Metadata, MetadataOutlet } = createMetadataComponents({ const { Viewport, Metadata, MetadataOutlet } = createMetadataComponents({
...@@ -588,10 +588,11 @@ async function generateDynamicFlightRenderResult( ...@@ -588,10 +588,11 @@ async function generateDynamicFlightRenderResult(
requestStore: RequestStore, requestStore: RequestStore,
options?: { options?: {
actionResult: ActionResult actionResult: ActionResult
skipFlight: boolean skipPageRendering: boolean
componentTree?: CacheNodeSeedData componentTree?: CacheNodeSeedData
preloadCallbacks?: PreloadCallbacks preloadCallbacks?: PreloadCallbacks
temporaryReferences?: WeakMap<any, string> temporaryReferences?: WeakMap<any, string>
waitUntil?: Promise<unknown>
} }
): Promise<RenderResult> { ): Promise<RenderResult> {
const { const {
...@@ -649,9 +650,11 @@ async function generateDynamicFlightRenderResult( ...@@ -649,9 +650,11 @@ async function generateDynamicFlightRenderResult(
} }
) )
return new FlightRenderResult(flightReadableStream, { return new FlightRenderResult(
fetchMetrics: workStore.fetchMetrics, flightReadableStream,
}) { fetchMetrics: workStore.fetchMetrics },
options?.waitUntil
)
} }
type RenderToReadableStreamServerOptions = NonNullable< type RenderToReadableStreamServerOptions = NonNullable<
......
...@@ -7,8 +7,13 @@ import RenderResult, { type RenderResultMetadata } from '../render-result' ...@@ -7,8 +7,13 @@ import RenderResult, { type RenderResultMetadata } from '../render-result'
export class FlightRenderResult extends RenderResult { export class FlightRenderResult extends RenderResult {
constructor( constructor(
response: string | ReadableStream<Uint8Array>, response: string | ReadableStream<Uint8Array>,
metadata: RenderResultMetadata = {} metadata: RenderResultMetadata = {},
waitUntil?: Promise<unknown>
) { ) {
super(response, { contentType: RSC_CONTENT_TYPE_HEADER, metadata }) super(response, {
contentType: RSC_CONTENT_TYPE_HEADER,
metadata,
waitUntil,
})
} }
} }
...@@ -220,25 +220,26 @@ async function createCachedDynamicResponse( ...@@ -220,25 +220,26 @@ async function createCachedDynamicResponse(
.finally(handleUnlock) .finally(handleUnlock)
const pendingRevalidateKey = `cache-set-${cacheKey}` const pendingRevalidateKey = `cache-set-${cacheKey}`
workStore.pendingRevalidates ??= {} const pendingRevalidates = (workStore.pendingRevalidates ??= {})
if (pendingRevalidateKey in workStore.pendingRevalidates) { let pendingRevalidatePromise = Promise.resolve()
// there is already a pending revalidate entry that we need to await to if (pendingRevalidateKey in pendingRevalidates) {
// avoid race conditions // There is already a pending revalidate entry that we need to await to
await workStore.pendingRevalidates[pendingRevalidateKey] // avoid race conditions.
pendingRevalidatePromise = pendingRevalidates[pendingRevalidateKey]
} }
workStore.pendingRevalidates[pendingRevalidateKey] = cacheSetPromise.finally( pendingRevalidates[pendingRevalidateKey] = pendingRevalidatePromise
() => { .then(() => cacheSetPromise)
.finally(() => {
// If the pending revalidate is not present in the store, then we have // If the pending revalidate is not present in the store, then we have
// nothing to delete. // nothing to delete.
if (!workStore.pendingRevalidates?.[pendingRevalidateKey]) { if (!pendingRevalidates?.[pendingRevalidateKey]) {
return return
} }
delete workStore.pendingRevalidates[pendingRevalidateKey] delete pendingRevalidates[pendingRevalidateKey]
} })
)
return cloned2 return cloned2
} }
......
import { nextTestSetup } from 'e2e-utils'
import { retry, waitFor } from 'next-test-utils'
describe('actions-streaming', () => {
const { next } = nextTestSetup({
files: __dirname,
})
describe('actions returning a ReadableStream', () => {
it('should properly stream the response without buffering', async () => {
const browser = await next.browser('/readable-stream')
await browser.elementById('stream-button').click()
expect(await browser.elementById('stream-button').text()).toBe(
'Streaming...'
)
// If we're streaming properly, we should see the first chunks arrive
// quickly.
expect(await browser.elementByCss('h3').text()).toMatch(
/Received \d+ chunks/
)
expect(await browser.elementById('chunks').text()).toInclude(
'Lorem ipsum dolor sit'
)
// Finally, wait for the response to finish streaming.
await waitFor(5000)
await retry(
async () => {
expect(await browser.elementByCss('h3').text()).toBe(
'Received 50 chunks'
)
expect(await browser.elementById('stream-button').text()).toBe(
'Start Stream'
)
},
10000,
1000
)
})
})
})
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import Link from 'next/link'
export default function Page() {
return (
<p>
<Link href="/readable-stream">Readable Stream</Link>
</p>
)
}
'use server'
export async function streamData(origin: string) {
const response = await fetch(new URL('/readable-stream/api', origin))
return response.body!
}
import { setTimeout } from 'timers/promises'
const loremIpsum =
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.\n'
export async function GET() {
const encoder = new TextEncoder()
const stream = new ReadableStream({
async start(controller) {
for (let i = 0; i < 50; i++) {
await setTimeout(100)
controller.enqueue(encoder.encode(loremIpsum))
}
controller.close()
},
})
return new Response(stream, { headers: { 'Content-Type': 'text/plain' } })
}
'use client'
import { useState } from 'react'
import { streamData } from './actions'
export default function Page() {
const [chunks, setChunks] = useState<string[] | null>(null)
const [isStreaming, setIsStreaming] = useState(false)
const handleClick = async () => {
setChunks(null)
setIsStreaming(true)
const stream = await streamData(window.location.origin)
const reader = stream.getReader()
const decoder = new TextDecoder()
try {
while (true) {
const { done, value } = await reader.read()
if (done) {
break
}
const chunk = decoder.decode(value, { stream: true })
setChunks((prev) => (prev ? [...prev, chunk] : [chunk]))
}
} finally {
reader.releaseLock()
setIsStreaming(false)
}
}
return (
<div>
<button disabled={isStreaming} onClick={handleClick} id="stream-button">
{isStreaming ? 'Streaming...' : 'Start Stream'}
</button>
{chunks && (
<>
<h3>Received {chunks.length} chunks</h3>
<ol id="chunks">
{chunks.map((chunk, i) => (
<li key={i}>{chunk}</li>
))}
</ol>
</>
)}
</div>
)
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}
module.exports = nextConfig
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