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 { LoaderTree } from '../../server/lib/app-dir-module'
import type { IncomingMessage, ServerResponse } from 'node:http' import type { IncomingMessage, ServerResponse } from 'node:http'
import type { FallbackRouteParam } from '../static-paths/types' import type { FallbackRouteParam } from '../static-paths/types'
import type { AppPageRenderOperation } from '../../server/app-render/types'
import { import {
AppPageRouteModule, AppPageRouteModule,
...@@ -90,8 +91,6 @@ import { InvariantError } from '../../shared/lib/invariant-error' with { 'turbop ...@@ -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 { 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' } 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 * 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 * from the shell that matched this request. Only params that can still be
...@@ -861,10 +860,11 @@ export function createAppPageEntrypoint({ ...@@ -861,10 +860,11 @@ export function createAppPageEntrypoint({
routeModule, routeModule,
page: srcPage, page: srcPage,
postponed, postponed,
renderOperation,
allowEmptyStaticShell, allowEmptyStaticShell,
serveStreamingMetadata, serveStreamingMetadata,
supportsDynamicResponse: supportsDynamicResponse:
renderOperation === 'render' && renderOperation !== 'prerender' &&
(typeof postponed === 'string' || supportsDynamicResponse), (typeof postponed === 'string' || supportsDynamicResponse),
buildManifest, buildManifest,
nextFontManifest, nextFontManifest,
...@@ -1567,12 +1567,25 @@ export function createAppPageEntrypoint({ ...@@ -1567,12 +1567,25 @@ export function createAppPageEntrypoint({
!isDebugPrerender && !isDebugPrerender &&
(supportsDynamicResponse || isPossibleServerAction) (supportsDynamicResponse || isPossibleServerAction)
const isResumeRender =
!forceStaticRender &&
!isDebugPrerender &&
typeof postponed === 'string' &&
hasPostponedState &&
!isPossibleServerAction
const renderOperation: AppPageRenderOperation = isResumeRender
? 'resume'
: isRequestSpecificRender
? 'render'
: 'prerender'
// Perform the render. // Perform the render.
return doRender({ return doRender({
span, span,
postponed, postponed,
fallbackRouteParams, fallbackRouteParams,
renderOperation: isRequestSpecificRender ? 'render' : 'prerender', renderOperation,
allowEmptyStaticShell: isInstantNavigationTest || undefined, allowEmptyStaticShell: isInstantNavigationTest || undefined,
}) })
} catch (err) { } catch (err) {
...@@ -2109,7 +2122,7 @@ export function createAppPageEntrypoint({ ...@@ -2109,7 +2122,7 @@ export function createAppPageEntrypoint({
// This is a resume render, not a fallback render. Fallback params // This is a resume render, not a fallback render. Fallback params
// (for cacheComponents routes) are plumbed via request meta above. // (for cacheComponents routes) are plumbed via request meta above.
fallbackRouteParams: null, fallbackRouteParams: null,
renderOperation: 'render', renderOperation: 'resume',
}) })
.then(async (result) => { .then(async (result) => {
if (!result) { if (!result) {
......
...@@ -131,6 +131,7 @@ async function requestHandler( ...@@ -131,6 +131,7 @@ async function requestHandler(
params, params,
page: srcPage, page: srcPage,
postponed: undefined, postponed: undefined,
renderOperation: 'render',
serveStreamingMetadata: true, serveStreamingMetadata: true,
supportsDynamicResponse: true, supportsDynamicResponse: true,
buildManifest, buildManifest,
......
...@@ -3324,11 +3324,10 @@ function prepareAppPage( ...@@ -3324,11 +3324,10 @@ function prepareAppPage(
) )
// If provided, parse the postponed state so its RDC is available to the // 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 let postponedState: PostponedState | null = null
if (typeof renderOpts.postponed === 'string') { if (typeof renderOpts.postponed === 'string') {
const { isFetchAction, isPossibleMPAAction } = const { isFetchAction } = getServerActionRequestMetadata(req)
getServerActionRequestMetadata(req)
if (fallbackRouteParams) { if (fallbackRouteParams) {
if (!isFetchAction) { if (!isFetchAction) {
...@@ -3348,18 +3347,6 @@ function prepareAppPage( ...@@ -3348,18 +3347,6 @@ function prepareAppPage(
renderOpts.experimental.disableResumeDataCacheCompression 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 { } else {
postponedState = parsePostponedState( postponedState = parsePostponedState(
renderOpts.postponed, renderOpts.postponed,
...@@ -3548,8 +3535,6 @@ async function renderToStream( ...@@ -3548,8 +3535,6 @@ async function renderToStream(
): Promise<AnyStream> { ): Promise<AnyStream> {
/* eslint-disable @next/internal/no-ambiguous-jsx -- React Client */ /* eslint-disable @next/internal/no-ambiguous-jsx -- React Client */
// MARK: renderToStream setup // MARK: renderToStream setup
const { isPossibleMPAAction } = getServerActionRequestMetadata(req)
const { const {
assetPrefix, assetPrefix,
htmlRequestId, htmlRequestId,
...@@ -3559,10 +3544,9 @@ async function renderToStream( ...@@ -3559,10 +3544,9 @@ async function renderToStream(
requestId, requestId,
workStore, workStore,
} = ctx } = 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 = const shouldUsePostponedResponse =
typeof renderOpts.postponed === 'string' && !isPossibleMPAAction typeof renderOpts.postponed === 'string' &&
renderOpts.renderOperation === 'resume'
const { const {
basePath, basePath,
......
...@@ -91,6 +91,8 @@ export type ServerOnInstrumentationRequestError = ( ...@@ -91,6 +91,8 @@ export type ServerOnInstrumentationRequestError = (
silenceLog: boolean silenceLog: boolean
) => void | Promise<void> ) => void | Promise<void>
export type AppPageRenderOperation = 'render' | 'prerender' | 'resume'
export interface RenderOptsPartial { export interface RenderOptsPartial {
dir?: string dir?: string
previewProps: __ApiPreviewProps | undefined previewProps: __ApiPreviewProps | undefined
...@@ -102,6 +104,12 @@ export interface RenderOptsPartial { ...@@ -102,6 +104,12 @@ export interface RenderOptsPartial {
trailingSlash: boolean trailingSlash: boolean
images: ImageConfigComplete images: ImageConfigComplete
supportsDynamicResponse: boolean 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 runtime?: ServerRuntime
serverComponents?: boolean serverComponents?: boolean
enableTainting?: boolean enableTainting?: boolean
......
...@@ -10,7 +10,6 @@ export function getServerActionRequestMetadata( ...@@ -10,7 +10,6 @@ export function getServerActionRequestMetadata(
isURLEncodedAction: boolean isURLEncodedAction: boolean
isMultipartAction: boolean isMultipartAction: boolean
isFetchAction: boolean isFetchAction: boolean
isPossibleMPAAction: boolean
isPossibleServerAction: boolean isPossibleServerAction: boolean
} { } {
let actionId: string | null let actionId: string | null
...@@ -38,17 +37,16 @@ export function getServerActionRequestMetadata( ...@@ -38,17 +37,16 @@ export function getServerActionRequestMetadata(
typeof actionId === 'string' && typeof actionId === 'string' &&
req.method === 'POST' req.method === 'POST'
) )
const isPossibleMPAAction =
!isFetchAction && (isURLEncodedAction || isMultipartAction)
const isPossibleServerAction = Boolean(isFetchAction || isPossibleMPAAction) const isPossibleServerAction = Boolean(
isFetchAction || isURLEncodedAction || isMultipartAction
)
return { return {
actionId, actionId,
isURLEncodedAction, isURLEncodedAction,
isMultipartAction, isMultipartAction,
isFetchAction, isFetchAction,
isPossibleMPAAction,
isPossibleServerAction, isPossibleServerAction,
} }
} }
......
import type { AppPageRouteDefinition } from '../../route-definitions/app-page-route-definition' import type { AppPageRouteDefinition } from '../../route-definitions/app-page-route-definition'
import type RenderResult from '../../render-result' 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 { addRequestMeta, type NextParsedUrlQuery } from '../../request-meta'
import type { LoaderTree } from '../../lib/app-dir-module' import type { LoaderTree } from '../../lib/app-dir-module'
import type { PrerenderManifest } from '../../../build' import type { PrerenderManifest } from '../../../build'
...@@ -75,7 +75,7 @@ export interface AppPageRouteHandlerContext extends RouteModuleHandleContext { ...@@ -75,7 +75,7 @@ export interface AppPageRouteHandlerContext extends RouteModuleHandleContext {
page: string page: string
query: NextParsedUrlQuery query: NextParsedUrlQuery
fallbackRouteParams: OpaqueFallbackRouteParams | null fallbackRouteParams: OpaqueFallbackRouteParams | null
renderOpts: RenderOpts renderOpts: RenderOpts & { renderOperation: AppPageRenderOperation }
serverComponentsHmrCache?: ServerComponentsHmrCache serverComponentsHmrCache?: ServerComponentsHmrCache
sharedContext: AppSharedContext sharedContext: AppSharedContext
} }
......
...@@ -55,6 +55,22 @@ describe('cache-components', () => { ...@@ -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 () => { it('should not have cache components errors when encoding bound args for inline server actions', async () => {
const browser = await next.browser('/server-action-inline') const browser = await next.browser('/server-action-inline')
expect(await browser.elementByCss('p').text()).toBe('initial') 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