Unverified Commit eab3db20 authored by Gerald Monaco's avatar Gerald Monaco Committed by GitHub

Simplify static + dynamic HTML generation (#28793)

parent f9987b09
...@@ -116,7 +116,7 @@ export function getPageHandler(ctx: ServerlessHandlerCtx) { ...@@ -116,7 +116,7 @@ export function getPageHandler(ctx: ServerlessHandlerCtx) {
previewProps: encodedPreviewProps, previewProps: encodedPreviewProps,
env: process.env, env: process.env,
basePath, basePath,
requireStaticHTML: true, // Serverless target doesn't support streaming supportsDynamicHTML: false, // Serverless target doesn't support streaming
..._renderOpts, ..._renderOpts,
} }
let _nextData = false let _nextData = false
......
...@@ -378,8 +378,8 @@ export default async function exportApp( ...@@ -378,8 +378,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 // Exported pages do not currently support dynamic HTML.
requireStaticHTML: true, supportsDynamicHTML: false,
concurrentFeatures: nextConfig.experimental.concurrentFeatures, concurrentFeatures: nextConfig.experimental.concurrentFeatures,
} }
......
...@@ -1250,13 +1250,12 @@ export default class Server { ...@@ -1250,13 +1250,12 @@ export default class Server {
query: ParsedUrlQuery query: ParsedUrlQuery
} }
): Promise<void> { ): Promise<void> {
// TODO: Determine when dynamic HTML is allowed
const requireStaticHTML = true
const ctx = { const ctx = {
...partialContext, ...partialContext,
renderOpts: { renderOpts: {
...this.renderOpts, ...this.renderOpts,
requireStaticHTML, // TODO: Determine when dynamic HTML is allowed
supportsDynamicHTML: false,
}, },
} as const } as const
const payload = await fn(ctx) const payload = await fn(ctx)
...@@ -1296,17 +1295,12 @@ export default class Server { ...@@ -1296,17 +1295,12 @@ export default class Server {
...partialContext, ...partialContext,
renderOpts: { renderOpts: {
...this.renderOpts, ...this.renderOpts,
requireStaticHTML: true, supportsDynamicHTML: false,
}, },
}) })
if (payload === null) { if (payload === null) {
return null return null
} }
if (payload.body.isDynamic()) {
throw new Error(
'invariant: expected a static result. This is a bug in Next.js'
)
}
return payload.body.toUnchunkedString() return payload.body.toUnchunkedString()
} }
...@@ -1490,6 +1484,17 @@ export default class Server { ...@@ -1490,6 +1484,17 @@ export default class Server {
delete query.amp delete query.amp
} }
if (opts.supportsDynamicHTML === true) {
// Disable dynamic HTML in cases that we know it won't be generated,
// so that we can continue generating a cache key when possible.
opts.supportsDynamicHTML =
!isSSG &&
!isLikeServerless &&
!query.amp &&
!this.minimalMode &&
typeof components.Document.getInitialProps !== 'function'
}
const locale = query.__nextLocale as string const locale = query.__nextLocale as string
const defaultLocale = isSSG const defaultLocale = isSSG
? this.nextConfig.i18n?.defaultLocale ? this.nextConfig.i18n?.defaultLocale
...@@ -1574,7 +1579,7 @@ export default class Server { ...@@ -1574,7 +1579,7 @@ export default class Server {
} }
let ssgCacheKey = let ssgCacheKey =
isPreviewMode || !isSSG || this.minimalMode isPreviewMode || !isSSG || this.minimalMode || opts.supportsDynamicHTML
? null // Preview mode bypasses the cache ? null // Preview mode bypasses the cache
: `${locale ? `/${locale}` : ''}${ : `${locale ? `/${locale}` : ''}${
(pathname === '/' || resolvedUrlPathname === '/') && locale (pathname === '/' || resolvedUrlPathname === '/') && locale
......
...@@ -194,7 +194,7 @@ export type RenderOptsPartial = { ...@@ -194,7 +194,7 @@ export type RenderOptsPartial = {
defaultLocale?: string defaultLocale?: string
domainLocales?: DomainLocale[] domainLocales?: DomainLocale[]
disableOptimizedLoading?: boolean disableOptimizedLoading?: boolean
requireStaticHTML?: boolean supportsDynamicHTML?: boolean
concurrentFeatures?: boolean concurrentFeatures?: boolean
customServer?: boolean customServer?: boolean
} }
...@@ -295,7 +295,7 @@ export async function renderToHTML( ...@@ -295,7 +295,7 @@ export async function renderToHTML(
previewProps, previewProps,
basePath, basePath,
devOnlyCacheBusterQueryString, devOnlyCacheBusterQueryString,
requireStaticHTML, supportsDynamicHTML,
concurrentFeatures, concurrentFeatures,
} = renderOpts } = renderOpts
...@@ -883,7 +883,20 @@ export async function renderToHTML( ...@@ -883,7 +883,20 @@ export async function renderToHTML(
} }
} }
const generateStaticHTML = requireStaticHTML || inAmpMode /**
* Rules of Static & Dynamic HTML:
*
* 1.) We must generate static HTML unless the caller explicitly opts
* in to dynamic HTML support.
*
* 2.) If dynamic HTML support is requested, we must honor that request
* or throw an error. It is the sole responsibility of the caller to
* ensure they aren't e.g. requesting dynamic HTML for an AMP page.
*
* These rules help ensure that other existing features like request caching,
* coalescing, and ISR continue working as intended.
*/
const generateStaticHTML = supportsDynamicHTML !== true
const renderToStream = (element: React.ReactElement) => const renderToStream = (element: React.ReactElement) =>
new Promise<Observable<string>>((resolve, reject) => { new Promise<Observable<string>>((resolve, reject) => {
const stream = new PassThrough() const stream = new PassThrough()
......
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