[next] separate context objects for Document + fix tests

This commit is contained in:
Resi Respati
2018-05-16 21:57:28 +07:00
parent 28b1367d70
commit 9d68e22a0c
3 changed files with 46 additions and 11 deletions
+13
View File
@@ -1,4 +1,5 @@
import * as React from "react";
import { NextContext } from ".";
export interface DocumentProps {
__NEXT_DATA__?: any;
@@ -9,6 +10,18 @@ export interface DocumentProps {
[key: string]: any;
}
/**
* Context object used inside `Document`
*/
export interface NextDocumentContext extends NextContext {
/** A callback that executes the actual React rendering logic (synchronously) */
renderPage(
cb?: (enhancer: () => JSX.Element) => React.ComponentType<any>
): {
[key: string]: any
};
}
export class Head extends React.Component<any> {}
export class Main extends React.Component {}
export class NextScript extends React.Component {}
-6
View File
@@ -36,12 +36,6 @@ declare namespace next {
jsonPageRes?: NodeResponse;
/** Error object if any error is encountered during the rendering */
err?: Error;
/** a callback that executes the actual React rendering logic (synchronously) */
renderPage(
cb?: (enhancer: () => JSX.Element) => React.ComponentType<any>
): {
[key: string]: any
};
}
type NextSFC<P = {}> = NextStatelessComponent<P>;
+33 -5
View File
@@ -1,12 +1,40 @@
import Document, * as document from "next/document";
import Document, { Head, Main, NextScript, NextDocumentContext } from 'next/document';
import * as React from "react";
const results = (
<Document any="property" should="work" here>
<document.Head some="more" properties>
<Head some="more" properties>
<meta name="description" content="Head can have children, too!" />
</document.Head>
<document.Main />
<document.NextScript />
</Head>
<Main />
<NextScript />
</Document>
);
const Wrapper: React.SFC = ({ children }) => <React.Fragment>{children}</React.Fragment>;
export default class MyDocument extends Document {
static async getInitialProps({ renderPage }: NextDocumentContext) {
// Without callback
const page = renderPage();
// With callback
const differentPage = renderPage(App => props => <Wrapper><App {...props} /></Wrapper>);
const style = {};
return { ...page, style };
}
render() {
return (
<html>
<Head>
<title>My page</title>
<style id='cxs-style' dangerouslySetInnerHTML={{ __html: this.props.style }} />
</Head>
<body>
<Main />
<NextScript />
</body>
</html>
);
}
}