Skip to content

Commit

Permalink
skip postpone to do dynamic render
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Feb 5, 2025
1 parent 1b4a2f4 commit 6dabe3a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
11 changes: 1 addition & 10 deletions packages/next/src/server/app-render/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1847,18 +1847,9 @@ async function renderToStream(
// one task before continuing
await waitAtLeastOneReactRenderTask()

// When streaming metadata is enabled and request UA is a html-limited bot, we should do a dynamic render.
const shouldDoDynamicRender =
ctx.renderOpts.botType === 'html' &&
ctx.renderOpts.experimental.streamingMetadata

// If provided, the postpone state should be parsed as JSON so it can be
// provided to React.
if (
typeof renderOpts.postponed === 'string' &&
// When we don't need to forcedly do a dynamic render, continue PPR renderer.
!shouldDoDynamicRender
) {
if (typeof renderOpts.postponed === 'string') {
if (postponedState?.type === DynamicState.DATA) {
// We have a complete HTML Document in the prerender but we need to
// still include the new server component render because it was not included
Expand Down
12 changes: 10 additions & 2 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,10 @@ import type { RouteModule } from './route-modules/route-module'
import { FallbackMode, parseFallbackField } from '../lib/fallback'
import { toResponseCacheEntry } from './response-cache/utils'
import { scheduleOnNextTick } from '../lib/scheduler'
import { shouldServeStreamingMetadata } from './lib/streaming-metadata'
import {
shouldServeStreamingMetadata,
shouldSkipPostponedState,
} from './lib/streaming-metadata'

export type FindComponentsResult = {
components: LoadComponentsReturnType
Expand Down Expand Up @@ -2097,6 +2100,11 @@ export default abstract class Server<
const hasDebugFallbackShellQuery =
hasDebugStaticShellQuery && query.__nextppronly === 'fallback'

const skipPostponed = shouldSkipPostponedState(
req,
this.renderOpts.experimental.streamingMetadata
)

// This page supports PPR if it is marked as being `PARTIALLY_STATIC` in the
// prerender manifest and this is an app page.
const isRoutePPREnabled: boolean =
Expand Down Expand Up @@ -2443,7 +2451,7 @@ export default abstract class Server<
isOnDemandRevalidate,
isDraftMode: isPreviewMode,
isServerAction,
postponed,
postponed: skipPostponed ? undefined : postponed,
waitUntil: this.getWaitUntil(),
onClose: res.onClose.bind(res),
onAfterTaskError: undefined,
Expand Down
18 changes: 17 additions & 1 deletion packages/next/src/server/lib/streaming-metadata.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { HTML_LIMITED_BOT_UA_RE_STRING } from '../../shared/lib/router/utils/is-bot'
import {
getBotType,
HTML_LIMITED_BOT_UA_RE_STRING,
} from '../../shared/lib/router/utils/is-bot'
import type { BaseNextRequest } from '../base-http'

export function shouldServeStreamingMetadata(
userAgent: string,
Expand All @@ -23,3 +27,15 @@ export function shouldServeStreamingMetadata(
!!userAgent && !blockingMetadataUARegex.test(userAgent)
)
}

// When streaming metadata is enabled and request UA is a html-limited bot, we should do a dynamic render.
// In this case, postpone state is not sent.
export function shouldSkipPostponedState(
req: BaseNextRequest,
streamingMetadata: boolean
): boolean {
const ua = req.headers['user-agent'] || ''
const botType = getBotType(ua)

return botType === 'html' && streamingMetadata
}
1 change: 0 additions & 1 deletion test/e2e/app-dir/ppr-metadata-streaming/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Link from 'next/link'
import { ReactNode } from 'react'

const hrefs = [
'/',
'/dynamic-metadata',
'/dynamic-metadata/partial',
'/dynamic-page',
Expand Down

0 comments on commit 6dabe3a

Please sign in to comment.