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' ...@@ -26,7 +26,6 @@ import { parse as parseQs, stringify as stringifyQs } from 'querystring'
import { format as formatUrl, parse as parseUrl } from 'url' import { format as formatUrl, parse as parseUrl } from 'url'
import { getRedirectStatus, modifyRouteRegex } from '../lib/load-custom-routes' import { getRedirectStatus, modifyRouteRegex } from '../lib/load-custom-routes'
import { import {
CLIENT_PUBLIC_FILES_PATH,
SERVERLESS_DIRECTORY, SERVERLESS_DIRECTORY,
SERVER_DIRECTORY, SERVER_DIRECTORY,
STATIC_STATUS_PAGES, STATIC_STATUS_PAGES,
...@@ -139,7 +138,6 @@ export default abstract class Server { ...@@ -139,7 +138,6 @@ export default abstract class Server {
protected pagesDir?: string protected pagesDir?: string
protected publicDir: string protected publicDir: string
protected hasStaticDir: boolean protected hasStaticDir: boolean
protected serverBuildDir: string
protected pagesManifest?: PagesManifest protected pagesManifest?: PagesManifest
protected buildId: string protected buildId: string
protected minimalMode: boolean protected minimalMode: boolean
...@@ -180,6 +178,7 @@ export default abstract class Server { ...@@ -180,6 +178,7 @@ export default abstract class Server {
public readonly hostname?: string public readonly hostname?: string
public readonly port?: number public readonly port?: number
protected abstract getPublicDir(): string
protected abstract getHasStaticDir(): boolean protected abstract getHasStaticDir(): boolean
protected abstract getPagesManifest(): PagesManifest | undefined protected abstract getPagesManifest(): PagesManifest | undefined
protected abstract getBuildId(): string protected abstract getBuildId(): string
...@@ -202,12 +201,11 @@ export default abstract class Server { ...@@ -202,12 +201,11 @@ export default abstract class Server {
query?: NextParsedUrlQuery, query?: NextParsedUrlQuery,
params?: Params | null params?: Params | null
): Promise<FindComponentsResult | null> ): Promise<FindComponentsResult | null>
protected abstract getMiddlewareInfo(params: { protected abstract getMiddlewareInfo(page: string): {
dev?: boolean name: string
distDir: string paths: string[]
page: string env: string[]
serverless: boolean }
}): { name: string; paths: string[]; env: string[] }
protected abstract getPagePath(pathname: string, locales?: string[]): string protected abstract getPagePath(pathname: string, locales?: string[]): string
protected abstract getFontManifest(): FontManifest | undefined protected abstract getFontManifest(): FontManifest | undefined
protected abstract getMiddlewareManifest(): MiddlewareManifest | undefined protected abstract getMiddlewareManifest(): MiddlewareManifest | undefined
...@@ -294,9 +292,8 @@ export default abstract class Server { ...@@ -294,9 +292,8 @@ export default abstract class Server {
this.nextConfig = conf as NextConfigComplete this.nextConfig = conf as NextConfigComplete
this.hostname = hostname this.hostname = hostname
this.port = port this.port = port
this.distDir = join(this.dir, this.nextConfig.distDir) 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() this.hasStaticDir = !minimalMode && this.getHasStaticDir()
// Only serverRuntimeConfig needs the default // Only serverRuntimeConfig needs the default
...@@ -351,11 +348,6 @@ export default abstract class Server { ...@@ -351,11 +348,6 @@ export default abstract class Server {
publicRuntimeConfig, publicRuntimeConfig,
}) })
this.serverBuildDir = join(
this.distDir,
this._isLikeServerless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY
)
this.pagesManifest = this.getPagesManifest() this.pagesManifest = this.getPagesManifest()
this.middlewareManifest = this.getMiddlewareManifest() this.middlewareManifest = this.getMiddlewareManifest()
...@@ -650,14 +642,7 @@ export default abstract class Server { ...@@ -650,14 +642,7 @@ export default abstract class Server {
_isSSR?: boolean _isSSR?: boolean
): Promise<boolean> { ): Promise<boolean> {
try { try {
return ( return this.getMiddlewareInfo(pathname).paths.length > 0
this.getMiddlewareInfo({
dev: this.renderOpts.dev,
distDir: this.distDir,
page: pathname,
serverless: this._isLikeServerless,
}).paths.length > 0
)
} catch (_) {} } catch (_) {}
return false return false
......
...@@ -27,6 +27,8 @@ import { ...@@ -27,6 +27,8 @@ import {
CLIENT_STATIC_FILES_RUNTIME, CLIENT_STATIC_FILES_RUNTIME,
PRERENDER_MANIFEST, PRERENDER_MANIFEST,
ROUTES_MANIFEST, ROUTES_MANIFEST,
CLIENT_PUBLIC_FILES_PATH,
SERVERLESS_DIRECTORY,
} from '../shared/lib/constants' } from '../shared/lib/constants'
import { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin' import { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
import { recursiveReadDirSync } from './lib/recursive-readdir-sync' import { recursiveReadDirSync } from './lib/recursive-readdir-sync'
...@@ -115,12 +117,20 @@ export default class NextNodeServer extends BaseServer { ...@@ -115,12 +117,20 @@ export default class NextNodeServer extends BaseServer {
loadEnvConfig(this.dir, dev, Log) loadEnvConfig(this.dir, dev, Log)
} }
protected getPublicDir(): string {
return join(this.dir, CLIENT_PUBLIC_FILES_PATH)
}
protected getHasStaticDir(): boolean { protected getHasStaticDir(): boolean {
return fs.existsSync(join(this.dir, 'static')) return fs.existsSync(join(this.dir, 'static'))
} }
protected getPagesManifest(): PagesManifest | undefined { 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) return require(pagesManifestPath)
} }
...@@ -762,13 +772,13 @@ export default class NextNodeServer extends BaseServer { ...@@ -762,13 +772,13 @@ export default class NextNodeServer extends BaseServer {
return filesystemUrls.has(resolved) return filesystemUrls.has(resolved)
} }
protected getMiddlewareInfo(params: { protected getMiddlewareInfo(page: string) {
dev?: boolean return getMiddlewareInfo({
distDir: string dev: this.renderOpts.dev,
page: string page,
serverless: boolean distDir: this.distDir,
}) { serverless: this._isLikeServerless,
return getMiddlewareInfo(params) })
} }
protected getMiddlewareManifest(): MiddlewareManifest | undefined { protected getMiddlewareManifest(): MiddlewareManifest | undefined {
...@@ -1013,12 +1023,7 @@ export default class NextNodeServer extends BaseServer { ...@@ -1013,12 +1023,7 @@ export default class NextNodeServer extends BaseServer {
await this.ensureMiddleware(middleware.page, middleware.ssr) await this.ensureMiddleware(middleware.page, middleware.ssr)
const middlewareInfo = this.getMiddlewareInfo({ const middlewareInfo = this.getMiddlewareInfo(middleware.page)
dev: this.renderOpts.dev,
distDir: this.distDir,
page: middleware.page,
serverless: this._isLikeServerless,
})
result = await run({ result = await run({
name: middlewareInfo.name, 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