Skip to content

Commit

Permalink
move getMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
shuding committed Jan 19, 2022
1 parent 09b4e4a commit 03b0c4e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
21 changes: 8 additions & 13 deletions packages/next/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import {
getRouteRegex,
getSortedRoutes,
isDynamicRoute,
getMiddlewareRegex,
} from '../shared/lib/router/utils'
import * as envConfig from '../shared/lib/runtime-config'
import { DecodeError, normalizeRepeatedSlashes } from '../shared/lib/utils'
Expand Down Expand Up @@ -193,6 +192,14 @@ export default abstract class Server {
protected abstract generateImageRoutes(): Route[]
protected abstract generateCatchAllMiddlewareRoute(): Route | undefined
protected abstract getFilesystemPaths(): Set<string>
protected abstract getMiddleware(): {
match: (pathname: string | null | undefined) =>
| false
| {
[paramName: string]: string | string[]
}
page: string
}[]
protected abstract findPageComponents(
pathname: string,
query?: NextParsedUrlQuery,
Expand Down Expand Up @@ -668,18 +675,6 @@ export default abstract class Server {
return this.getPrerenderManifest().preview
}

protected getMiddleware() {
const middleware = this.middlewareManifest?.middleware || {}
return (
this.middlewareManifest?.sortedMiddleware.map((page) => ({
match: getRouteMatcher(
getMiddlewareRegex(page, MIDDLEWARE_ROUTE.test(middleware[page].name))
),
page,
})) || []
)
}

protected async hasMiddleware(
pathname: string,
_isSSR?: boolean
Expand Down
14 changes: 14 additions & 0 deletions packages/next/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ import { relativizeURL } from '../shared/lib/router/utils/relativize-url'
import { parseNextUrl } from '../shared/lib/router/utils/parse-next-url'
import { prepareDestination } from '../shared/lib/router/utils/prepare-destination'
import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
import { getMiddlewareRegex, getRouteMatcher } from '../shared/lib/router/utils'
import { MIDDLEWARE_ROUTE } from '../lib/constants'

export * from './base-server'

Expand Down Expand Up @@ -771,6 +773,18 @@ export default class NextNodeServer extends BaseServer {
}
}

protected getMiddleware() {
const middleware = this.middlewareManifest?.middleware || {}
return (
this.middlewareManifest?.sortedMiddleware.map((page) => ({
match: getRouteMatcher(
getMiddlewareRegex(page, MIDDLEWARE_ROUTE.test(middleware[page].name))
),
page,
})) || []
)
}

private middlewareBetaWarning = execOnce(() => {
Log.warn(
`using beta Middleware (not covered by semver) - https://nextjs.org/docs/messages/beta-middleware`
Expand Down

0 comments on commit 03b0c4e

Please sign in to comment.