import React, { Component, ReactElement, ReactNode, useContext } from 'react' import { OPTIMIZED_FONT_PROVIDERS, NEXT_BUILTIN_DOCUMENT, } from '../shared/lib/constants' import type { DocumentContext, DocumentInitialProps, DocumentProps, DocumentType, } from '../shared/lib/utils' import { BuildManifest, getPageFiles } from '../server/get-page-files' import { cleanAmpPath } from '../server/utils' import { htmlEscapeJsonString } from '../server/htmlescape' import Script, { ScriptProps } from '../client/script' import isError from '../lib/is-error' import { HtmlContext } from '../shared/lib/html-context' import type { HtmlProps } from '../shared/lib/html-context' export { DocumentContext, DocumentInitialProps, DocumentProps } export type OriginProps = { nonce?: string crossOrigin?: string 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 function getDocumentFiles( buildManifest: BuildManifest, pathname: string, inAmpMode: boolean ): DocumentFiles { const sharedFiles: readonly string[] = getPageFiles(buildManifest, '/_app') const pageFiles: readonly string[] = 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, devOnlyCacheBusterQueryString, disableOptimizedLoading, crossOrigin, } = context return buildManifest.polyfillFiles .filter( (polyfill) => polyfill.endsWith('.js') && !polyfill.endsWith('.module.js') ) .map((polyfill) => (