From 9d68e22a0c62fbd58869ab901ad09badf8eb6dcf Mon Sep 17 00:00:00 2001 From: Resi Respati Date: Wed, 16 May 2018 21:57:28 +0700 Subject: [PATCH] [next] separate context objects for Document + fix tests --- types/next/document.d.ts | 13 +++++++++ types/next/index.d.ts | 6 ---- types/next/test/next-document-tests.tsx | 38 +++++++++++++++++++++---- 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/types/next/document.d.ts b/types/next/document.d.ts index 4696819626..01344de7c8 100644 --- a/types/next/document.d.ts +++ b/types/next/document.d.ts @@ -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 + ): { + [key: string]: any + }; +} + export class Head extends React.Component {} export class Main extends React.Component {} export class NextScript extends React.Component {} diff --git a/types/next/index.d.ts b/types/next/index.d.ts index 939dc27bdb..6679420ba3 100644 --- a/types/next/index.d.ts +++ b/types/next/index.d.ts @@ -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 - ): { - [key: string]: any - }; } type NextSFC

= NextStatelessComponent

; diff --git a/types/next/test/next-document-tests.tsx b/types/next/test/next-document-tests.tsx index 0177d1d451..2b3257cd25 100644 --- a/types/next/test/next-document-tests.tsx +++ b/types/next/test/next-document-tests.tsx @@ -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 = ( - + - - - + +

+ ); + +const Wrapper: React.SFC = ({ children }) => {children}; + +export default class MyDocument extends Document { + static async getInitialProps({ renderPage }: NextDocumentContext) { + // Without callback + const page = renderPage(); + // With callback + const differentPage = renderPage(App => props => ); + const style = {}; + return { ...page, style }; + } + + render() { + return ( + + + My page +