Commit b2adfde9 authored by Gerald Monaco's avatar Gerald Monaco Committed by Joe Haddad

Remove _document context (#9068)

parent 09681cd5
import * as React from 'react'
import { DocumentProps } from './utils'
type DocumentContext = {
readonly _documentProps: DocumentProps
readonly _devOnlyInvalidateCacheQueryString: string
}
export const DocumentContext = React.createContext<DocumentContext>(null as any)
...@@ -4,6 +4,7 @@ import { ComponentType } from 'react' ...@@ -4,6 +4,7 @@ import { ComponentType } from 'react'
import { ParsedUrlQuery } from 'querystring' import { ParsedUrlQuery } from 'querystring'
import { ManifestItem } from '../server/render' import { ManifestItem } from '../server/render'
import { NextRouter } from './router/router' import { NextRouter } from './router/router'
import { DocumentContext as DocumentComponentContext } from './document-context'
/** /**
* Types used by both next and next-server * Types used by both next and next-server
...@@ -20,7 +21,12 @@ export type DocumentType = NextComponentType< ...@@ -20,7 +21,12 @@ export type DocumentType = NextComponentType<
DocumentContext, DocumentContext,
DocumentInitialProps, DocumentInitialProps,
DocumentProps DocumentProps
> > & {
renderDocument(
Document: DocumentType,
props: DocumentProps
): React.ReactElement
}
export type AppType = NextComponentType< export type AppType = NextComponentType<
AppContextType, AppContextType,
......
...@@ -209,8 +209,8 @@ function renderDocument( ...@@ -209,8 +209,8 @@ function renderDocument(
'<!DOCTYPE html>' + '<!DOCTYPE html>' +
renderToStaticMarkup( renderToStaticMarkup(
<AmpStateContext.Provider value={ampState}> <AmpStateContext.Provider value={ampState}>
<Document {Document.renderDocument(Document, {
__NEXT_DATA__={{ __NEXT_DATA__: {
dataManager: dataManagerData, dataManager: dataManagerData,
props, // The result of getInitialProps props, // The result of getInitialProps
page: pathname, // The rendered page page: pathname, // The rendered page
...@@ -224,21 +224,21 @@ function renderDocument( ...@@ -224,21 +224,21 @@ function renderDocument(
dynamicIds: dynamicIds:
dynamicImportsIds.length === 0 ? undefined : dynamicImportsIds, dynamicImportsIds.length === 0 ? undefined : dynamicImportsIds,
err: err ? serializeError(dev, err) : undefined, // Error if one happened, otherwise don't sent in the resulting HTML err: err ? serializeError(dev, err) : undefined, // Error if one happened, otherwise don't sent in the resulting HTML
}} },
dangerousAsPath={dangerousAsPath} dangerousAsPath,
canonicalBase={canonicalBase} canonicalBase,
ampPath={ampPath} ampPath,
inAmpMode={inAmpMode} inAmpMode,
isDevelopment={!!dev} isDevelopment: !!dev,
hasCssMode={hasCssMode} hasCssMode,
hybridAmp={hybridAmp} hybridAmp,
staticMarkup={staticMarkup} staticMarkup,
devFiles={devFiles} devFiles,
files={files} files,
dynamicImports={dynamicImports} dynamicImports,
assetPrefix={assetPrefix} assetPrefix,
{...docProps} ...docProps,
/> })}
</AmpStateContext.Provider> </AmpStateContext.Provider>
) )
) )
......
...@@ -13,6 +13,7 @@ import { ...@@ -13,6 +13,7 @@ import {
CLIENT_STATIC_FILES_RUNTIME_AMP, CLIENT_STATIC_FILES_RUNTIME_AMP,
CLIENT_STATIC_FILES_RUNTIME_WEBPACK, CLIENT_STATIC_FILES_RUNTIME_WEBPACK,
} from '../next-server/lib/constants' } from '../next-server/lib/constants'
import { DocumentContext as DocumentComponentContext } from '../next-server/lib/document-context'
export { DocumentContext, DocumentInitialProps, DocumentProps } export { DocumentContext, DocumentInitialProps, DocumentProps }
...@@ -21,11 +22,6 @@ export type OriginProps = { ...@@ -21,11 +22,6 @@ export type OriginProps = {
crossOrigin?: string crossOrigin?: string
} }
export type DocumentComponentContext = {
readonly _documentProps: DocumentProps
readonly _devOnlyInvalidateCacheQueryString: string
}
export async function middleware({ req, res }: DocumentContext) {} export async function middleware({ req, res }: DocumentContext) {}
function dedupe(bundles: any[]): any[] { function dedupe(bundles: any[]): any[] {
...@@ -52,11 +48,6 @@ function getOptionalModernScriptVariant(path: string) { ...@@ -52,11 +48,6 @@ function getOptionalModernScriptVariant(path: string) {
* Commonly used for implementing server side rendering for `css-in-js` libraries. * Commonly used for implementing server side rendering for `css-in-js` libraries.
*/ */
export default class Document<P = {}> extends Component<DocumentProps & P> { export default class Document<P = {}> extends Component<DocumentProps & P> {
static childContextTypes = {
_documentProps: PropTypes.any,
_devOnlyInvalidateCacheQueryString: PropTypes.string,
}
/** /**
* `getInitialProps` hook returns the context object with the addition of `renderPage`. * `getInitialProps` hook returns the context object with the addition of `renderPage`.
* `renderPage` callback executes `React` rendering logic synchronously to support server-rendering wrappers * `renderPage` callback executes `React` rendering logic synchronously to support server-rendering wrappers
...@@ -69,17 +60,24 @@ export default class Document<P = {}> extends Component<DocumentProps & P> { ...@@ -69,17 +60,24 @@ export default class Document<P = {}> extends Component<DocumentProps & P> {
return { html, head, styles, dataOnly } return { html, head, styles, dataOnly }
} }
context!: DocumentComponentContext static renderDocument<P>(
Document: new () => Document<P>,
getChildContext(): DocumentComponentContext { props: DocumentProps & P
return { ): React.ReactElement {
_documentProps: this.props, return (
// In dev we invalidate the cache by appending a timestamp to the resource URL. <DocumentComponentContext.Provider
// This is a workaround to fix https://github.com/zeit/next.js/issues/5860 value={{
// TODO: remove this workaround when https://bugs.webkit.org/show_bug.cgi?id=187726 is fixed. _documentProps: props,
_devOnlyInvalidateCacheQueryString: // In dev we invalidate the cache by appending a timestamp to the resource URL.
process.env.NODE_ENV !== 'production' ? '?ts=' + Date.now() : '', // This is a workaround to fix https://github.com/zeit/next.js/issues/5860
} // TODO: remove this workaround when https://bugs.webkit.org/show_bug.cgi?id=187726 is fixed.
_devOnlyInvalidateCacheQueryString:
process.env.NODE_ENV !== 'production' ? '?ts=' + Date.now() : '',
}}
>
<Document {...props} />
</DocumentComponentContext.Provider>
)
} }
render() { render() {
...@@ -101,15 +99,13 @@ export class Html extends Component< ...@@ -101,15 +99,13 @@ export class Html extends Component<
HTMLHtmlElement HTMLHtmlElement
> >
> { > {
static contextTypes = { static contextType = DocumentComponentContext
_documentProps: PropTypes.any,
}
static propTypes = { static propTypes = {
children: PropTypes.node.isRequired, children: PropTypes.node.isRequired,
} }
context!: DocumentComponentContext context!: React.ContextType<typeof DocumentComponentContext>
render() { render() {
const { inAmpMode } = this.context._documentProps const { inAmpMode } = this.context._documentProps
...@@ -132,17 +128,14 @@ export class Head extends Component< ...@@ -132,17 +128,14 @@ export class Head extends Component<
HTMLHeadElement HTMLHeadElement
> >
> { > {
static contextTypes = { static contextType = DocumentComponentContext
_documentProps: PropTypes.any,
_devOnlyInvalidateCacheQueryString: PropTypes.string,
}
static propTypes = { static propTypes = {
nonce: PropTypes.string, nonce: PropTypes.string,
crossOrigin: PropTypes.string, crossOrigin: PropTypes.string,
} }
context!: DocumentComponentContext context!: React.ContextType<typeof DocumentComponentContext>
getCssLinks() { getCssLinks() {
const { assetPrefix, files } = this.context._documentProps const { assetPrefix, files } = this.context._documentProps
...@@ -469,12 +462,9 @@ export class Head extends Component< ...@@ -469,12 +462,9 @@ export class Head extends Component<
} }
export class Main extends Component { export class Main extends Component {
static contextTypes = { static contextType = DocumentComponentContext
_documentProps: PropTypes.any,
_devOnlyInvalidateCacheQueryString: PropTypes.string,
}
context!: DocumentComponentContext context!: React.ContextType<typeof DocumentComponentContext>
render() { render() {
const { inAmpMode, html } = this.context._documentProps const { inAmpMode, html } = this.context._documentProps
...@@ -484,17 +474,14 @@ export class Main extends Component { ...@@ -484,17 +474,14 @@ export class Main extends Component {
} }
export class NextScript extends Component<OriginProps> { export class NextScript extends Component<OriginProps> {
static contextTypes = { static contextType = DocumentComponentContext
_documentProps: PropTypes.any,
_devOnlyInvalidateCacheQueryString: PropTypes.string,
}
static propTypes = { static propTypes = {
nonce: PropTypes.string, nonce: PropTypes.string,
crossOrigin: PropTypes.string, crossOrigin: PropTypes.string,
} }
context!: DocumentComponentContext context!: React.ContextType<typeof DocumentComponentContext>
// Source: https://gist.github.com/samthor/64b114e4a4f539915a95b91ffd340acc // Source: https://gist.github.com/samthor/64b114e4a4f539915a95b91ffd340acc
static safariNomoduleFix = static safariNomoduleFix =
......
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