Skip to content

Commit

Permalink
Simplify getMiddlewareInfo calls
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Jan 21, 2022
1 parent 8959292 commit a0b2c4b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 37 deletions.
30 changes: 6 additions & 24 deletions packages/next/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
PRERENDER_MANIFEST,
ROUTES_MANIFEST,
SERVERLESS_DIRECTORY,
Expand Down Expand Up @@ -139,9 +138,7 @@ export default abstract class Server {
protected nextConfig: NextConfigComplete
protected distDir: string
protected pagesDir?: string
protected publicDir: string
protected hasStaticDir: boolean
protected serverBuildDir: string
protected pagesManifest?: PagesManifest
protected buildId: string
protected minimalMode: boolean
Expand Down Expand Up @@ -204,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
Expand Down Expand Up @@ -294,9 +290,7 @@ 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.hasStaticDir = !minimalMode && this.getHasStaticDir()

// Only serverRuntimeConfig needs the default
Expand Down Expand Up @@ -351,11 +345,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()

Expand Down Expand Up @@ -659,14 +648,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
Expand Down
32 changes: 19 additions & 13 deletions packages/next/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {
MIDDLEWARE_MANIFEST,
CLIENT_STATIC_FILES_PATH,
CLIENT_STATIC_FILES_RUNTIME,
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'
Expand Down Expand Up @@ -84,6 +86,9 @@ export interface NodeRequestHandler {
}

export default class NextNodeServer extends BaseServer {
protected publicDir: string
protected serverBuildDir: string

constructor(options: Options) {
super(options)
/**
Expand All @@ -101,6 +106,12 @@ export default class NextNodeServer extends BaseServer {
if (this.renderOpts.optimizeCss) {
process.env.__NEXT_OPTIMIZE_CSS = JSON.stringify(true)
}

this.publicDir = join(this.dir, CLIENT_PUBLIC_FILES_PATH)
this.serverBuildDir = join(
this.distDir,
this._isLikeServerless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY
)
}

private compression =
Expand Down Expand Up @@ -759,13 +770,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 {
Expand Down Expand Up @@ -1010,12 +1021,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,
Expand Down

0 comments on commit a0b2c4b

Please sign in to comment.