Skip to content

Commit

Permalink
Merge branch 'canary' into opt-render
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Apr 28, 2022
2 parents ea77b1d + 4392b6a commit aa9d9df
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 28 deletions.
Expand Up @@ -64,12 +64,6 @@ export default async function middlewareSSRLoader(this: any) {
const reactLoadableManifest = self.__REACT_LOADABLE_MANIFEST
const rscManifest = self.__RSC_MANIFEST
// Set server context
self.__server_context = {
page: ${JSON.stringify(page)},
buildId: ${JSON.stringify(buildId)},
}
const render = getRender({
dev: ${dev},
page: ${JSON.stringify(page)},
Expand Down
Expand Up @@ -57,6 +57,7 @@ export function getRender({
conf: config,
minimalMode: true,
webServerConfig: {
page,
extendRenderOpts: {
buildId,
reactRoot: true,
Expand Down
26 changes: 15 additions & 11 deletions packages/next/server/base-server.ts
Expand Up @@ -128,7 +128,7 @@ type RequestContext = {
renderOpts: RenderOptsPartial
}

export default abstract class Server {
export default abstract class Server<ServerOptions extends Options = Options> {
protected dir: string
protected quiet: boolean
protected nextConfig: NextConfigComplete
Expand Down Expand Up @@ -171,6 +171,7 @@ export default abstract class Server {
serverComponentProps?: any
reactRoot: boolean
}
protected serverOptions: ServerOptions
private incrementalCache: IncrementalCache
private responseCache: ResponseCache
protected router: Router
Expand Down Expand Up @@ -261,16 +262,19 @@ export default abstract class Server {

protected abstract loadEnvConfig(params: { dev: boolean }): void

public constructor({
dir = '.',
quiet = false,
conf,
dev = false,
minimalMode = false,
customServer = true,
hostname,
port,
}: Options) {
public constructor(options: ServerOptions) {
const {
dir = '.',
quiet = false,
conf,
dev = false,
minimalMode = false,
customServer = true,
hostname,
port,
} = options
this.serverOptions = options

this.dir = resolve(dir)
this.quiet = quiet
this.loadEnvConfig({ dev })
Expand Down
28 changes: 17 additions & 11 deletions packages/next/server/web-server.ts
Expand Up @@ -11,18 +11,22 @@ import BaseServer from './base-server'
import { renderToHTML } from './render'
import { byteLength, generateETag } from './api-utils/web'

interface WebServerConfig {
loadComponent: (pathname: string) => Promise<LoadComponentsReturnType | null>
extendRenderOpts?: Partial<BaseServer['renderOpts']>
interface WebServerOptions extends Options {
webServerConfig: {
page: string
loadComponent: (
pathname: string
) => Promise<LoadComponentsReturnType | null>
extendRenderOpts: Partial<BaseServer['renderOpts']> &
Pick<BaseServer['renderOpts'], 'buildId'>
}
}

export default class NextWebServer extends BaseServer {
webServerConfig: WebServerConfig

constructor(options: Options & { webServerConfig: WebServerConfig }) {
export default class NextWebServer extends BaseServer<WebServerOptions> {
constructor(options: WebServerOptions) {
super(options)

this.webServerConfig = options.webServerConfig
// Extend `renderOpts`.
Object.assign(this.renderOpts, options.webServerConfig.extendRenderOpts)
}

Expand Down Expand Up @@ -58,7 +62,7 @@ export default class NextWebServer extends BaseServer {
return ''
}
protected getBuildId() {
return (globalThis as any).__server_context.buildId
return this.serverOptions.webServerConfig.extendRenderOpts.buildId
}
protected loadEnvConfig() {
// The web server does not need to load the env config. This is done by the
Expand Down Expand Up @@ -96,7 +100,7 @@ export default class NextWebServer extends BaseServer {
}
protected getPagesManifest() {
return {
[(globalThis as any).__server_context.page]: '',
[this.serverOptions.webServerConfig.page]: '',
}
}
protected getFilesystemPaths() {
Expand Down Expand Up @@ -199,7 +203,9 @@ export default class NextWebServer extends BaseServer {
query?: NextParsedUrlQuery,
params?: Params | null
) {
const result = await this.webServerConfig.loadComponent(pathname)
const result = await this.serverOptions.webServerConfig.loadComponent(
pathname
)
if (!result) return null

return {
Expand Down

0 comments on commit aa9d9df

Please sign in to comment.