Skip to content

Commit

Permalink
Decouple nodejs path dependency from base server
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Apr 20, 2022
1 parent af23248 commit 3da1442
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 11 deletions.
17 changes: 7 additions & 10 deletions packages/next/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@ import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plug
import type { BaseNextRequest, BaseNextResponse } from './base-http'
import type { PayloadOptions } from './send-payload'

import { join, resolve } from 'path'
import { parse as parseQs } from 'querystring'
import { format as formatUrl, parse as parseUrl } from 'url'
import { getRedirectStatus } from '../lib/load-custom-routes'
import {
SERVERLESS_DIRECTORY,
SERVER_DIRECTORY,
STATIC_STATUS_PAGES,
TEMPORARY_REDIRECT_STATUS,
} from '../shared/lib/constants'
Expand Down Expand Up @@ -182,6 +179,9 @@ export default abstract class Server {
public readonly hostname?: string
public readonly port?: number

protected abstract getDir(_dir: string): string
protected abstract getDistDir(): string
protected abstract getPagesDir(): string
protected abstract getPublicDir(): string
protected abstract getHasStaticDir(): boolean
protected abstract getPagesManifest(): PagesManifest | undefined
Expand Down Expand Up @@ -271,7 +271,7 @@ export default abstract class Server {
hostname,
port,
}: Options) {
this.dir = resolve(dir)
this.dir = this.getDir(dir)
this.quiet = quiet
this.loadEnvConfig({ dev })

Expand All @@ -280,7 +280,7 @@ export default abstract class Server {
this.nextConfig = conf as NextConfigComplete
this.hostname = hostname
this.port = port
this.distDir = join(this.dir, this.nextConfig.distDir)
this.distDir = this.getDistDir()
this.publicDir = this.getPublicDir()
this.hasStaticDir = !minimalMode && this.getHasStaticDir()

Expand Down Expand Up @@ -350,15 +350,12 @@ export default abstract class Server {
this.router = new Router(this.generateRoutes())
this.setAssetPrefix(assetPrefix)

const pagesDir = this.getPagesDir()
this.incrementalCache = new IncrementalCache({
fs: this.getCacheFilesystem(),
dev,
distDir: this.distDir,
pagesDir: join(
this.distDir,
this._isLikeServerless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY,
'pages'
),
pagesDir,
locales: this.nextConfig.i18n?.locales,
max: this.nextConfig.experimental.isrMemoryCacheSize,
flushToDisk: !minimalMode && this.nextConfig.experimental.isrFlushToDisk,
Expand Down
16 changes: 16 additions & 0 deletions packages/next/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,26 @@ export default class NextNodeServer extends BaseServer {
loadEnvConfig(this.dir, dev, Log)
}

protected getDir(_dir: string): string {
return resolve(_dir)
}

protected getPublicDir(): string {
return join(this.dir, CLIENT_PUBLIC_FILES_PATH)
}

protected getDistDir(): string {
return join(this.dir, this.nextConfig.distDir)
}

protected getPagesDir(): string {
return join(
this.distDir,
this._isLikeServerless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY,
'pages'
)
}

protected getHasStaticDir(): boolean {
return fs.existsSync(join(this.dir, 'static'))
}
Expand Down
3 changes: 2 additions & 1 deletion packages/next/server/render.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { IncomingMessage, ServerResponse } from 'http'
import type { ParsedUrlQuery } from 'querystring'
import type { NextRouter } from '../shared/lib/router/router'
import type { HtmlProps } from '../shared/lib/html-context'
import type { DomainLocale } from './config'
Expand All @@ -20,7 +21,7 @@ import type { GetServerSideProps, GetStaticProps, PreviewData } from '../types'
import type { UnwrapPromise } from '../lib/coalesced-function'

import React from 'react'
import { ParsedUrlQuery, stringify as stringifyQuery } from 'querystring'
import { stringify as stringifyQuery } from 'querystring'
import { createFromReadableStream } from 'next/dist/compiled/react-server-dom-webpack'
import { renderToReadableStream } from 'next/dist/compiled/react-server-dom-webpack/writer.browser.server'
import { StyleRegistry, createStyleRegistry } from 'styled-jsx'
Expand Down
11 changes: 11 additions & 0 deletions packages/next/server/web-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ export default class NextWebServer extends BaseServer {

constructor(options: Options & { webServerConfig: WebServerConfig }) {
super(options)

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

protected generateRewrites() {
// @TODO: assuming minimal mode right now
return {
Expand Down Expand Up @@ -50,10 +52,19 @@ export default class NextWebServer extends BaseServer {
// @TODO
return ''
}
protected getDir() {
return '.'
}
protected getPublicDir() {
// Public files are not handled by the web server.
return ''
}
protected getDistDir() {
return ''
}
protected getPagesDir() {
return ''
}
protected getBuildId() {
return (globalThis as any).__server_context.buildId
}
Expand Down
4 changes: 4 additions & 0 deletions test/integration/react-18/app/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ export default function Index() {
</div>
)
}

export const config = {
// runtime: 'edge'
}
3 changes: 3 additions & 0 deletions test/integration/react-18/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import webdriver from 'next-webdriver'
const appDir = join(__dirname, '../app')
const nextConfig = new File(join(appDir, 'next.config.js'))
const invalidPage = new File(join(appDir, 'pages/invalid.js'))
const indexPage = new File(join(appDir, 'pages/index.js'))

describe('Basics', () => {
runTests('default setting with react 18', basics)
Expand Down Expand Up @@ -67,12 +68,14 @@ function runTestsAgainstRuntime(runtime) {
invalidPage.write(`export const value = 1`)
}
nextConfig.replace("// runtime: 'edge'", `runtime: '${runtime}'`)
indexPage.replace("// runtime: 'edge'", `runtime: '${runtime}'`)
},
afterAll: (env) => {
if (env === 'dev') {
invalidPage.delete()
}
nextConfig.restore()
indexPage.restore()
},
}
)
Expand Down

0 comments on commit 3da1442

Please sign in to comment.