Unverified Commit 707afe1d authored by Gerald Monaco's avatar Gerald Monaco Committed by GitHub

Add `RenderResult` (#27319)

Adds `RenderResult`, replacing the `string` that `renderToHTML` used to return, with an `Observable`-like API that callers can use to subscribe and get a callback when chunks are available to flush, etc.

This is the last architectural change needed for streaming. There are, however, other things currently standing in the way of streaming. For example, it is common to mutate `res` in `getServerSideProps` to do routing work, or write headers, before fetching page data. This pattern effectively nullifies any advantages of streaming. I may do a follow-up PR that adds an experimental alternative for applications not using React 18, but the main purpose for this support is for Suspense and Server Components.

For that reason, there's no actual streaming here yet: instead we just flush a single chunk. A follow-up PR will add support for streaming suspense boundaries in React 18.
parent 26105e28
...@@ -11,6 +11,7 @@ import { setLazyProp, getCookieParser } from '../../../../server/api-utils' ...@@ -11,6 +11,7 @@ import { setLazyProp, getCookieParser } from '../../../../server/api-utils'
import { getRedirectStatus } from '../../../../lib/load-custom-routes' import { getRedirectStatus } from '../../../../lib/load-custom-routes'
import getRouteNoAssetPath from '../../../../shared/lib/router/utils/get-route-from-asset-path' import getRouteNoAssetPath from '../../../../shared/lib/router/utils/get-route-from-asset-path'
import { PERMANENT_REDIRECT_STATUS } from '../../../../shared/lib/constants' import { PERMANENT_REDIRECT_STATUS } from '../../../../shared/lib/constants'
import { resultToChunks } from '../../../../server/utils'
export function getPageHandler(ctx: ServerlessHandlerCtx) { export function getPageHandler(ctx: ServerlessHandlerCtx) {
const { const {
...@@ -333,11 +334,11 @@ export function getPageHandler(ctx: ServerlessHandlerCtx) { ...@@ -333,11 +334,11 @@ export function getPageHandler(ctx: ServerlessHandlerCtx) {
defaultLocale: i18n?.defaultLocale, defaultLocale: i18n?.defaultLocale,
}) })
) )
const html = result2 ? (await resultToChunks(result2)).join('') : ''
sendPayload( sendPayload(
req, req,
res, res,
result2, html,
'html', 'html',
{ {
generateEtags, generateEtags,
...@@ -401,7 +402,7 @@ export function getPageHandler(ctx: ServerlessHandlerCtx) { ...@@ -401,7 +402,7 @@ export function getPageHandler(ctx: ServerlessHandlerCtx) {
} }
if (renderMode) return { html: result, renderOpts } if (renderMode) return { html: result, renderOpts }
return result return result ? (await resultToChunks(result)).join('') : null
} catch (err) { } catch (err) {
if (!parsedUrl!) { if (!parsedUrl!) {
parsedUrl = parseUrl(req.url!, true) parsedUrl = parseUrl(req.url!, true)
...@@ -463,7 +464,7 @@ export function getPageHandler(ctx: ServerlessHandlerCtx) { ...@@ -463,7 +464,7 @@ export function getPageHandler(ctx: ServerlessHandlerCtx) {
err: res.statusCode === 404 ? undefined : err, err: res.statusCode === 404 ? undefined : err,
}) })
) )
return result2 return result2 ? (await resultToChunks(result2)).join('') : null
} }
} }
......
...@@ -376,6 +376,8 @@ export default async function exportApp( ...@@ -376,6 +376,8 @@ export default async function exportApp(
domainLocales: i18n?.domains, domainLocales: i18n?.domains,
trailingSlash: nextConfig.trailingSlash, trailingSlash: nextConfig.trailingSlash,
disableOptimizedLoading: nextConfig.experimental.disableOptimizedLoading, disableOptimizedLoading: nextConfig.experimental.disableOptimizedLoading,
// TODO: We should support dynamic HTML too
requireStaticHTML: true,
} }
const { serverRuntimeConfig, publicRuntimeConfig } = nextConfig const { serverRuntimeConfig, publicRuntimeConfig } = nextConfig
......
...@@ -18,6 +18,7 @@ import { FontManifest } from '../server/font-utils' ...@@ -18,6 +18,7 @@ import { FontManifest } from '../server/font-utils'
import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path' import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
import { trace } from '../telemetry/trace' import { trace } from '../telemetry/trace'
import { isInAmpMode } from '../shared/lib/amp' import { isInAmpMode } from '../shared/lib/amp'
import { resultFromChunks, resultToChunks } from '../server/utils'
const envConfig = require('../shared/lib/runtime-config') const envConfig = require('../shared/lib/runtime-config')
...@@ -226,7 +227,7 @@ export default async function exportPage({ ...@@ -226,7 +227,7 @@ export default async function exportPage({
let htmlFilepath = join(outDir, htmlFilename) let htmlFilepath = join(outDir, htmlFilename)
await promises.mkdir(baseDir, { recursive: true }) await promises.mkdir(baseDir, { recursive: true })
let html let renderResult
let curRenderOpts: RenderOpts = {} let curRenderOpts: RenderOpts = {}
let renderMethod = renderToHTML let renderMethod = renderToHTML
let inAmpMode = false, let inAmpMode = false,
...@@ -266,7 +267,7 @@ export default async function exportPage({ ...@@ -266,7 +267,7 @@ export default async function exportPage({
// if it was auto-exported the HTML is loaded here // if it was auto-exported the HTML is loaded here
if (typeof mod === 'string') { if (typeof mod === 'string') {
html = mod renderResult = resultFromChunks([mod])
queryWithAutoExportWarn() queryWithAutoExportWarn()
} else { } else {
// for non-dynamic SSG pages we should have already // for non-dynamic SSG pages we should have already
...@@ -308,10 +309,10 @@ export default async function exportPage({ ...@@ -308,10 +309,10 @@ export default async function exportPage({
params params
) )
curRenderOpts = (result as any).renderOpts || {} curRenderOpts = (result as any).renderOpts || {}
html = (result as any).html renderResult = (result as any).html
} }
if (!html && !(curRenderOpts as any).isNotFound) { if (!renderResult && !(curRenderOpts as any).isNotFound) {
throw new Error(`Failed to render serverless page`) throw new Error(`Failed to render serverless page`)
} }
} else { } else {
...@@ -344,7 +345,7 @@ export default async function exportPage({ ...@@ -344,7 +345,7 @@ export default async function exportPage({
} }
if (typeof components.Component === 'string') { if (typeof components.Component === 'string') {
html = components.Component renderResult = resultFromChunks([components.Component])
queryWithAutoExportWarn() queryWithAutoExportWarn()
} else { } else {
/** /**
...@@ -376,8 +377,14 @@ export default async function exportPage({ ...@@ -376,8 +377,14 @@ export default async function exportPage({
: null, : null,
locale: locale as string, locale: locale as string,
} }
// @ts-ignore renderResult = await renderMethod(
html = await renderMethod(req, res, page, query, curRenderOpts) req,
res,
page,
query,
// @ts-ignore
curRenderOpts
)
} }
} }
results.ssgNotFound = (curRenderOpts as any).isNotFound results.ssgNotFound = (curRenderOpts as any).isNotFound
...@@ -403,6 +410,8 @@ export default async function exportPage({ ...@@ -403,6 +410,8 @@ export default async function exportPage({
} }
} }
const htmlChunks = renderResult ? await resultToChunks(renderResult) : []
const html = htmlChunks.join('')
if (inAmpMode && !curRenderOpts.ampSkipValidation) { if (inAmpMode && !curRenderOpts.ampSkipValidation) {
if (!results.ssgNotFound) { if (!results.ssgNotFound) {
await validateAmp(html, path, curRenderOpts.ampValidatorPath) await validateAmp(html, path, curRenderOpts.ampValidatorPath)
...@@ -420,11 +429,11 @@ export default async function exportPage({ ...@@ -420,11 +429,11 @@ export default async function exportPage({
await promises.access(ampHtmlFilepath) await promises.access(ampHtmlFilepath)
} catch (_) { } catch (_) {
// make sure it doesn't exist from manual mapping // make sure it doesn't exist from manual mapping
let ampHtml let ampRenderResult
if (serverless) { if (serverless) {
req.url += (req.url!.includes('?') ? '&' : '?') + 'amp=1' req.url += (req.url!.includes('?') ? '&' : '?') + 'amp=1'
// @ts-ignore // @ts-ignore
ampHtml = ( ampRenderResult = (
await (renderMethod as any)( await (renderMethod as any)(
req, req,
res, res,
...@@ -434,7 +443,7 @@ export default async function exportPage({ ...@@ -434,7 +443,7 @@ export default async function exportPage({
) )
).html ).html
} else { } else {
ampHtml = await renderMethod( ampRenderResult = await renderMethod(
req, req,
res, res,
page, page,
...@@ -444,6 +453,8 @@ export default async function exportPage({ ...@@ -444,6 +453,8 @@ export default async function exportPage({
) )
} }
const ampChunks = await resultToChunks(ampRenderResult)
const ampHtml = ampChunks.join('')
if (!curRenderOpts.ampSkipValidation) { if (!curRenderOpts.ampSkipValidation) {
await validateAmp(ampHtml, page + '?amp=1') await validateAmp(ampHtml, page + '?amp=1')
} }
......
...@@ -71,11 +71,16 @@ import Router, { ...@@ -71,11 +71,16 @@ import Router, {
import prepareDestination, { import prepareDestination, {
compileNonPath, compileNonPath,
} from '../shared/lib/router/utils/prepare-destination' } from '../shared/lib/router/utils/prepare-destination'
import { sendPayload, setRevalidateHeaders } from './send-payload' import { sendRenderResult, setRevalidateHeaders } from './send-payload'
import { serveStatic } from './serve-static' import { serveStatic } from './serve-static'
import { IncrementalCache } from './incremental-cache' import { IncrementalCache } from './incremental-cache'
import { execOnce } from '../shared/lib/utils' import { execOnce } from '../shared/lib/utils'
import { isBlockedPage } from './utils' import {
isBlockedPage,
RenderResult,
resultFromChunks,
resultToChunks,
} from './utils'
import { loadEnvConfig } from '@next/env' import { loadEnvConfig } from '@next/env'
import './node-polyfill-fetch' import './node-polyfill-fetch'
import { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin' import { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
...@@ -1232,17 +1237,17 @@ export default class Server { ...@@ -1232,17 +1237,17 @@ export default class Server {
// In dev, we should not cache pages for any reason. // In dev, we should not cache pages for any reason.
res.setHeader('Cache-Control', 'no-store, must-revalidate') res.setHeader('Cache-Control', 'no-store, must-revalidate')
} }
return sendPayload( return sendRenderResult({
req, req,
res, res,
body, resultOrPayload: requireStaticHTML
? (await resultToChunks(body)).join('')
: body,
type, type,
{ generateEtags,
generateEtags, poweredByHeader,
poweredByHeader, options: revalidateOptions,
}, })
revalidateOptions
)
} }
} }
...@@ -1262,7 +1267,11 @@ export default class Server { ...@@ -1262,7 +1267,11 @@ export default class Server {
requireStaticHTML: true, requireStaticHTML: true,
}, },
}) })
return payload ? payload.body : null if (payload === null) {
return null
}
const chunks = await resultToChunks(payload.body)
return chunks.join('')
} }
public async render( public async render(
...@@ -1438,7 +1447,8 @@ export default class Server { ...@@ -1438,7 +1447,8 @@ export default class Server {
if (typeof components.Component === 'string') { if (typeof components.Component === 'string') {
return { return {
type: 'html', type: 'html',
body: components.Component, // TODO: Static pages should be written as chunks
body: resultFromChunks([components.Component]),
} }
} }
...@@ -1564,7 +1574,7 @@ export default class Server { ...@@ -1564,7 +1574,7 @@ export default class Server {
const doRender: () => Promise<ResponseCacheEntry | null> = async () => { const doRender: () => Promise<ResponseCacheEntry | null> = async () => {
let pageData: any let pageData: any
let html: string | null let body: RenderResult | null
let sprRevalidate: number | false let sprRevalidate: number | false
let isNotFound: boolean | undefined let isNotFound: boolean | undefined
let isRedirect: boolean | undefined let isRedirect: boolean | undefined
...@@ -1586,7 +1596,7 @@ export default class Server { ...@@ -1586,7 +1596,7 @@ export default class Server {
} }
) )
html = renderResult.html body = renderResult.html
pageData = renderResult.renderOpts.pageData pageData = renderResult.renderOpts.pageData
sprRevalidate = renderResult.renderOpts.revalidate sprRevalidate = renderResult.renderOpts.revalidate
isNotFound = renderResult.renderOpts.isNotFound isNotFound = renderResult.renderOpts.isNotFound
...@@ -1632,7 +1642,7 @@ export default class Server { ...@@ -1632,7 +1642,7 @@ export default class Server {
renderOpts renderOpts
) )
html = renderResult body = renderResult
// TODO: change this to a different passing mechanism // TODO: change this to a different passing mechanism
pageData = (renderOpts as any).pageData pageData = (renderOpts as any).pageData
sprRevalidate = (renderOpts as any).revalidate sprRevalidate = (renderOpts as any).revalidate
...@@ -1646,10 +1656,10 @@ export default class Server { ...@@ -1646,10 +1656,10 @@ export default class Server {
} else if (isRedirect) { } else if (isRedirect) {
value = { kind: 'REDIRECT', props: pageData } value = { kind: 'REDIRECT', props: pageData }
} else { } else {
if (!html) { if (!body) {
return null return null
} }
value = { kind: 'PAGE', html, pageData } value = { kind: 'PAGE', html: body, pageData }
} }
return { revalidate: sprRevalidate, value } return { revalidate: sprRevalidate, value }
} }
...@@ -1716,7 +1726,7 @@ export default class Server { ...@@ -1716,7 +1726,7 @@ export default class Server {
return { return {
value: { value: {
kind: 'PAGE', kind: 'PAGE',
html, html: resultFromChunks([html]),
pageData: {}, pageData: {},
}, },
} }
...@@ -1795,7 +1805,7 @@ export default class Server { ...@@ -1795,7 +1805,7 @@ export default class Server {
if (isDataReq) { if (isDataReq) {
return { return {
type: 'json', type: 'json',
body: JSON.stringify(cachedData.props), body: resultFromChunks([JSON.stringify(cachedData.props)]),
revalidateOptions, revalidateOptions,
} }
} else { } else {
...@@ -1805,7 +1815,9 @@ export default class Server { ...@@ -1805,7 +1815,9 @@ export default class Server {
} else { } else {
return { return {
type: isDataReq ? 'json' : 'html', type: isDataReq ? 'json' : 'html',
body: isDataReq ? JSON.stringify(cachedData.pageData) : cachedData.html, body: isDataReq
? resultFromChunks([JSON.stringify(cachedData.pageData)])
: cachedData.html,
revalidateOptions, revalidateOptions,
} }
} }
...@@ -2039,7 +2051,7 @@ export default class Server { ...@@ -2039,7 +2051,7 @@ export default class Server {
} }
return { return {
type: 'html', type: 'html',
body: 'Internal Server Error', body: resultFromChunks(['Internal Server Error']),
} }
} }
} }
...@@ -2239,6 +2251,6 @@ export class WrappedBuildError extends Error { ...@@ -2239,6 +2251,6 @@ export class WrappedBuildError extends Error {
type ResponsePayload = { type ResponsePayload = {
type: 'html' | 'json' type: 'html' | 'json'
body: string body: RenderResult
revalidateOptions?: any revalidateOptions?: any
} }
...@@ -59,7 +59,8 @@ import { ...@@ -59,7 +59,8 @@ import {
getRedirectStatus, getRedirectStatus,
Redirect, Redirect,
} from '../lib/load-custom-routes' } from '../lib/load-custom-routes'
import type { DomainLocale } from './config' import { DomainLocale } from './config'
import { RenderResult, resultFromChunks } from './utils'
function noRouter() { function noRouter() {
const message = const message =
...@@ -376,7 +377,7 @@ export async function renderToHTML( ...@@ -376,7 +377,7 @@ export async function renderToHTML(
pathname: string, pathname: string,
query: ParsedUrlQuery, query: ParsedUrlQuery,
renderOpts: RenderOpts renderOpts: RenderOpts
): Promise<string | null> { ): Promise<RenderResult | null> {
// In dev we invalidate the cache by appending a timestamp to the resource URL. // In dev we invalidate the cache by appending a timestamp to the resource URL.
// This is a workaround to fix https://github.com/vercel/next.js/issues/5860 // This is a workaround to fix https://github.com/vercel/next.js/issues/5860
// TODO: remove this workaround when https://bugs.webkit.org/show_bug.cgi?id=187726 is fixed. // TODO: remove this workaround when https://bugs.webkit.org/show_bug.cgi?id=187726 is fixed.
...@@ -951,7 +952,7 @@ export async function renderToHTML( ...@@ -951,7 +952,7 @@ export async function renderToHTML(
// Avoid rendering page un-necessarily for getServerSideProps data request // Avoid rendering page un-necessarily for getServerSideProps data request
// and getServerSideProps/getStaticProps redirects // and getServerSideProps/getStaticProps redirects
if ((isDataReq && !isSSG) || (renderOpts as any).isRedirect) { if ((isDataReq && !isSSG) || (renderOpts as any).isRedirect) {
return props return resultFromChunks([JSON.stringify(props)])
} }
// We don't call getStaticProps or getServerSideProps while generating // We don't call getStaticProps or getServerSideProps while generating
...@@ -1160,7 +1161,7 @@ export async function renderToHTML( ...@@ -1160,7 +1161,7 @@ export async function renderToHTML(
html = html.replace(/&amp;amp=1/g, '&amp=1') html = html.replace(/&amp;amp=1/g, '&amp=1')
} }
return html return resultFromChunks([html])
} }
function errorToJSON(err: Error): Error { function errorToJSON(err: Error): Error {
......
import { IncrementalCache } from './incremental-cache' import { IncrementalCache } from './incremental-cache'
import { RenderResult, resultFromChunks, resultToChunks } from './utils'
interface CachedRedirectValue { interface CachedRedirectValue {
kind: 'REDIRECT' kind: 'REDIRECT'
...@@ -7,7 +8,7 @@ interface CachedRedirectValue { ...@@ -7,7 +8,7 @@ interface CachedRedirectValue {
interface CachedPageValue { interface CachedPageValue {
kind: 'PAGE' kind: 'PAGE'
html: string html: RenderResult
pageData: Object pageData: Object
} }
...@@ -73,7 +74,14 @@ export default class ResponseCache { ...@@ -73,7 +74,14 @@ export default class ResponseCache {
if (cachedResponse) { if (cachedResponse) {
resolve({ resolve({
revalidate: cachedResponse.curRevalidate, revalidate: cachedResponse.curRevalidate,
value: cachedResponse.value, value:
cachedResponse.value?.kind === 'PAGE'
? {
kind: 'PAGE',
html: resultFromChunks([cachedResponse.value.html]),
pageData: cachedResponse.value.pageData,
}
: cachedResponse.value,
}) })
if (!cachedResponse.isStale) { if (!cachedResponse.isStale) {
// The cached value is still valid, so we don't need // The cached value is still valid, so we don't need
...@@ -88,7 +96,13 @@ export default class ResponseCache { ...@@ -88,7 +96,13 @@ export default class ResponseCache {
if (key && cacheEntry && typeof cacheEntry.revalidate !== 'undefined') { if (key && cacheEntry && typeof cacheEntry.revalidate !== 'undefined') {
await this.incrementalCache.set( await this.incrementalCache.set(
key, key,
cacheEntry.value, cacheEntry.value?.kind === 'PAGE'
? {
kind: 'PAGE',
html: (await resultToChunks(cacheEntry.value.html)).join(''),
pageData: cacheEntry.value.pageData,
}
: cacheEntry.value,
cacheEntry.revalidate cacheEntry.revalidate
) )
} }
......
...@@ -2,6 +2,7 @@ import { IncomingMessage, ServerResponse } from 'http' ...@@ -2,6 +2,7 @@ import { IncomingMessage, ServerResponse } from 'http'
import { isResSent } from '../shared/lib/utils' import { isResSent } from '../shared/lib/utils'
import generateETag from 'etag' import generateETag from 'etag'
import fresh from 'next/dist/compiled/fresh' import fresh from 'next/dist/compiled/fresh'
import { RenderResult } from './utils'
export type PayloadOptions = export type PayloadOptions =
| { private: true } | { private: true }
...@@ -46,6 +47,34 @@ export function sendPayload( ...@@ -46,6 +47,34 @@ export function sendPayload(
}: { generateEtags: boolean; poweredByHeader: boolean }, }: { generateEtags: boolean; poweredByHeader: boolean },
options?: PayloadOptions options?: PayloadOptions
): void { ): void {
sendRenderResult({
req,
res,
resultOrPayload: payload,
type,
generateEtags,
poweredByHeader,
options,
})
}
export function sendRenderResult({
req,
res,
resultOrPayload,
type,
generateEtags,
poweredByHeader,
options,
}: {
req: IncomingMessage
res: ServerResponse
resultOrPayload: RenderResult | string
type: 'html' | 'json'
generateEtags: boolean
poweredByHeader: boolean
options?: PayloadOptions
}): void {
if (isResSent(res)) { if (isResSent(res)) {
return return
} }
...@@ -54,9 +83,15 @@ export function sendPayload( ...@@ -54,9 +83,15 @@ export function sendPayload(
res.setHeader('X-Powered-By', 'Next.js') res.setHeader('X-Powered-By', 'Next.js')
} }
const etag = generateEtags ? generateETag(payload) : undefined const isPayload = typeof resultOrPayload === 'string'
if (sendEtagResponse(req, res, etag)) {
return if (isPayload) {
const etag = generateEtags
? generateETag(resultOrPayload as string)
: undefined
if (sendEtagResponse(req, res, etag)) {
return
}
} }
if (!res.getHeader('Content-Type')) { if (!res.getHeader('Content-Type')) {
...@@ -65,11 +100,29 @@ export function sendPayload( ...@@ -65,11 +100,29 @@ export function sendPayload(
type === 'json' ? 'application/json' : 'text/html; charset=utf-8' type === 'json' ? 'application/json' : 'text/html; charset=utf-8'
) )
} }
res.setHeader('Content-Length', Buffer.byteLength(payload))
if (isPayload) {
res.setHeader(
'Content-Length',
Buffer.byteLength(resultOrPayload as string)
)
}
if (options != null) { if (options != null) {
setRevalidateHeaders(res, options) setRevalidateHeaders(res, options)
} }
res.end(req.method === 'HEAD' ? null : payload)
if (req.method === 'HEAD') {
res.end(null)
} else if (isPayload) {
res.end(resultOrPayload as string)
} else {
;(resultOrPayload as RenderResult)({
next: (chunk) => res.write(chunk),
error: (_) => res.end(),
complete: () => res.end(),
})
}
} }
export function sendEtagResponse( export function sendEtagResponse(
......
...@@ -14,3 +14,32 @@ export function cleanAmpPath(pathname: string): string { ...@@ -14,3 +14,32 @@ export function cleanAmpPath(pathname: string): string {
pathname = pathname.replace(/\?$/, '') pathname = pathname.replace(/\?$/, '')
return pathname return pathname
} }
export type Disposable = () => void
// TODO: Consider just using an actual Observable here
export type RenderResult = (observer: {
next(chunk: string): void
error(error: Error): void
complete(): void
}) => Disposable
export function resultFromChunks(chunks: string[]): RenderResult {
return ({ next, complete }) => {
chunks.forEach(next)
complete()
return () => {}
}
}
export function resultToChunks(result: RenderResult): Promise<string[]> {
return new Promise((resolve, reject) => {
const chunks: string[] = []
result({
next: (chunk) => {
chunks.push(chunk)
},
error: (error) => reject(error),
complete: () => resolve(chunks),
})
})
}
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