[next] Add enhanceApp / enhanceComponent

cf31021e25 (customizing-renderpage)
7e7be0e2e8/packages/next-server/server/render.tsx (L13-L32)
This commit is contained in:
Spencer Elliott
2019-01-31 10:47:37 -05:00
parent f8898c3dd1
commit c467d277ab
2 changed files with 21 additions and 6 deletions

View File

@@ -27,8 +27,15 @@ export type Enhancer<E extends PageProps = AnyPageProps, P extends any = E> = (
*/
export interface NextDocumentContext<Q extends DefaultQuery = DefaultQuery> extends NextContext<Q> {
/** A callback that executes the actual React rendering logic (synchronously) */
renderPage<E extends PageProps = AnyPageProps, P extends any = E>(
enhancer?: Enhancer<E, P> // tslint:disable-line no-unnecessary-generics
renderPage<
E extends PageProps = AnyPageProps,
P = E,
EA extends PageProps = AnyPageProps,
PA = EA,
>(
enhancer?:
| Enhancer<E, P> // tslint:disable-line no-unnecessary-generics
| { enhanceApp?: Enhancer<EA, PA>; enhanceComponent?: Enhancer<E, P> } // tslint:disable-line no-unnecessary-generics
): RenderPageResponse;
}

View File

@@ -38,12 +38,20 @@ class MyDoc extends Document<WithUrlProps> {
static async getInitialProps(ctx: NextDocumentContext) {
const { req, renderPage } = ctx;
// without callback
// without enhancer
const _page = renderPage();
// with callback
const enhancer: Enhancer<PageProps, {}> = App => props => <App />;
const { html, head, buildManifest } = renderPage(enhancer);
// with component enhancer
const enhancer: Enhancer<PageProps, {}> = Component => props => <Component />;
const _enhancedPage = renderPage(enhancer);
// with app and component enhancers
const enhanceApp: Enhancer<PageProps, {}> = App => props => <App />;
const enhanceComponent: Enhancer<PageProps, {}> = Component => props => <Component />;
const { html, head, buildManifest } = renderPage({
enhanceApp,
enhanceComponent
});
const initialProps = await Document.getInitialProps(ctx);