Unverified Commit 9a1b756d authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

refactor: simplify middleware execution (#39192)

* refactor: simplify middleware execution

* remove ssr prop from RoutingItem

* fix build
parent dce8c0ce
...@@ -87,7 +87,6 @@ export type FindComponentsResult = { ...@@ -87,7 +87,6 @@ export type FindComponentsResult = {
export interface RoutingItem { export interface RoutingItem {
page: string page: string
match: RouteMatch match: RouteMatch
ssr?: boolean
re?: RegExp re?: RegExp
} }
......
...@@ -387,7 +387,6 @@ export default class DevServer extends Server { ...@@ -387,7 +387,6 @@ export default class DevServer extends Server {
match: getRouteMatcher(middlewareRegex), match: getRouteMatcher(middlewareRegex),
page, page,
re: middlewareRegex.re, re: middlewareRegex.re,
ssr: !isRootMiddleware,
} }
if (isRootMiddleware) { if (isRootMiddleware) {
...@@ -911,17 +910,16 @@ export default class DevServer extends Server { ...@@ -911,17 +910,16 @@ export default class DevServer extends Server {
return undefined return undefined
} }
protected async hasMiddleware( protected async hasMiddleware(): Promise<boolean> {
pathname: string, return this.hasPage(this.actualMiddlewareFile!)
isSSR?: boolean
): Promise<boolean> {
return this.hasPage(isSSR ? pathname : this.actualMiddlewareFile!)
} }
protected async ensureMiddleware(pathname: string, isSSR?: boolean) { protected async ensureMiddleware() {
return this.hotReloader!.ensurePage( return this.hotReloader!.ensurePage(this.actualMiddlewareFile!)
isSSR ? pathname : this.actualMiddlewareFile! }
)
protected async ensureEdgeFunction(pathname: string) {
return this.hotReloader!.ensurePage(pathname)
} }
generateRoutes() { generateRoutes() {
...@@ -979,10 +977,7 @@ export default class DevServer extends Server { ...@@ -979,10 +977,7 @@ export default class DevServer extends Server {
res res
.body( .body(
JSON.stringify( JSON.stringify(
edgeRoutes.map((edgeRoute) => [ edgeRoutes.map((edgeRoute) => [edgeRoute.re!.source])
edgeRoute.re!.source,
!!edgeRoute.ssr,
])
) )
) )
.send() .send()
......
...@@ -1054,14 +1054,15 @@ export default class NextNodeServer extends BaseServer { ...@@ -1054,14 +1054,15 @@ export default class NextNodeServer extends BaseServer {
*/ */
protected getMiddleware(): RoutingItem | undefined { protected getMiddleware(): RoutingItem | undefined {
const manifest = this.getMiddlewareManifest() const manifest = this.getMiddlewareManifest()
if (!manifest) { const rootMiddleware = manifest?.middleware?.['/']
if (!rootMiddleware) {
return return
} }
return manifest.sortedMiddleware.map((page) => ({ return {
match: getMiddlewareMatcher(manifest.middleware[page]), match: getMiddlewareMatcher(rootMiddleware),
page, page: '/',
}))[0] }
} }
protected getEdgeFunctions(): RoutingItem[] { protected getEdgeFunctions(): RoutingItem[] {
...@@ -1139,20 +1140,18 @@ export default class NextNodeServer extends BaseServer { ...@@ -1139,20 +1140,18 @@ export default class NextNodeServer extends BaseServer {
* server where we need to check the filesystem. Here we just check the * server where we need to check the filesystem. Here we just check the
* middleware manifest. * middleware manifest.
*/ */
protected async hasMiddleware( protected async hasMiddleware(pathname: string): Promise<boolean> {
pathname: string,
_isSSR?: boolean
): Promise<boolean> {
const info = this.getEdgeFunctionInfo({ page: pathname, middleware: true }) const info = this.getEdgeFunctionInfo({ page: pathname, middleware: true })
return Boolean(info && info.paths.length > 0) return Boolean(info && info.paths.length > 0)
} }
/** /**
* A placeholder for a function to be defined in the development server. * A placeholder for a function to be defined in the development server.
* It will make sure that the middleware has been compiled so that we * It will make sure that the root middleware or an edge function has been compiled
* can run it. * so that we can run it.
*/ */
protected async ensureMiddleware(_pathname: string, _isSSR?: boolean) {} protected async ensureMiddleware() {}
protected async ensureEdgeFunction(_pathname: string) {}
/** /**
* This method gets all middleware matchers and execute them when the request * This method gets all middleware matchers and execute them when the request
...@@ -1210,18 +1209,20 @@ export default class NextNodeServer extends BaseServer { ...@@ -1210,18 +1209,20 @@ export default class NextNodeServer extends BaseServer {
let result: FetchEventResult | null = null let result: FetchEventResult | null = null
const method = (params.request.method || 'GET').toUpperCase() const method = (params.request.method || 'GET').toUpperCase()
const edgeRoutesList = this.getEdgeRoutes() const middleware = this.getMiddleware()
for (const middleware of edgeRoutesList) { if (!middleware) {
if (middleware.match(normalizedPathname)) { return { finished: false }
if (!(await this.hasMiddleware(middleware.page, middleware.ssr))) { }
if (!(await this.hasMiddleware(middleware.page))) {
console.warn(`The Edge Function for ${middleware.page} was not found`) console.warn(`The Edge Function for ${middleware.page} was not found`)
continue return { finished: false }
} }
await this.ensureMiddleware(middleware.page, middleware.ssr) if (middleware && middleware.match(normalizedPathname)) {
await this.ensureMiddleware()
const middlewareInfo = this.getEdgeFunctionInfo({ const middlewareInfo = this.getEdgeFunctionInfo({
page: middleware.page, page: middleware.page,
middleware: !middleware.ssr, middleware: true,
}) })
if (!middlewareInfo) { if (!middlewareInfo) {
...@@ -1261,11 +1262,6 @@ export default class NextNodeServer extends BaseServer { ...@@ -1261,11 +1262,6 @@ export default class NextNodeServer extends BaseServer {
console.error(`Uncaught: middleware waitUntil errored`, error) console.error(`Uncaught: middleware waitUntil errored`, error)
}) })
} }
if (!result.response.headers.has('x-middleware-next')) {
break
}
}
} }
if (!result) { if (!result) {
...@@ -1554,7 +1550,7 @@ export default class NextNodeServer extends BaseServer { ...@@ -1554,7 +1550,7 @@ export default class NextNodeServer extends BaseServer {
let middlewareInfo: ReturnType<typeof this.getEdgeFunctionInfo> | undefined let middlewareInfo: ReturnType<typeof this.getEdgeFunctionInfo> | undefined
try { try {
await this.ensureMiddleware(params.page, true) await this.ensureEdgeFunction(params.page)
middlewareInfo = this.getEdgeFunctionInfo({ middlewareInfo = this.getEdgeFunctionInfo({
page: params.page, page: params.page,
middleware: false, middleware: false,
......
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