mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
d177272802
We want to make sure that we can block the reveal of a well designed complete shell reliably. In the Suspense model, client transitions don't have any way to implicitly resolve. This means you need to use Suspense or SuspenseList to explicitly split the document. Relying on implicit would mean you can't add a Suspense boundary later where needed. So we highly encourage the use of them around large content. However, if you have constructed a too large shell (e.g. by not adding any Suspense boundaries at all) then that might take too long to render on the client. We shouldn't punish users (or overzealous metrics tracking tools like search engines) in that scenario. This opts out of render blocking if the shell ends up too large to be intentional and too slow to load. Instead it deopts to showing the content split up in arbitrary ways (browser default). It only does this for SSR, and not client navs so it's not reliable. In fact, we issue an error to `onError`. This error is recoverable in that the document is still produced. It's up to your framework to decide if this errors the build or just surface it for action later. What should be the limit though? There's a trade off here. If this limit is too low then you can't fit a reasonably well built UI within it without getting errors. If it's too high then things that accidentally fall below it might take too long to load. I came up with 512kB of uncompressed shell HTML. See the comment in code for the rationale for this number. TL;DR: Data and theory indicates that having this much content inside `rel="expect"` doesn't meaningfully change metrics. Research of above-the-fold content on various websites indicate that this can comfortable fit all of them which should be enough for any intentional initial paint.
247 lines
6.2 KiB
JavaScript
247 lines
6.2 KiB
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow
|
|
*/
|
|
|
|
import type {ReactNodeList} from 'shared/ReactTypes';
|
|
|
|
import type {
|
|
RenderState,
|
|
ResumableState,
|
|
PreambleState,
|
|
HoistableState,
|
|
FormatContext,
|
|
} from 'react-dom-bindings/src/server/ReactFizzConfigDOM';
|
|
|
|
import {
|
|
pushStartInstance as pushStartInstanceImpl,
|
|
writePreambleStart as writePreambleStartImpl,
|
|
} from 'react-dom-bindings/src/server/ReactFizzConfigDOM';
|
|
|
|
import type {
|
|
Destination,
|
|
Chunk,
|
|
PrecomputedChunk,
|
|
} from 'react-server/src/ReactServerStreamConfig';
|
|
|
|
import type {FormStatus} from 'react-dom-bindings/src/shared/ReactDOMFormActions';
|
|
|
|
import {NotPending} from 'react-dom-bindings/src/shared/ReactDOMFormActions';
|
|
|
|
import hasOwnProperty from 'shared/hasOwnProperty';
|
|
|
|
// Allow embedding inside another Fizz render.
|
|
export const isPrimaryRenderer = false;
|
|
|
|
// Disable Client Hooks
|
|
export const supportsClientAPIs = false;
|
|
|
|
import {stringToChunk} from 'react-server/src/ReactServerStreamConfig';
|
|
|
|
export type {
|
|
RenderState,
|
|
ResumableState,
|
|
HoistableState,
|
|
PreambleState,
|
|
FormatContext,
|
|
} from 'react-dom-bindings/src/server/ReactFizzConfigDOM';
|
|
|
|
export {
|
|
getChildFormatContext,
|
|
getSuspenseFallbackFormatContext,
|
|
getSuspenseContentFormatContext,
|
|
makeId,
|
|
pushEndInstance,
|
|
pushFormStateMarkerIsMatching,
|
|
pushFormStateMarkerIsNotMatching,
|
|
writeStartSegment,
|
|
writeEndSegment,
|
|
writeCompletedSegmentInstruction,
|
|
writeCompletedBoundaryInstruction,
|
|
writeClientRenderBoundaryInstruction,
|
|
writeStartPendingSuspenseBoundary,
|
|
writeEndPendingSuspenseBoundary,
|
|
writeHoistablesForBoundary,
|
|
writePlaceholder,
|
|
createRootFormatContext,
|
|
createRenderState,
|
|
createResumableState,
|
|
createPreambleState,
|
|
createHoistableState,
|
|
writePreambleEnd,
|
|
writeHoistables,
|
|
writePostamble,
|
|
hoistHoistables,
|
|
resetResumableState,
|
|
completeResumableState,
|
|
emitEarlyPreloads,
|
|
doctypeChunk,
|
|
canHavePreamble,
|
|
hoistPreambleState,
|
|
isPreambleReady,
|
|
isPreambleContext,
|
|
} from 'react-dom-bindings/src/server/ReactFizzConfigDOM';
|
|
|
|
import escapeTextForBrowser from 'react-dom-bindings/src/server/escapeTextForBrowser';
|
|
|
|
export function getViewTransitionFormatContext(
|
|
resumableState: ResumableState,
|
|
parentContext: FormatContext,
|
|
update: void | null | 'none' | 'auto' | string,
|
|
enter: void | null | 'none' | 'auto' | string,
|
|
exit: void | null | 'none' | 'auto' | string,
|
|
share: void | null | 'none' | 'auto' | string,
|
|
name: void | null | 'auto' | string,
|
|
autoName: string, // name or an autogenerated unique name
|
|
): FormatContext {
|
|
// ViewTransition reveals are not supported in markup renders.
|
|
return parentContext;
|
|
}
|
|
|
|
export function pushStartInstance(
|
|
target: Array<Chunk | PrecomputedChunk>,
|
|
type: string,
|
|
props: Object,
|
|
resumableState: ResumableState,
|
|
renderState: RenderState,
|
|
preambleState: null | PreambleState,
|
|
hoistableState: null | HoistableState,
|
|
formatContext: FormatContext,
|
|
textEmbedded: boolean,
|
|
): ReactNodeList {
|
|
for (const propKey in props) {
|
|
if (hasOwnProperty.call(props, propKey)) {
|
|
const propValue = props[propKey];
|
|
if (propKey === 'ref' && propValue != null) {
|
|
throw new Error(
|
|
'Cannot pass ref in renderToHTML because they will never be hydrated.',
|
|
);
|
|
}
|
|
if (typeof propValue === 'function') {
|
|
throw new Error(
|
|
'Cannot pass event handlers (' +
|
|
propKey +
|
|
') in renderToHTML because ' +
|
|
'the HTML will never be hydrated so they can never get called.',
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
return pushStartInstanceImpl(
|
|
target,
|
|
type,
|
|
props,
|
|
resumableState,
|
|
renderState,
|
|
preambleState,
|
|
hoistableState,
|
|
formatContext,
|
|
textEmbedded,
|
|
);
|
|
}
|
|
|
|
export function pushTextInstance(
|
|
target: Array<Chunk | PrecomputedChunk>,
|
|
text: string,
|
|
renderState: RenderState,
|
|
textEmbedded: boolean,
|
|
): boolean {
|
|
// Markup doesn't need any termination.
|
|
target.push(stringToChunk(escapeTextForBrowser(text)));
|
|
return false;
|
|
}
|
|
|
|
export function pushSegmentFinale(
|
|
target: Array<Chunk | PrecomputedChunk>,
|
|
renderState: RenderState,
|
|
lastPushedText: boolean,
|
|
textEmbedded: boolean,
|
|
): void {
|
|
// Markup doesn't need any termination.
|
|
return;
|
|
}
|
|
|
|
export function pushStartActivityBoundary(
|
|
target: Array<Chunk | PrecomputedChunk>,
|
|
renderState: RenderState,
|
|
): void {
|
|
// Markup doesn't have any instructions.
|
|
return;
|
|
}
|
|
|
|
export function pushEndActivityBoundary(
|
|
target: Array<Chunk | PrecomputedChunk>,
|
|
renderState: RenderState,
|
|
): void {
|
|
// Markup doesn't have any instructions.
|
|
return;
|
|
}
|
|
|
|
export function writeStartCompletedSuspenseBoundary(
|
|
destination: Destination,
|
|
renderState: RenderState,
|
|
): boolean {
|
|
// Markup doesn't have any instructions.
|
|
return true;
|
|
}
|
|
|
|
export function writeStartClientRenderedSuspenseBoundary(
|
|
destination: Destination,
|
|
renderState: RenderState,
|
|
// flushing these error arguments are not currently supported in this legacy streaming format.
|
|
errorDigest: ?string,
|
|
errorMessage: ?string,
|
|
errorStack: ?string,
|
|
errorComponentStack: ?string,
|
|
): boolean {
|
|
// Markup doesn't have any instructions.
|
|
return true;
|
|
}
|
|
|
|
export function writeEndCompletedSuspenseBoundary(
|
|
destination: Destination,
|
|
renderState: RenderState,
|
|
): boolean {
|
|
// Markup doesn't have any instructions.
|
|
return true;
|
|
}
|
|
export function writeEndClientRenderedSuspenseBoundary(
|
|
destination: Destination,
|
|
renderState: RenderState,
|
|
): boolean {
|
|
// Markup doesn't have any instructions.
|
|
return true;
|
|
}
|
|
|
|
export function writePreambleStart(
|
|
destination: Destination,
|
|
resumableState: ResumableState,
|
|
renderState: RenderState,
|
|
skipBlockingShell: boolean,
|
|
): void {
|
|
return writePreambleStartImpl(
|
|
destination,
|
|
resumableState,
|
|
renderState,
|
|
true, // skipBlockingShell
|
|
);
|
|
}
|
|
|
|
export function writeCompletedRoot(
|
|
destination: Destination,
|
|
resumableState: ResumableState,
|
|
renderState: RenderState,
|
|
isComplete: boolean,
|
|
): boolean {
|
|
// Markup doesn't have any bootstrap scripts nor shell completions.
|
|
return true;
|
|
}
|
|
|
|
export type TransitionStatus = FormStatus;
|
|
export const NotPendingTransition: TransitionStatus = NotPending;
|