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

Make NextServer runtime agnostic #31971

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/next/server/dev/next-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,13 @@ export default class DevServer extends Server {
})
}

protected getPagesManifest(): undefined {
return undefined
}
protected getMiddlewareManifest(): undefined {
return undefined
}

protected getMiddleware(): never[] {
return []
}
Expand Down
41 changes: 26 additions & 15 deletions packages/next/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export default class Server {

this.distDir = join(this.dir, this.nextConfig.distDir)
this.publicDir = join(this.dir, CLIENT_PUBLIC_FILES_PATH)
this.hasStaticDir = !minimalMode && fs.existsSync(join(this.dir, 'static'))
this.hasStaticDir = !minimalMode && this.getHasStaticDir()

// Only serverRuntimeConfig needs the default
// publicRuntimeConfig gets it's default in client/index.js
Expand Down Expand Up @@ -278,18 +278,9 @@ export default class Server {
this.distDir,
this._isLikeServerless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY
)
const pagesManifestPath = join(this.serverBuildDir, PAGES_MANIFEST)
const middlewareManifestPath = join(
join(this.distDir, SERVER_DIRECTORY),
MIDDLEWARE_MANIFEST
)

if (!dev) {
this.pagesManifest = require(pagesManifestPath)
if (!this.minimalMode) {
this.middlewareManifest = require(middlewareManifestPath)
}
}
this.pagesManifest = this.getPagesManifest()
this.middlewareManifest = this.getMiddlewareManifest()

this.customRoutes = this.getCustomRoutes()
this.router = new Router(this.generateRoutes())
Expand Down Expand Up @@ -589,6 +580,22 @@ export default class Server {
return this.getPrerenderManifest().preview
}

protected getPagesManifest(): PagesManifest | undefined {
const pagesManifestPath = join(this.serverBuildDir, PAGES_MANIFEST)
return require(pagesManifestPath)
}

protected getMiddlewareManifest(): MiddlewareManifest | undefined {
if (!this.minimalMode) {
const middlewareManifestPath = join(
join(this.distDir, SERVER_DIRECTORY),
MIDDLEWARE_MANIFEST
)
return require(middlewareManifestPath)
}
return undefined
}

protected getMiddleware() {
const middleware = this.middlewareManifest?.middleware || {}
return (
Expand Down Expand Up @@ -749,9 +756,7 @@ export default class Server {
locales: string[]
} {
const server: Server = this
const publicRoutes = fs.existsSync(this.publicDir)
? this.generatePublicRoutes()
: []
const publicRoutes = this.generatePublicRoutes()

const staticFilesRoute = this.hasStaticDir
? [
Expand Down Expand Up @@ -1492,7 +1497,13 @@ export default class Server {
return true
}

protected getHasStaticDir(): boolean {
return fs.existsSync(join(this.dir, 'static'))
}

protected generatePublicRoutes(): Route[] {
if (!fs.existsSync(this.publicDir)) return []

const publicFiles = new Set(
recursiveReadDirSync(this.publicDir).map((p) =>
encodeURI(p.replace(/\\/g, '/'))
Expand Down
37 changes: 37 additions & 0 deletions packages/next/server/web/next-web-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Server, { ServerConstructor } from '../next-server'
import { NextConfig } from '../config'
import { PagesManifest } from '../../build/webpack/plugins/pages-manifest-plugin'
import { Route } from '../router'

type WebRuntimeConfig = {
buildId: string
pagesManifest: PagesManifest
}

export default class WebServer extends Server {
protected runtimeConfig: WebRuntimeConfig

constructor(
options: ServerConstructor & {
conf: NextConfig
runtimeConfig: WebRuntimeConfig
}
) {
super(options)
this.runtimeConfig = options.runtimeConfig
}

protected generatePublicRoutes(): Route[] {
return []
}
protected getHasStaticDir(): boolean {
return false
}
protected readBuildId(): string {
return this.runtimeConfig.buildId
}
// The web runtime doesn't need to serve any static files
protected isServeableUrl(): boolean {
return false
}
}