Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SSG types and clean up RenderOpts type #10259

Merged
merged 4 commits into from Jan 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next/build/babel/plugins/next-page-config.ts
@@ -1,6 +1,6 @@
import { NodePath, PluginObj } from '@babel/core'
import * as BabelTypes from '@babel/types'
import { PageConfig } from '../../../types'
import { PageConfig } from 'next/types'

const configKeys = new Set(['amp'])
const STRING_LITERAL_DROP_BUNDLE = '__NEXT_DROP_CLIENT_FILE__'
Expand Down
2 changes: 1 addition & 1 deletion packages/next/next-server/lib/utils.ts
Expand Up @@ -3,7 +3,7 @@ import { ParsedUrlQuery } from 'querystring'
import { ComponentType } from 'react'
import { format, URLFormatOptions, UrlObject } from 'url'

import { ManifestItem } from '../server/render'
import { ManifestItem } from '../server/load-components'
import { NextRouter } from './router/router'

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/next/next-server/server/api-utils.ts
Expand Up @@ -4,7 +4,7 @@ import { Stream } from 'stream'
import getRawBody from 'raw-body'
import { parse } from 'content-type'
import { Params } from './router'
import { PageConfig } from '../../types'
import { PageConfig } from 'next/types'
import { interopDefault } from './load-components'
import { isResSent } from '../lib/utils'

Expand Down
48 changes: 32 additions & 16 deletions packages/next/next-server/server/load-components.ts
Expand Up @@ -5,28 +5,44 @@ import {
SERVER_DIRECTORY,
} from '../lib/constants'
import { join } from 'path'
import { PageConfig } from '../../types'
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) {
return mod.default || mod
}

export type ManifestItem = {
id: number | string
name: string
file: string
publicPath: string
}

type ReactLoadableManifest = { [moduleId: string]: ManifestItem[] }

type Unstable_getStaticProps = (params: {
params: ParsedUrlQuery | undefined
}) => Promise<{
props: { [key: string]: any }
revalidate?: number | boolean
}>

type Unstable_getStaticPaths = () => Promise<Array<string | ParsedUrlQuery>>

export type LoadComponentsReturnType = {
Component: any
pageConfig: PageConfig
unstable_getStaticProps?: (params: {
params: any
}) => {
props: any
revalidate?: number | boolean
}
unstable_getStaticPaths?: () => void
buildManifest?: any
reactLoadableManifest?: any
Document?: any
DocumentMiddleware?: any
App?: any
Component: React.ComponentType
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(
Expand All @@ -42,7 +58,7 @@ export async function loadComponents(
pageConfig: Component.config || {},
unstable_getStaticProps: Component.unstable_getStaticProps,
unstable_getStaticPaths: Component.unstable_getStaticPaths,
}
} as LoadComponentsReturnType
}
const documentPath = join(
distDir,
Expand Down
10 changes: 7 additions & 3 deletions packages/next/next-server/server/next-server.ts
Expand Up @@ -863,15 +863,15 @@ export default class Server {
// check request state
const isLikeServerless =
typeof result.Component === 'object' &&
typeof result.Component.renderReqToHTML === 'function'
typeof (result.Component as any).renderReqToHTML === 'function'
const isSSG = !!result.unstable_getStaticProps

// non-spr requests should render like normal
if (!isSSG) {
// handle serverless
if (isLikeServerless) {
this.prepareServerlessUrl(req, query)
return result.Component.renderReqToHTML(req, res)
return (result.Component as any).renderReqToHTML(req, res)
}

return renderToHTML(req, res, pathname, query, {
Expand Down Expand Up @@ -929,7 +929,11 @@ export default class Server {
let renderResult
// handle serverless
if (isLikeServerless) {
renderResult = await result.Component.renderReqToHTML(req, res, true)
renderResult = await (result.Component as any).renderReqToHTML(
req,
res,
true
)

html = renderResult.html
pageData = renderResult.renderOpts.pageData
Expand Down
34 changes: 4 additions & 30 deletions packages/next/next-server/server/render.tsx
Expand Up @@ -14,31 +14,19 @@ import {
NextComponentType,
DocumentType,
AppType,
NextPageContext,
} from '../lib/utils'
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 { LoadableContext } from '../lib/loadable-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 optimizeAmp from './optimize-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 { SSG_GET_INITIAL_PROPS_CONFLICT } from '../../lib/constants'
import { AMP_RENDER_TARGET } from '../lib/constants'

export type ManifestItem = {
id: number | string
name: string
file: string
publicPath: string
}

type ReactLoadableManifest = { [moduleId: string]: ManifestItem[] }
import { LoadComponentsReturnType, ManifestItem } from './load-components'

function noRouter() {
const message =
Expand Down Expand Up @@ -122,8 +110,7 @@ function render(
return { html, head }
}

type RenderOpts = {
documentMiddlewareEnabled: boolean
type RenderOpts = LoadComponentsReturnType & {
staticMarkup: boolean
buildId: string
canonicalBase: string
Expand All @@ -139,22 +126,9 @@ type RenderOpts = {
ampPath?: string
inAmpMode?: 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 }>
ampValidator?: (html: string, pathname: string) => Promise<void>
unstable_getStaticProps?: (params: {
params: any | undefined
}) => {
props: any
revalidate?: number | boolean
}
unstable_getStaticPaths?: () => void
documentMiddlewareEnabled?: boolean
}

function renderDocument(
Expand Down