Skip to content

Commit

Permalink
expose defaultGetInitialProps on DocumentContext
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Sep 2, 2021
1 parent 9a4681e commit a0ea060
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
12 changes: 2 additions & 10 deletions packages/next/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,8 @@ export default class Document<P = {}> extends Component<DocumentProps & P> {
* `getInitialProps` hook returns the context object with the addition of `renderPage`.
* `renderPage` callback executes `React` rendering logic synchronously to support server-rendering wrappers
*/
static async getInitialProps(
ctx: DocumentContext
): Promise<DocumentInitialProps> {
const enhanceApp = (App: any) => {
return (props: any) => <App {...props} />
}

const { html, head } = await ctx.renderPage({ enhanceApp })
const styles = ctx.jsxStyleRegistry.styles()
return { html, head, styles }
static getInitialProps(ctx: DocumentContext): Promise<DocumentInitialProps> {
return ctx.defaultGetInitialProps(ctx)
}

render() {
Expand Down
18 changes: 15 additions & 3 deletions packages/next/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
ComponentsEnhancer,
DocumentInitialProps,
DocumentProps,
DocumentContext,
HtmlContext,
HtmlProps,
getDisplayName,
Expand Down Expand Up @@ -482,6 +483,7 @@ export async function renderToHTML(
isPreview,
(req as any).__nextIsLocaleDomain
)
const jsxStyleRegistry = createStyleRegistry()
const ctx = {
err,
req: isAutoExport ? undefined : req,
Expand All @@ -499,7 +501,17 @@ export async function renderToHTML(
</AppContainer>
)
},
jsxStyleRegistry: createStyleRegistry(),
defaultGetInitialProps: async (
docCtx: DocumentContext
): Promise<DocumentInitialProps> => {
const enhanceApp = (AppComp: any) => {
return (props: any) => <AppComp {...props} />
}

const { html, head } = await docCtx.renderPage({ enhanceApp })
const styles = jsxStyleRegistry.styles()
return { html, head, styles }
},
}
let props: any

Expand Down Expand Up @@ -537,7 +549,7 @@ export async function renderToHTML(
<LoadableContext.Provider
value={(moduleName) => reactLoadableModules.push(moduleName)}
>
<StyleRegistry registry={ctx.jsxStyleRegistry}>
<StyleRegistry registry={jsxStyleRegistry}>
{children}
</StyleRegistry>
</LoadableContext.Provider>
Expand Down Expand Up @@ -1008,7 +1020,7 @@ export async function renderToHTML(
documentElement: () => (Document as any)(),
head,
headTags: [],
styles: ctx.jsxStyleRegistry.styles(),
styles: jsxStyleRegistry.styles(),
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions packages/next/shared/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import type { ParsedUrlQuery } from 'querystring'
import type { PreviewData } from 'next/types'
import type { UrlObject } from 'url'
import { createContext } from 'react'
import { StyleRegistry } from 'styled-jsx'

export type NextComponentType<
C extends BaseContext = NextPageContext,
Expand Down Expand Up @@ -179,7 +178,7 @@ export type AppPropsType<

export type DocumentContext = NextPageContext & {
renderPage: RenderPage
jsxStyleRegistry: StyleRegistry
defaultGetInitialProps(ctx: DocumentContext): Promise<DocumentInitialProps>
}

export type DocumentInitialProps = RenderPageResult & {
Expand Down

0 comments on commit a0ea060

Please sign in to comment.