Buckets:
| /* eslint-disable @next/internal/no-ambiguous-jsx -- whole module is used in React Client */ | |
| import React, { type JSX } from 'react' | |
| import { isHTTPAccessFallbackError } from '../../client/components/http-access-fallback/http-access-fallback' | |
| import { | |
| getURLFromRedirectError, | |
| getRedirectStatusCodeFromError, | |
| } from '../../client/components/redirect' | |
| import { isRedirectError } from '../../client/components/redirect-error' | |
| import { renderToReadableStream } from 'react-dom/server' | |
| import { streamToString } from '../stream-utils/node-web-streams-helper' | |
| import { RedirectStatusCode } from '../../client/components/redirect-status-code' | |
| import { addPathPrefix } from '../../shared/lib/router/utils/add-path-prefix' | |
| import type { ClientTraceDataEntry } from '../lib/trace/tracer' | |
| export function makeGetServerInsertedHTML({ | |
| polyfills, | |
| renderServerInsertedHTML, | |
| serverCapturedErrors, | |
| tracingMetadata, | |
| basePath, | |
| }: { | |
| polyfills: JSX.IntrinsicElements['script'][] | |
| renderServerInsertedHTML: () => React.ReactNode | |
| tracingMetadata: ClientTraceDataEntry[] | undefined | |
| serverCapturedErrors: Array<unknown> | |
| basePath: string | |
| }) { | |
| let flushedErrorMetaTagsUntilIndex = 0 | |
| // These only need to be rendered once, they'll be set to empty arrays once flushed. | |
| let polyfillTags = polyfills.map((polyfill) => { | |
| return <script key={polyfill.src} {...polyfill} /> | |
| }) | |
| let traceMetaTags = (tracingMetadata || []).map(({ key, value }, index) => ( | |
| )) | |
| return async function getServerInsertedHTML() { | |
| // Loop through all the errors that have been captured but not yet | |
| // flushed. | |
| const errorMetaTags = [] | |
| while (flushedErrorMetaTagsUntilIndex < serverCapturedErrors.length) { | |
| const error = serverCapturedErrors[flushedErrorMetaTagsUntilIndex] | |
| flushedErrorMetaTagsUntilIndex++ | |
| if (isHTTPAccessFallbackError(error)) { | |
| errorMetaTags.push( | |
| , | |
| process.env.NODE_ENV === 'development' ? ( | |
| ) : null | |
| ) | |
| } else if (isRedirectError(error)) { | |
| const redirectUrl = addPathPrefix( | |
| getURLFromRedirectError(error), | |
| basePath | |
| ) | |
| const statusCode = getRedirectStatusCodeFromError(error) | |
| const isPermanent = | |
| statusCode === RedirectStatusCode.PermanentRedirect ? true : false | |
| if (redirectUrl) { | |
| errorMetaTags.push( | |
| ) | |
| } | |
| } | |
| } | |
| const serverInsertedHTML = renderServerInsertedHTML() | |
| // Skip React rendering if we know the content is empty. | |
| if ( | |
| polyfillTags.length === 0 && | |
| traceMetaTags.length === 0 && | |
| errorMetaTags.length === 0 && | |
| Array.isArray(serverInsertedHTML) && | |
| serverInsertedHTML.length === 0 | |
| ) { | |
| return '' | |
| } | |
| // TODO: This should use Node streams when __NEXT_USE_NODE_STREAMS is true | |
| const stream = await renderToReadableStream( | |
| , | |
| { | |
| // Larger chunk because this isn't sent over the network. | |
| // Let's set it to 1MB. | |
| progressiveChunkSize: 1024 * 1024, | |
| } | |
| ) | |
| // The polyfills and trace metadata have been flushed, so they don't need to be rendered again | |
| polyfillTags = [] | |
| traceMetaTags = [] | |
| // There's no need to wait for the stream to be ready | |
| // e.g. calling `await stream.allReady` because `streamToString` will | |
| // wait and decode the stream progressively with better parallelism. | |
| return streamToString(stream) | |
| } | |
| } | |
Xet Storage Details
- Size:
- 3.98 kB
- Xet hash:
- 8c2973f263d515704f13f074a4b8145e090e3e183d3d8b5d3980e6b2e1bb68ec
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.