Skip to content

Commit

Permalink
store isPrerender flag in RSC payload
Browse files Browse the repository at this point in the history
  • Loading branch information
ztanner committed Sep 30, 2024
1 parent 501223a commit 585ae55
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 31 deletions.
1 change: 1 addition & 0 deletions packages/next/src/client/app-index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ const pendingActionQueue: Promise<AppRouterActionQueue> = new Promise(
location: window.location,
couldBeIntercepted: initialRSCPayload.i,
postponed: initialRSCPayload.s,
isPrerender: initialRSCPayload.S,
})
)
)
Expand Down
1 change: 0 additions & 1 deletion packages/next/src/client/components/app-router-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ export const FLIGHT_HEADERS = [
export const NEXT_RSC_UNION_QUERY = '_rsc' as const

export const NEXT_DID_POSTPONE_HEADER = 'x-nextjs-postponed' as const
export const NEXT_IS_PRERENDER_HEADER = 'x-nextjs-prerender' as const
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
RSC_HEADER,
RSC_CONTENT_TYPE_HEADER,
NEXT_HMR_REFRESH_HEADER,
NEXT_IS_PRERENDER_HEADER,
NEXT_DID_POSTPONE_HEADER,
} from '../app-router-headers'
import { callServer } from '../../app-call-server'
Expand Down Expand Up @@ -176,7 +175,6 @@ export async function fetchServerResponse(

const contentType = res.headers.get('content-type') || ''
const interception = !!res.headers.get('vary')?.includes(NEXT_URL)
const isPrerender = !!res.headers.get(NEXT_IS_PRERENDER_HEADER)
const postponed = !!res.headers.get(NEXT_DID_POSTPONE_HEADER)
let isFlightResponse = contentType.startsWith(RSC_CONTENT_TYPE_HEADER)

Expand Down Expand Up @@ -223,7 +221,7 @@ export async function fetchServerResponse(
flightData: normalizeFlightData(response.f),
canonicalUrl: canonicalUrl,
couldBeIntercepted: interception,
isPrerender: isPrerender,
isPrerender: response.S,
postponed,
}
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type {
import { callServer } from '../../../app-call-server'
import {
ACTION_HEADER,
NEXT_IS_PRERENDER_HEADER,
NEXT_ROUTER_STATE_TREE_HEADER,
NEXT_URL,
RSC_CONTENT_TYPE_HEADER,
Expand Down Expand Up @@ -106,7 +105,6 @@ async function fetchServerAction(
redirectType = undefined
}

const isPrerender = !!res.headers.get(NEXT_IS_PRERENDER_HEADER)
let revalidatedParts: FetchServerActionResult['revalidatedParts']
try {
const revalidatedHeader = JSON.parse(
Expand Down Expand Up @@ -150,7 +148,7 @@ async function fetchServerAction(
redirectLocation,
redirectType,
revalidatedParts,
isPrerender,
isPrerender: response.S,
}
}

Expand All @@ -160,7 +158,7 @@ async function fetchServerAction(
redirectLocation,
redirectType,
revalidatedParts,
isPrerender,
isPrerender: response.S,
}
}

Expand All @@ -180,7 +178,7 @@ async function fetchServerAction(
redirectLocation,
redirectType,
revalidatedParts,
isPrerender,
isPrerender: false,
}
}

Expand Down
4 changes: 0 additions & 4 deletions packages/next/src/export/routes/app-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { hasNextSupport } from '../../server/ci-info'
import { lazyRenderAppPage } from '../../server/route-modules/app-page/module.render'
import { isBailoutToCSRError } from '../../shared/lib/lazy-dynamic/bailout-to-csr'
import { NodeNextRequest, NodeNextResponse } from '../../server/base-http/node'
import { NEXT_IS_PRERENDER_HEADER } from '../../client/components/app-router-headers'
import type { FetchMetrics } from '../../server/base-http'
import type { StaticGenerationStore } from '../../client/components/static-generation-async-storage.external'
import type { FallbackRouteParams } from '../../server/request/fallback-params'
Expand Down Expand Up @@ -137,9 +136,6 @@ export async function exportAppPage(

const headers: OutgoingHttpHeaders = { ...metadata.headers }

// If we're writing the file to disk, we know it's a prerender.
headers[NEXT_IS_PRERENDER_HEADER] = '1'

if (fetchTags) {
headers[NEXT_CACHE_TAGS_HEADER] = fetchTags
}
Expand Down
6 changes: 6 additions & 0 deletions packages/next/src/server/app-render/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -453,13 +453,15 @@ async function generateDynamicRSCPayload(
a: options.actionResult,
f: flightData,
b: ctx.renderOpts.buildId,
S: staticGenerationStore.isStaticGeneration,
}
}

// Otherwise, it's a regular RSC response.
return {
b: ctx.renderOpts.buildId,
f: flightData,
S: staticGenerationStore.isStaticGeneration,
}
}

Expand Down Expand Up @@ -642,6 +644,7 @@ async function getRSCPayload(
m: missingSlots,
G: GlobalError,
s: typeof ctx.renderOpts.postponed === 'string',
S: staticGenerationStore.isStaticGeneration,
}
}

