Unverified Commit e1f9057f authored by David Alexandru Ilie's avatar David Alexandru Ilie Committed by GitHub

Make postponed resume mode explicit

parent 10b0db32
import type { LoaderTree } from '../../server/lib/app-dir-module'
import type { IncomingMessage, ServerResponse } from 'node:http'
import type { FallbackRouteParam } from '../static-paths/types'
import type { AppPageRenderOperation } from '../../server/app-render/types'
import {
AppPageRouteModule,
......@@ -90,8 +91,6 @@ import { InvariantError } from '../../shared/lib/invariant-error' with { 'turbop
import { scheduleOnNextTick } from '../../lib/scheduler' with { 'turbopack-transition': 'next-server-utility' }
import { getSegmentParam } from '../../shared/lib/router/utils/get-segment-param' with { 'turbopack-transition': 'next-server-utility' }
type AppPageRenderOperation = 'render' | 'prerender'
/**
* Builds the cache key for the most complete prerenderable shell we can derive
* from the shell that matched this request. Only params that can still be
......@@ -861,10 +860,11 @@ export function createAppPageEntrypoint({
routeModule,
page: srcPage,
postponed,
renderOperation,
allowEmptyStaticShell,
serveStreamingMetadata,
supportsDynamicResponse:
renderOperation === 'render' &&
renderOperation !== 'prerender' &&
(typeof postponed === 'string' || supportsDynamicResponse),
buildManifest,
nextFontManifest,
......@@ -1567,12 +1567,25 @@ export function createAppPageEntrypoint({
!isDebugPrerender &&
(supportsDynamicResponse || isPossibleServerAction)
const isResumeRender =
!forceStaticRender &&
!isDebugPrerender &&
typeof postponed === 'string' &&
hasPostponedState &&
!isPossibleServerAction
const renderOperation: AppPageRenderOperation = isResumeRender
? 'resume'
: isRequestSpecificRender
? 'render'
: 'prerender'
// Perform the render.
return doRender({
span,
postponed,
fallbackRouteParams,
renderOperation: isRequestSpecificRender ? 'render' : 'prerender',
renderOperation,
allowEmptyStaticShell: isInstantNavigationTest || undefined,
})
} catch (err) {
......@@ -2109,7 +2122,7 @@ export function createAppPageEntrypoint({
// This is a resume render, not a fallback render. Fallback params
// (for cacheComponents routes) are plumbed via request meta above.
fallbackRouteParams: null,
renderOperation: 'render',
renderOperation: 'resume',
})
.then(async (result) => {
if (!result) {
......
......@@ -131,6 +131,7 @@ async function requestHandler(
params,
page: srcPage,
postponed: undefined,
renderOperation: 'render',
serveStreamingMetadata: true,
supportsDynamicResponse: true,
buildManifest,
......
......@@ -3324,11 +3324,10 @@ function prepareAppPage(
)
// If provided, parse the postponed state so its RDC is available to the
// render. Keep the React state only for requests that can resume it.
// render.
let postponedState: PostponedState | null = null
if (typeof renderOpts.postponed === 'string') {
const { isFetchAction, isPossibleMPAAction } =
getServerActionRequestMetadata(req)
const { isFetchAction } = getServerActionRequestMetadata(req)
if (fallbackRouteParams) {
if (!isFetchAction) {
......@@ -3348,18 +3347,6 @@ function prepareAppPage(
renderOpts.experimental.disableResumeDataCacheCompression
),
}
} else if (isPossibleMPAAction) {
// An MPA action needs a complete HTML document, so it cannot resume the
// React render. Its RDC is still needed to keep cached values consistent
// with the static shell when the action did not invalidate them.
postponedState = {
type: DynamicState.DATA,
renderResumeDataCache: parseResumeDataCacheFromPostponedState(
renderOpts.postponed,
renderOpts.experimental.maxPostponedStateSizeBytes,
renderOpts.experimental.disableResumeDataCacheCompression
),
}
} else {
postponedState = parsePostponedState(
renderOpts.postponed,
......@@ -3548,8 +3535,6 @@ async function renderToStream(
): Promise<AnyStream> {
/* eslint-disable @next/internal/no-ambiguous-jsx -- React Client */
// MARK: renderToStream setup
const { isPossibleMPAAction } = getServerActionRequestMetadata(req)
const {
assetPrefix,
htmlRequestId,
......@@ -3559,10 +3544,9 @@ async function renderToStream(
requestId,
workStore,
} = ctx
// MPA actions reuse the RDC from the postponed state, but their response
// must come from a complete server render rather than a postponed response.
const shouldUsePostponedResponse =
typeof renderOpts.postponed === 'string' && !isPossibleMPAAction
typeof renderOpts.postponed === 'string' &&
renderOpts.renderOperation === 'resume'
const {
basePath,
......
......@@ -91,6 +91,8 @@ export type ServerOnInstrumentationRequestError = (
silenceLog: boolean
) => void | Promise<void>
export type AppPageRenderOperation = 'render' | 'prerender' | 'resume'
export interface RenderOptsPartial {
dir?: string
previewProps: __ApiPreviewProps | undefined
......@@ -102,6 +104,12 @@ export interface RenderOptsPartial {
trailingSlash: boolean
images: ImageConfigComplete
supportsDynamicResponse: boolean
/**
* Describes how the outer runtime will compose this render's HTML. A resume
* render is appended to an existing shell; render and prerender operations
* must produce standalone output.
*/
renderOperation?: AppPageRenderOperation
runtime?: ServerRuntime
serverComponents?: boolean
enableTainting?: boolean
......
......@@ -10,7 +10,6 @@ export function getServerActionRequestMetadata(
isURLEncodedAction: boolean
isMultipartAction: boolean
isFetchAction: boolean
isPossibleMPAAction: boolean
isPossibleServerAction: boolean
} {
let actionId: string | null
......@@ -38,17 +37,16 @@ export function getServerActionRequestMetadata(
typeof actionId === 'string' &&
req.method === 'POST'
)
const isPossibleMPAAction =
!isFetchAction && (isURLEncodedAction || isMultipartAction)
const isPossibleServerAction = Boolean(isFetchAction || isPossibleMPAAction)
const isPossibleServerAction = Boolean(
isFetchAction || isURLEncodedAction || isMultipartAction
)
return {
actionId,
isURLEncodedAction,
isMultipartAction,
isFetchAction,
isPossibleMPAAction,
isPossibleServerAction,
}
}
......
import type { AppPageRouteDefinition } from '../../route-definitions/app-page-route-definition'
import type RenderResult from '../../render-result'
import type { RenderOpts } from '../../app-render/types'
import type { AppPageRenderOperation, RenderOpts } from '../../app-render/types'
import { addRequestMeta, type NextParsedUrlQuery } from '../../request-meta'
import type { LoaderTree } from '../../lib/app-dir-module'
import type { PrerenderManifest } from '../../../build'
......@@ -75,7 +75,7 @@ export interface AppPageRouteHandlerContext extends RouteModuleHandleContext {
page: string
query: NextParsedUrlQuery
fallbackRouteParams: OpaqueFallbackRouteParams | null
renderOpts: RenderOpts
renderOpts: RenderOpts & { renderOperation: AppPageRenderOperation }
serverComponentsHmrCache?: ServerComponentsHmrCache
sharedContext: AppSharedContext
}
......
......@@ -55,6 +55,22 @@ describe('cache-components', () => {
})
})
it('should return complete HTML for an unrelated urlencoded POST', async () => {
const response = await next.fetch('/server-action-mpa-partial', {
method: 'POST',
headers: {
'content-type': 'application/x-www-form-urlencoded',
},
body: 'foo=bar',
})
const html = await response.text()
expect(response.status).toBe(200)
expect(html).toStartWith('<!DOCTYPE html>')
expect(html).toContain('id="cached-timestamp"')
expect(html).toEndWith('</body></html>')
})
it('should not have cache components errors when encoding bound args for inline server actions', async () => {
const browser = await next.browser('/server-action-inline')
expect(await browser.elementByCss('p').text()).toBe('initial')
......
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