diff --git a/packages/next/server/dev/next-dev-server.ts b/packages/next/server/dev/next-dev-server.ts index 2d90aefefcd2b..39c5d1669c21d 100644 --- a/packages/next/server/dev/next-dev-server.ts +++ b/packages/next/server/dev/next-dev-server.ts @@ -696,6 +696,13 @@ export default class DevServer extends Server { }) } + protected getPagesManifest(): undefined { + return undefined + } + protected getMiddlewareManifest(): undefined { + return undefined + } + protected getMiddleware(): never[] { return [] } diff --git a/packages/next/server/next-server.ts b/packages/next/server/next-server.ts index 698ad607a5115..e7f7a5e99d666 100644 --- a/packages/next/server/next-server.ts +++ b/packages/next/server/next-server.ts @@ -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 @@ -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()) @@ -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 ( @@ -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 ? [ @@ -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, '/')) diff --git a/packages/next/server/web/next-web-server.ts b/packages/next/server/web/next-web-server.ts new file mode 100644 index 0000000000000..6753e095884f5 --- /dev/null +++ b/packages/next/server/web/next-web-server.ts @@ -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 + } +}