///
import React, { type JSX } from 'react'
import { NEXT_BUILTIN_DOCUMENT } from '../shared/lib/constants'
import type {
DocumentContext,
DocumentInitialProps,
DocumentProps,
DocumentType,
NEXT_DATA,
} from '../shared/lib/utils'
import type { ScriptProps } from '../client/script'
import type { NextFontManifest } from '../build/webpack/plugins/next-font-manifest-plugin'
import { getPageFiles } from '../server/get-page-files'
import type { BuildManifest } from '../server/get-page-files'
import { htmlEscapeJsonString } from '../server/htmlescape'
import isError from '../lib/is-error'
import {
HtmlContext,
useHtmlContext,
} from '../shared/lib/html-context.shared-runtime'
import type { HtmlProps } from '../shared/lib/html-context.shared-runtime'
import { encodeURIPath } from '../shared/lib/encode-uri-path'
import type { DeepReadonly } from '../shared/lib/deep-readonly'
import { getTracer } from '../server/lib/trace/tracer'
import { getTracedMetadata } from '../server/lib/trace/utils'
export type { DocumentContext, DocumentInitialProps, DocumentProps }
export type OriginProps = {
nonce?: string
crossOrigin?: 'anonymous' | 'use-credentials' | '' | undefined
children?: React.ReactNode
}
type DocumentFiles = {
sharedFiles: readonly string[]
pageFiles: readonly string[]
allFiles: readonly string[]
}
type HeadHTMLProps = React.DetailedHTMLProps<
React.HTMLAttributes,
HTMLHeadElement
>
type HeadProps = OriginProps & HeadHTMLProps
/** Set of pages that have triggered a large data warning on production mode. */
const largePageDataWarnings = new Set()
function getDocumentFiles(
buildManifest: BuildManifest,
pathname: string,
inAmpMode: boolean
): DocumentFiles {
const sharedFiles: readonly string[] = getPageFiles(buildManifest, '/_app')
const pageFiles: readonly string[] =
process.env.NEXT_RUNTIME !== 'edge' && inAmpMode
? []
: getPageFiles(buildManifest, pathname)
return {
sharedFiles,
pageFiles,
allFiles: [...new Set([...sharedFiles, ...pageFiles])],
}
}
function getPolyfillScripts(context: HtmlProps, props: OriginProps) {
// polyfills.js has to be rendered as nomodule without async
// It also has to be the first script to load
const {
assetPrefix,
buildManifest,
assetQueryString,
disableOptimizedLoading,
crossOrigin,
} = context
return buildManifest.polyfillFiles
.filter(
(polyfill) => polyfill.endsWith('.js') && !polyfill.endsWith('.module.js')
)
.map((polyfill) => (
))
}
function hasComponentProps(child: any): child is React.ReactElement {
return !!child && !!child.props
}
function AmpStyles({
styles,
}: {
styles?: React.ReactElement[] | Iterable
}) {
if (!styles) return null
// try to parse styles from fragment for backwards compat
const curStyles: React.ReactElement[] = Array.isArray(styles)
? (styles as React.ReactElement[])
: []
if (
// @ts-ignore Property 'props' does not exist on type ReactElement
styles.props &&
// @ts-ignore Property 'props' does not exist on type ReactElement
Array.isArray(styles.props.children)
) {
const hasStyles = (el: React.ReactElement) =>
el?.props?.dangerouslySetInnerHTML?.__html
// @ts-ignore Property 'props' does not exist on type ReactElement
styles.props.children.forEach((child: React.ReactElement) => {
if (Array.isArray(child)) {
child.forEach((el) => hasStyles(el) && curStyles.push(el))
} else if (hasStyles(child)) {
curStyles.push(child)
}
})
}
/* Add custom styles before AMP styles to prevent accidental overrides */
return (