Skip to content

Commit 3da1442

Browse files
committed
Decouple nodejs path dependency from base server
1 parent af23248 commit 3da1442

File tree

6 files changed

+43
-11
lines changed

6 files changed

+43
-11
lines changed

packages/next/server/base-server.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,10 @@ import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plug
1818
import type { BaseNextRequest, BaseNextResponse } from './base-http'
1919
import type { PayloadOptions } from './send-payload'
2020

21-
import { join, resolve } from 'path'
2221
import { parse as parseQs } from 'querystring'
2322
import { format as formatUrl, parse as parseUrl } from 'url'
2423
import { getRedirectStatus } from '../lib/load-custom-routes'
2524
import {
26-
SERVERLESS_DIRECTORY,
27-
SERVER_DIRECTORY,
2825
STATIC_STATUS_PAGES,
2926
TEMPORARY_REDIRECT_STATUS,
3027
} from '../shared/lib/constants'
@@ -182,6 +179,9 @@ export default abstract class Server {
182179
public readonly hostname?: string
183180
public readonly port?: number
184181

182+
protected abstract getDir(_dir: string): string
183+
protected abstract getDistDir(): string
184+
protected abstract getPagesDir(): string
185185
protected abstract getPublicDir(): string
186186
protected abstract getHasStaticDir(): boolean
187187
protected abstract getPagesManifest(): PagesManifest | undefined
@@ -271,7 +271,7 @@ export default abstract class Server {
271271
hostname,
272272
port,
273273
}: Options) {
274-
this.dir = resolve(dir)
274+
this.dir = this.getDir(dir)
275275
this.quiet = quiet
276276
this.loadEnvConfig({ dev })
277277

@@ -280,7 +280,7 @@ export default abstract class Server {
280280
this.nextConfig = conf as NextConfigComplete
281281
this.hostname = hostname
282282
this.port = port
283-
this.distDir = join(this.dir, this.nextConfig.distDir)
283+
this.distDir = this.getDistDir()
284284
this.publicDir = this.getPublicDir()
285285
this.hasStaticDir = !minimalMode && this.getHasStaticDir()
286286

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

353+
const pagesDir = this.getPagesDir()
353354
this.incrementalCache = new IncrementalCache({
354355
fs: this.getCacheFilesystem(),
355356
dev,
356357
distDir: this.distDir,
357-
pagesDir: join(
358-
this.distDir,
359-
this._isLikeServerless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY,
360-
'pages'
361-
),
358+
pagesDir,
362359
locales: this.nextConfig.i18n?.locales,
363360
max: this.nextConfig.experimental.isrMemoryCacheSize,
364361
flushToDisk: !minimalMode && this.nextConfig.experimental.isrFlushToDisk,

packages/next/server/next-server.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,26 @@ export default class NextNodeServer extends BaseServer {
147147
loadEnvConfig(this.dir, dev, Log)
148148
}
149149

150+
protected getDir(_dir: string): string {
151+
return resolve(_dir)
152+
}
153+
150154
protected getPublicDir(): string {
151155
return join(this.dir, CLIENT_PUBLIC_FILES_PATH)
152156
}
153157

158+
protected getDistDir(): string {
159+
return join(this.dir, this.nextConfig.distDir)
160+
}
161+
162+
protected getPagesDir(): string {
163+
return join(
164+
this.distDir,
165+
this._isLikeServerless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY,
166+
'pages'
167+
)
168+
}
169+
154170
protected getHasStaticDir(): boolean {
155171
return fs.existsSync(join(this.dir, 'static'))
156172
}

packages/next/server/render.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { IncomingMessage, ServerResponse } from 'http'
2+
import type { ParsedUrlQuery } from 'querystring'
23
import type { NextRouter } from '../shared/lib/router/router'
34
import type { HtmlProps } from '../shared/lib/html-context'
45
import type { DomainLocale } from './config'
@@ -20,7 +21,7 @@ import type { GetServerSideProps, GetStaticProps, PreviewData } from '../types'
2021
import type { UnwrapPromise } from '../lib/coalesced-function'
2122

2223
import React from 'react'
23-
import { ParsedUrlQuery, stringify as stringifyQuery } from 'querystring'
24+
import { stringify as stringifyQuery } from 'querystring'
2425
import { createFromReadableStream } from 'next/dist/compiled/react-server-dom-webpack'
2526
import { renderToReadableStream } from 'next/dist/compiled/react-server-dom-webpack/writer.browser.server'
2627
import { StyleRegistry, createStyleRegistry } from 'styled-jsx'

packages/next/server/web-server.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ export default class NextWebServer extends BaseServer {
2020

2121
constructor(options: Options & { webServerConfig: WebServerConfig }) {
2222
super(options)
23+
2324
this.webServerConfig = options.webServerConfig
2425
Object.assign(this.renderOpts, options.webServerConfig.extendRenderOpts)
2526
}
27+
2628
protected generateRewrites() {
2729
// @TODO: assuming minimal mode right now
2830
return {
@@ -50,10 +52,19 @@ export default class NextWebServer extends BaseServer {
5052
// @TODO
5153
return ''
5254
}
55+
protected getDir() {
56+
return '.'
57+
}
5358
protected getPublicDir() {
5459
// Public files are not handled by the web server.
5560
return ''
5661
}
62+
protected getDistDir() {
63+
return ''
64+
}
65+
protected getPagesDir() {
66+
return ''
67+
}
5768
protected getBuildId() {
5869
return (globalThis as any).__server_context.buildId
5970
}

test/integration/react-18/app/pages/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ export default function Index() {
1919
</div>
2020
)
2121
}
22+
23+
export const config = {
24+
// runtime: 'edge'
25+
}

test/integration/react-18/test/index.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import webdriver from 'next-webdriver'
2121
const appDir = join(__dirname, '../app')
2222
const nextConfig = new File(join(appDir, 'next.config.js'))
2323
const invalidPage = new File(join(appDir, 'pages/invalid.js'))
24+
const indexPage = new File(join(appDir, 'pages/index.js'))
2425

2526
describe('Basics', () => {
2627
runTests('default setting with react 18', basics)
@@ -67,12 +68,14 @@ function runTestsAgainstRuntime(runtime) {
6768
invalidPage.write(`export const value = 1`)
6869
}
6970
nextConfig.replace("// runtime: 'edge'", `runtime: '${runtime}'`)
71+
indexPage.replace("// runtime: 'edge'", `runtime: '${runtime}'`)
7072
},
7173
afterAll: (env) => {
7274
if (env === 'dev') {
7375
invalidPage.delete()
7476
}
7577
nextConfig.restore()
78+
indexPage.restore()
7679
},
7780
}
7881
)

0 commit comments

Comments
 (0)