Commit f143ca63 authored by JJ Kasper's avatar JJ Kasper Committed by Joe Haddad

Update SSG types and clean up RenderOpts type (#10259)

* Update SSG types and clean up RenderOpts type

* Move SSG types back to internal module
parent e079cce4
import { NodePath, PluginObj } from '@babel/core' import { NodePath, PluginObj } from '@babel/core'
import * as BabelTypes from '@babel/types' import * as BabelTypes from '@babel/types'
import { PageConfig } from '../../../types' import { PageConfig } from 'next/types'
const configKeys = new Set(['amp']) const configKeys = new Set(['amp'])
const STRING_LITERAL_DROP_BUNDLE = '__NEXT_DROP_CLIENT_FILE__' const STRING_LITERAL_DROP_BUNDLE = '__NEXT_DROP_CLIENT_FILE__'
......
...@@ -3,7 +3,7 @@ import { ParsedUrlQuery } from 'querystring' ...@@ -3,7 +3,7 @@ import { ParsedUrlQuery } from 'querystring'
import { ComponentType } from 'react' import { ComponentType } from 'react'
import { format, URLFormatOptions, UrlObject } from 'url' import { format, URLFormatOptions, UrlObject } from 'url'
import { ManifestItem } from '../server/render' import { ManifestItem } from '../server/load-components'
import { NextRouter } from './router/router' import { NextRouter } from './router/router'
/** /**
......
...@@ -4,7 +4,7 @@ import { Stream } from 'stream' ...@@ -4,7 +4,7 @@ import { Stream } from 'stream'
import getRawBody from 'raw-body' import getRawBody from 'raw-body'
import { parse } from 'content-type' import { parse } from 'content-type'
import { Params } from './router' import { Params } from './router'
import { PageConfig } from '../../types' import { PageConfig } from 'next/types'
import { interopDefault } from './load-components' import { interopDefault } from './load-components'
import { isResSent } from '../lib/utils' import { isResSent } from '../lib/utils'
......
...@@ -5,28 +5,44 @@ import { ...@@ -5,28 +5,44 @@ import {
SERVER_DIRECTORY, SERVER_DIRECTORY,
} from '../lib/constants' } from '../lib/constants'
import { join } from 'path' import { join } from 'path'
import { PageConfig } from '../../types'
import { requirePage } from './require' import { requirePage } from './require'
import { ParsedUrlQuery } from 'querystring'
import { BuildManifest } from './get-page-files'
import { AppType, DocumentType } from '../lib/utils'
import { PageConfig, NextPageContext } from 'next/types'
export function interopDefault(mod: any) { export function interopDefault(mod: any) {
return mod.default || mod return mod.default || mod
} }
export type LoadComponentsReturnType = { export type ManifestItem = {
Component: any id: number | string
pageConfig: PageConfig name: string
unstable_getStaticProps?: (params: { file: string
params: any publicPath: string
}) => { }
props: any
type ReactLoadableManifest = { [moduleId: string]: ManifestItem[] }
type Unstable_getStaticProps = (params: {
params: ParsedUrlQuery | undefined
}) => Promise<{
props: { [key: string]: any }
revalidate?: number | boolean revalidate?: number | boolean
} }>
unstable_getStaticPaths?: () => void
buildManifest?: any type Unstable_getStaticPaths = () => Promise<Array<string | ParsedUrlQuery>>
reactLoadableManifest?: any
Document?: any export type LoadComponentsReturnType = {
DocumentMiddleware?: any Component: React.ComponentType
App?: any pageConfig?: PageConfig
buildManifest: BuildManifest
reactLoadableManifest: ReactLoadableManifest
Document: DocumentType
DocumentMiddleware?: (ctx: NextPageContext) => void
App: AppType
unstable_getStaticProps?: Unstable_getStaticProps
unstable_getStaticPaths?: Unstable_getStaticPaths
} }
export async function loadComponents( export async function loadComponents(
...@@ -42,7 +58,7 @@ export async function loadComponents( ...@@ -42,7 +58,7 @@ export async function loadComponents(
pageConfig: Component.config || {}, pageConfig: Component.config || {},
unstable_getStaticProps: Component.unstable_getStaticProps, unstable_getStaticProps: Component.unstable_getStaticProps,
unstable_getStaticPaths: Component.unstable_getStaticPaths, unstable_getStaticPaths: Component.unstable_getStaticPaths,
} } as LoadComponentsReturnType
} }
const documentPath = join( const documentPath = join(
distDir, distDir,
......
...@@ -863,7 +863,7 @@ export default class Server { ...@@ -863,7 +863,7 @@ export default class Server {
// check request state // check request state
const isLikeServerless = const isLikeServerless =
typeof result.Component === 'object' && typeof result.Component === 'object' &&
typeof result.Component.renderReqToHTML === 'function' typeof (result.Component as any).renderReqToHTML === 'function'
const isSSG = !!result.unstable_getStaticProps const isSSG = !!result.unstable_getStaticProps
// non-spr requests should render like normal // non-spr requests should render like normal
...@@ -871,7 +871,7 @@ export default class Server { ...@@ -871,7 +871,7 @@ export default class Server {
// handle serverless // handle serverless
if (isLikeServerless) { if (isLikeServerless) {
this.prepareServerlessUrl(req, query) this.prepareServerlessUrl(req, query)
return result.Component.renderReqToHTML(req, res) return (result.Component as any).renderReqToHTML(req, res)
} }
return renderToHTML(req, res, pathname, query, { return renderToHTML(req, res, pathname, query, {
...@@ -929,7 +929,11 @@ export default class Server { ...@@ -929,7 +929,11 @@ export default class Server {
let renderResult let renderResult
// handle serverless // handle serverless
if (isLikeServerless) { if (isLikeServerless) {
renderResult = await result.Component.renderReqToHTML(req, res, true) renderResult = await (result.Component as any).renderReqToHTML(
req,
res,
true
)
html = renderResult.html html = renderResult.html
pageData = renderResult.renderOpts.pageData pageData = renderResult.renderOpts.pageData
......
...@@ -14,31 +14,19 @@ import { ...@@ -14,31 +14,19 @@ import {
NextComponentType, NextComponentType,
DocumentType, DocumentType,
AppType, AppType,
NextPageContext,
} from '../lib/utils' } from '../lib/utils'
import Head, { defaultHead } from '../lib/head' import Head, { defaultHead } from '../lib/head'
// @ts-ignore types will be added later as it's an internal module
import Loadable from '../lib/loadable' import Loadable from '../lib/loadable'
import { LoadableContext } from '../lib/loadable-context' import { LoadableContext } from '../lib/loadable-context'
import { RouterContext } from '../lib/router-context' import { RouterContext } from '../lib/router-context'
import { getPageFiles, BuildManifest } from './get-page-files' import { getPageFiles } from './get-page-files'
import { AmpStateContext } from '../lib/amp-context' import { AmpStateContext } from '../lib/amp-context'
import optimizeAmp from './optimize-amp' import optimizeAmp from './optimize-amp'
import { isInAmpMode } from '../lib/amp' import { isInAmpMode } from '../lib/amp'
// Uses a module path because of the compiled output directory location
import { PageConfig } from 'next/types'
import { isDynamicRoute } from '../lib/router/utils/is-dynamic' import { isDynamicRoute } from '../lib/router/utils/is-dynamic'
import { SSG_GET_INITIAL_PROPS_CONFLICT } from '../../lib/constants' import { SSG_GET_INITIAL_PROPS_CONFLICT } from '../../lib/constants'
import { AMP_RENDER_TARGET } from '../lib/constants' import { AMP_RENDER_TARGET } from '../lib/constants'
import { LoadComponentsReturnType, ManifestItem } from './load-components'
export type ManifestItem = {
id: number | string
name: string
file: string
publicPath: string
}
type ReactLoadableManifest = { [moduleId: string]: ManifestItem[] }
function noRouter() { function noRouter() {
const message = const message =
...@@ -122,8 +110,7 @@ function render( ...@@ -122,8 +110,7 @@ function render(
return { html, head } return { html, head }
} }
type RenderOpts = { type RenderOpts = LoadComponentsReturnType & {
documentMiddlewareEnabled: boolean
staticMarkup: boolean staticMarkup: boolean
buildId: string buildId: string
canonicalBase: string canonicalBase: string
...@@ -139,22 +126,9 @@ type RenderOpts = { ...@@ -139,22 +126,9 @@ type RenderOpts = {
ampPath?: string ampPath?: string
inAmpMode?: boolean inAmpMode?: boolean
hybridAmp?: boolean hybridAmp?: boolean
buildManifest: BuildManifest
reactLoadableManifest: ReactLoadableManifest
pageConfig: PageConfig
Component: React.ComponentType
Document: DocumentType
DocumentMiddleware: (ctx: NextPageContext) => void
App: AppType
ErrorDebug?: React.ComponentType<{ error: Error }> ErrorDebug?: React.ComponentType<{ error: Error }>
ampValidator?: (html: string, pathname: string) => Promise<void> ampValidator?: (html: string, pathname: string) => Promise<void>
unstable_getStaticProps?: (params: { documentMiddlewareEnabled?: boolean
params: any | undefined
}) => {
props: any
revalidate?: number | boolean
}
unstable_getStaticPaths?: () => void
} }
function renderDocument( function renderDocument(
......
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