Expand Down Expand Up @@ -731,6 +734,7 @@ async function getErrorRSCPayload(
f: [[initialTree, initialSeedData, initialHead]],
G: GlobalError,
s: typeof ctx.renderOpts.postponed === 'string',
S: staticGenerationStore.isStaticGeneration,
} satisfies InitialRSCPayload
}

Expand Down Expand Up @@ -767,6 +771,7 @@ function App<T>({
location: null,
couldBeIntercepted: response.i,
postponed: response.s,
isPrerender: response.S,
})

const actionQueue = createMutableActionQueue(initialState)
Expand Down Expand Up @@ -825,6 +830,7 @@ function AppWithoutContext<T>({
location: null,
couldBeIntercepted: response.i,
postponed: response.s,
isPrerender: response.S,
})

const actionQueue = createMutableActionQueue(initialState)
Expand Down
6 changes: 6 additions & 0 deletions packages/next/src/server/app-render/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ export type InitialRSCPayload = {
G: React.ComponentType<any>
/** postponed */
s: boolean
/** isPrerender */
S: boolean
}

// Response from `createFromFetch` for normal rendering
Expand All @@ -231,6 +233,8 @@ export type NavigationFlightResponse = {
b: string
/** flightData */
f: FlightData
/** isPrerender */
S: boolean
}

// Response from `createFromFetch` for server actions. Action's flight data can be null
Expand All @@ -241,6 +245,8 @@ export type ActionFlightResponse = {
b: string
/** flightData */
f: FlightData
/** isPrerender */
S: boolean
}

export type RSCPayload =
Expand Down
31 changes: 13 additions & 18 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ import {
NEXT_DID_POSTPONE_HEADER,
NEXT_URL,
NEXT_ROUTER_STATE_TREE_HEADER,
NEXT_IS_PRERENDER_HEADER,
} from '../client/components/app-router-headers'
import type {
MatchOptions,
Expand Down Expand Up @@ -3142,25 +3141,21 @@ export default abstract class Server<
// data. If this is a Dynamic RSC request or wasn't a Prefetch RSC
// request, then we should set the cache header.
!isDynamicRSCRequest &&
!this.minimalMode &&
(!didPostpone || isPrefetchRSCRequest)
) {
if (!this.minimalMode) {
// set x-nextjs-cache header to match the header
// we set for the image-optimizer
res.setHeader(
'x-nextjs-cache',
isOnDemandRevalidate
? 'REVALIDATED'
: cacheEntry.isMiss
? 'MISS'
: cacheEntry.isStale
? 'STALE'
: 'HIT'
)
}
// Set a header used by the client router to signal the response is static
// and should respect the `static` cache staleTime value.
res.setHeader(NEXT_IS_PRERENDER_HEADER, '1')
// set x-nextjs-cache header to match the header
// we set for the image-optimizer
res.setHeader(
'x-nextjs-cache',
isOnDemandRevalidate
? 'REVALIDATED'
: cacheEntry.isMiss
? 'MISS'
: cacheEntry.isStale
? 'STALE'
: 'HIT'
)
}

const { value: cachedData } = cacheEntry
Expand Down

0 comments on commit 585ae55

Please sign in to comment.