Unverified Commit 557a9725 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Simplify getMiddlewareInfo calls (#33542)

Subtask of #31506

* move `serverBuildDir` and getter function of `publicDir` to next-server as only used place
* simplify `getMiddlewareInfo`, remove `distDir` and other params could be accessed from web server itself
parent 76b9a549
......@@ -26,7 +26,6 @@ import { parse as parseQs, stringify as stringifyQs } from 'querystring'
import { format as formatUrl, parse as parseUrl } from 'url'
import { getRedirectStatus, modifyRouteRegex } from '../lib/load-custom-routes'
import {
CLIENT_PUBLIC_FILES_PATH,
SERVERLESS_DIRECTORY,
SERVER_DIRECTORY,
STATIC_STATUS_PAGES,
......@@ -139,7 +138,6 @@ export default abstract class Server {
protected pagesDir?: string
protected publicDir: string
protected hasStaticDir: boolean
protected serverBuildDir: string
protected pagesManifest?: PagesManifest
protected buildId: string
protected minimalMode: boolean
......@@ -180,6 +178,7 @@ export default abstract class Server {
public readonly hostname?: string
public readonly port?: number
protected abstract getPublicDir(): string
protected abstract getHasStaticDir(): boolean
protected abstract getPagesManifest(): PagesManifest | undefined
protected abstract getBuildId(): string
......@@ -202,12 +201,11 @@ export default abstract class Server {
query?: NextParsedUrlQuery,
params?: Params | null
): Promise<FindComponentsResult | null>
protected abstract getMiddlewareInfo(params: {
dev?: boolean
distDir: string
page: string
serverless: boolean
}): { name: string; paths: string[]; env: string[] }
protected abstract getMiddlewareInfo(page: string): {
name: string
paths: string[]
env: string[]
}
protected abstract getPagePath(pathname: string, locales?: string[]): string
protected abstract getFontManifest(): FontManifest | undefined
protected abstract getMiddlewareManifest(): MiddlewareManifest | undefined
......@@ -294,9 +292,8 @@ 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.publicDir = join(this.dir, CLIENT_PUBLIC_FILES_PATH)
this.publicDir = this.getPublicDir()
this.hasStaticDir = !minimalMode && this.getHasStaticDir()
// Only serverRuntimeConfig needs the default
......@@ -351,11 +348,6 @@ export default abstract class Server {
publicRuntimeConfig,
})
this.serverBuildDir = join(
this.distDir,
this._isLikeServerless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY
)
this.pagesManifest = this.getPagesManifest()
this.middlewareManifest = this.getMiddlewareManifest()
......@@ -650,14 +642,7 @@ export default abstract class Server {
_isSSR?: boolean
): Promise<boolean> {
try {
return (
this.getMiddlewareInfo({
dev: this.renderOpts.dev,
distDir: this.distDir,
page: pathname,
serverless: this._isLikeServerless,
}).paths.length > 0
)
return this.getMiddlewareInfo(pathname).paths.length > 0
} catch (_) {}
return false
......
......@@ -27,6 +27,8 @@ import {
CLIENT_STATIC_FILES_RUNTIME,
PRERENDER_MANIFEST,
ROUTES_MANIFEST,
CLIENT_PUBLIC_FILES_PATH,
SERVERLESS_DIRECTORY,
} from '../shared/lib/constants'
import { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
import { recursiveReadDirSync } from './lib/recursive-readdir-sync'
......@@ -115,12 +117,20 @@ export default class NextNodeServer extends BaseServer {
loadEnvConfig(this.dir, dev, Log)
}
protected getPublicDir(): string {
return join(this.dir, CLIENT_PUBLIC_FILES_PATH)
}
protected getHasStaticDir(): boolean {
return fs.existsSync(join(this.dir, 'static'))
}
protected getPagesManifest(): PagesManifest | undefined {
const pagesManifestPath = join(this.serverBuildDir, PAGES_MANIFEST)
const serverBuildDir = join(
this.distDir,
this._isLikeServerless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY
)
const pagesManifestPath = join(serverBuildDir, PAGES_MANIFEST)
return require(pagesManifestPath)
}
......@@ -762,13 +772,13 @@ export default class NextNodeServer extends BaseServer {
return filesystemUrls.has(resolved)
}
protected getMiddlewareInfo(params: {
dev?: boolean
distDir: string
page: string
serverless: boolean
}) {
return getMiddlewareInfo(params)
protected getMiddlewareInfo(page: string) {
return getMiddlewareInfo({
dev: this.renderOpts.dev,
page,
distDir: this.distDir,
serverless: this._isLikeServerless,
})
}
protected getMiddlewareManifest(): MiddlewareManifest | undefined {
......@@ -1013,12 +1023,7 @@ export default class NextNodeServer extends BaseServer {
await this.ensureMiddleware(middleware.page, middleware.ssr)
const middlewareInfo = this.getMiddlewareInfo({
dev: this.renderOpts.dev,
distDir: this.distDir,
page: middleware.page,
serverless: this._isLikeServerless,
})
const middlewareInfo = this.getMiddlewareInfo(middleware.page)
result = await run({
name: middlewareInfo.name,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment