Unverified Commit fb3f0456 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Generate prefetch RSC paylod during build for SSR paths (#54403)

This ensures we prerender the prefetch RSC payload for non-fully
prerendered paths so that we don't need to invoke un-necessarily to
generate this payload.

x-ref: [slack
thread](https://vercel.slack.com/archives/C05KYT5S9FF/p1692155103311829)
parent 7a6b6c8a
......@@ -128,6 +128,7 @@ import { flatReaddir } from '../lib/flat-readdir'
import { eventSwcPlugins } from '../telemetry/events/swc-plugins'
import { normalizeAppPath } from '../shared/lib/router/utils/app-paths'
import {
NEXT_ROUTER_PREFETCH,
RSC,
RSC_CONTENT_TYPE_HEADER,
RSC_VARY_HEADER,
......@@ -223,6 +224,7 @@ export type RoutesManifest = {
rsc: {
header: typeof RSC
varyHeader: typeof RSC_VARY_HEADER
prefetchHeader: typeof NEXT_ROUTER_PREFETCH
}
skipMiddlewareUrlNormalize?: boolean
caseSensitive?: boolean
......@@ -782,6 +784,7 @@ export default async function build(
rsc: {
header: RSC,
varyHeader: RSC_VARY_HEADER,
prefetchHeader: NEXT_ROUTER_PREFETCH,
contentTypeHeader: RSC_CONTENT_TYPE_HEADER,
},
skipMiddlewareUrlNormalize: config.skipMiddlewareUrlNormalize,
......@@ -1135,6 +1138,7 @@ export default async function build(
const additionalSsgPaths = new Map<string, Array<string>>()
const additionalSsgPathsEncoded = new Map<string, Array<string>>()
const appStaticPaths = new Map<string, Array<string>>()
const appPrefetchPaths = new Map<string, string>()
const appStaticPathsEncoded = new Map<string, Array<string>>()
const appNormalizedPaths = new Map<string, string>()
const appDynamicParamPaths = new Set<string>()
......@@ -1653,6 +1657,14 @@ export default async function build(
appDynamicParamPaths.add(originalAppPath)
}
appDefaultConfigs.set(originalAppPath, appConfig)
if (
!isStatic &&
!isAppRouteRoute(originalAppPath) &&
!isDynamicRoute(originalAppPath)
) {
appPrefetchPaths.set(originalAppPath, page)
}
}
} else {
if (isEdgeRuntime(pageRuntime)) {
......@@ -2499,6 +2511,15 @@ export default async function build(
})
})
for (const [originalAppPath, page] of appPrefetchPaths) {
defaultMap[page] = {
page: originalAppPath,
query: {},
_isAppDir: true,
_isAppPrefetch: true,
}
}
if (i18n) {
for (const page of [
...staticPages,
......
......@@ -50,6 +50,11 @@ import {
signalFromNodeResponse,
} from '../server/web/spec-extension/adapters/next-request'
import * as ciEnvironment from '../telemetry/ci-info'
import {
NEXT_ROUTER_PREFETCH,
NEXT_URL,
RSC,
} from '../client/components/app-router-headers'
const envConfig = require('../shared/lib/runtime-config')
......@@ -164,6 +169,7 @@ export default async function exportPage({
const { page } = pathMap
const pathname = normalizeAppPath(page)
const isAppDir = Boolean(pathMap._isAppDir)
const isAppPrefetch = Boolean(pathMap._isAppPrefetch)
const isDynamicError = pathMap._isDynamicError
const filePath = normalizePagePath(path)
const isDynamic = isDynamicRoute(page)
......@@ -387,7 +393,45 @@ export default async function exportPage({
err.digest === NEXT_DYNAMIC_NO_SSR_CODE ||
isRedirectError(err)
if (isRouteHandler) {
const isNotFoundPage = page === '/_not-found'
const generatePrefetchRsc = async () => {
// If we bail for prerendering due to dynamic usage we need to
// generate a static prefetch payload to prevent invoking
// functions during runtime just for prefetching
const { renderToHTMLOrFlight } =
require('../server/app-render/app-render') as typeof import('../server/app-render/app-render')
req.headers[RSC.toLowerCase()] = '1'
req.headers[NEXT_URL.toLowerCase()] = path
req.headers[NEXT_ROUTER_PREFETCH.toLowerCase()] = '1'
curRenderOpts.supportsDynamicHTML = true
delete (curRenderOpts as any).isRevalidate
const prefetchRenderResult = await renderToHTMLOrFlight(
req as any,
res as any,
isNotFoundPage ? '/404' : pathname,
query,
curRenderOpts as any
)
prefetchRenderResult.pipe(res as import('http').ServerResponse)
await res.hasStreamed
const prefetchRscData = Buffer.concat(res.buffers).toString()
await promises.writeFile(
htmlFilepath.replace(/\.html$/, '.prefetch.rsc'),
prefetchRscData
)
}
// for dynamic routes with no generate static params
// we generate strictly the prefetch RSC payload to
// avoid attempting to render with default params e.g. [slug]
if (isAppPrefetch) {
await generatePrefetchRsc()
} else if (isRouteHandler) {
// Ensure that the url for the page is absolute.
req.url = `http://localhost:3000${req.url}`
const request = NextRequestAdapter.fromNodeNextRequest(
......@@ -493,8 +537,6 @@ export default async function exportPage({
try {
curRenderOpts.params ||= {}
const isNotFoundPage = page === '/_not-found'
const result = await renderToHTMLOrFlight(
req as any,
res as any,
......@@ -537,6 +579,8 @@ export default async function exportPage({
throw new Error(
`Page with dynamic = "error" encountered dynamic data method on ${path}.`
)
} else {
await generatePrefetchRsc()
}
const staticBailoutInfo = metadata.staticBailoutInfo || {}
......
......@@ -85,6 +85,8 @@ import {
RSC_VARY_HEADER,
FLIGHT_PARAMETERS,
NEXT_RSC_UNION_QUERY,
NEXT_ROUTER_PREFETCH,
RSC_CONTENT_TYPE_HEADER,
} from '../client/components/app-router-headers'
import {
MatchOptions,
......@@ -324,6 +326,10 @@ export default abstract class Server<ServerOptions extends Options = Options> {
renderOpts: RenderOpts
): Promise<RenderResult>
protected async getPrefetchRsc(_pathname: string): Promise<string | null> {
return null
}
protected abstract getIncrementalCache(options: {
requestHeaders: Record<string, undefined | string | string[]>
requestProtocol: 'http' | 'https'
......@@ -1935,6 +1941,31 @@ export default abstract class Server<ServerOptions extends Options = Options> {
} else if (
components.routeModule?.definition.kind === RouteKind.APP_PAGE
) {
const isAppPrefetch = req.headers[NEXT_ROUTER_PREFETCH.toLowerCase()]
if (
isAppPrefetch &&
ssgCacheKey &&
process.env.NODE_ENV === 'production'
) {
try {
const prefetchRsc = await this.getPrefetchRsc(ssgCacheKey)
if (prefetchRsc) {
res.setHeader(
'cache-control',
'private, no-cache, no-store, max-age=0, must-revalidate'
)
res.setHeader('content-type', RSC_CONTENT_TYPE_HEADER)
res.body(prefetchRsc).send()
return null
}
} catch (_) {
// we fallback to invoking the function if prefetch
// data is not available
}
}
const module = components.routeModule as AppPageRouteModule
// Due to the way we pass data by mutating `renderOpts`, we can't extend the
......
......@@ -309,6 +309,7 @@ export type ExportPathMap = {
page: string
query?: NextParsedUrlQuery
_isAppDir?: boolean
_isAppPrefetch?: boolean
_isDynamicError?: boolean
}
}
......
......@@ -1045,6 +1045,12 @@ export default class NextNodeServer extends BaseServer {
return this.runApi(req, res, query, match)
}
protected async getPrefetchRsc(pathname: string) {
return this.getCacheFilesystem()
.readFile(join(this.serverDistDir, 'app', `${pathname}.prefetch.rsc`))
.then((res) => res.toString())
}
protected getCacheFilesystem(): CacheFs {
return nodeFs
}
......
export default function DeploymentsBreakdownPage(props) {
console.log('rendering dashboard/(custom)/deployments/breakdown')
return (
<>
<p>hello from app/dashboard/(custom)/deployments/breakdown</p>
......
......@@ -16,6 +16,7 @@ async function getData({ params }) {
}
export default function DeploymentsPage(props) {
console.log('rendering /dashboard/deployments/[id]')
const data = use(getData(props))
return (
......
export default function AboutPage() {
console.log('rendering linking about page')
return <p id="about-page">About page</p>
}
......@@ -5,8 +5,18 @@ export default function Layout({ children }) {
<>
<header>
<nav>
<Link href="/linking">Home</Link>
<Link href="/linking/about">About</Link>
<Link href="/linking" id="home">
Home
</Link>
<Link href="/linking/about" id="about">
About
</Link>
<Link href="/dashboard/deployments/breakdown" id="breakdown">
/dashboard/deployments/breakdown
</Link>
<Link href="/dashboard/deployments/123" id="deployments">
/dashboard/deployments/123
</Link>
</nav>
</header>
{children}
......
export default function Page() {
console.log('rendering linking home page')
return <p id="home-page">Home page</p>
}
......@@ -3,6 +3,8 @@ import crypto from 'crypto'
import { check, getRedboxHeader, hasRedbox, waitFor } from 'next-test-utils'
import cheerio from 'cheerio'
import stripAnsi from 'strip-ansi'
import { BrowserInterface } from 'test/lib/browsers/base'
import { Request } from 'playwright-core'
createNextDescribe(
'app dir',
......@@ -11,6 +13,59 @@ createNextDescribe(
},
({ next, isNextDev: isDev, isNextStart, isNextDeploy }) => {
if (isNextStart) {
it('should use RSC prefetch data from build', async () => {
expect(
await next.readFile('.next/server/app/linking.prefetch.rsc')
).toBeTruthy()
expect(
await next.readFile('.next/server/app/linking/about.prefetch.rsc')
).toBeTruthy()
expect(
await next.readFile(
'.next/server/app/dashboard/deployments/breakdown.prefetch.rsc'
)
).toBeTruthy()
expect(
await next
.readFile(
'.next/server/app/dashboard/deployments/[id].prefetch.rsc'
)
.catch(() => false)
).toBeFalsy()
const outputStart = next.cliOutput.length
const browser: BrowserInterface = await next.browser('/')
const rscReqs = []
browser.on('request', (req: Request) => {
if (req.headers()['rsc']) {
rscReqs.push(req.url())
}
})
await browser.eval('window.location.href = "/linking"')
await check(async () => {
return rscReqs.length > 3 ? 'success' : JSON.stringify(rscReqs)
}, 'success')
const trimmedOutput = next.cliOutput.substring(outputStart)
expect(trimmedOutput).not.toContain(
'rendering dashboard/(custom)/deployments/breakdown'
)
expect(trimmedOutput).not.toContain(
'rendering /dashboard/deployments/[id]'
)
expect(trimmedOutput).not.toContain('rendering linking about page')
await browser.elementByCss('#breakdown').click()
await check(
() => next.cliOutput.substring(outputStart),
/rendering .*breakdown/
)
})
it('should have correct size in build output', async () => {
expect(next.cliOutput).toMatch(
/\/dashboard\/another.*? [^0]{1,} [\w]{1,}B/
......
......@@ -2660,6 +2660,7 @@ const runTests = (isDev = false, isTurbo = false) => {
contentTypeHeader: 'text/x-component',
varyHeader:
'RSC, Next-Router-State-Tree, Next-Router-Prefetch, Next-Url',
prefetchHeader: 'Next-Router-Prefetch',
},
})
})
......
......@@ -1493,6 +1493,7 @@ function runTests({ dev }) {
contentTypeHeader: 'text/x-component',
varyHeader:
'RSC, Next-Router-State-Tree, Next-Router-Prefetch, Next-Url',
prefetchHeader: 'Next-Router-Prefetch',
},
})
})
......
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