@@ -5,7 +5,14 @@ import { RouteKind } from '../future/route-kind'
5
5
import { DefaultRouteMatcherManager } from '../future/route-matcher-managers/default-route-matcher-manager'
6
6
import { RouteMatch } from '../future/route-matches/route-match'
7
7
import type { PageChecker , Route } from '../router'
8
+ import { getMiddlewareMatchers } from '../../build/analysis/get-page-static-info'
9
+ import { getMiddlewareRouteMatcher } from '../../shared/lib/router/utils/middleware-route-matcher'
10
+ import { join } from 'path'
8
11
12
+ type MiddlewareConfig = {
13
+ matcher : string [ ]
14
+ files : string [ ]
15
+ }
9
16
type RouteResult =
10
17
| {
11
18
type : 'rewrite'
@@ -48,7 +55,11 @@ class DevRouteMatcherManager extends DefaultRouteMatcherManager {
48
55
}
49
56
}
50
57
51
- export async function makeResolver ( dir : string , nextConfig : NextConfig ) {
58
+ export async function makeResolver (
59
+ dir : string ,
60
+ nextConfig : NextConfig ,
61
+ middleware : MiddlewareConfig
62
+ ) {
52
63
const url = require ( 'url' ) as typeof import ( 'url' )
53
64
const { default : Router } = require ( '../router' ) as typeof import ( '../router' )
54
65
const { getPathMatch } =
@@ -65,14 +76,57 @@ export async function makeResolver(dir: string, nextConfig: NextConfig) {
65
76
const devServer = new DevServer ( {
66
77
dir,
67
78
conf : nextConfig ,
79
+ hostname : 'localhost' ,
80
+ port : 3000 ,
68
81
} )
82
+
69
83
await devServer . matchers . reload ( )
70
84
71
- // @ts -expect-error
72
- devServer . customRoutes = await loadCustomRoutes ( nextConfig )
85
+ if ( middleware . files ?. length ) {
86
+ // @ts -expect-error
87
+ devServer . customRoutes = await loadCustomRoutes ( nextConfig )
88
+
89
+ const matchers = middleware . matcher
90
+ ? getMiddlewareMatchers ( middleware . matcher , nextConfig )
91
+ : [ { regexp : '.*' } ]
92
+ // @ts -expect-error
93
+ devServer . middleware = {
94
+ page : '/' ,
95
+ match : getMiddlewareRouteMatcher ( matchers ) ,
96
+ matchers,
97
+ }
98
+
99
+ type GetEdgeFunctionInfo =
100
+ typeof DevServer [ 'prototype' ] [ 'getEdgeFunctionInfo' ]
101
+ const getEdgeFunctionInfo = (
102
+ original : GetEdgeFunctionInfo
103
+ ) : GetEdgeFunctionInfo => {
104
+ return ( params : { page : string ; middleware : boolean } ) => {
105
+ if ( params . middleware ) {
106
+ return {
107
+ name : 'middleware' ,
108
+ paths : middleware . files . map ( ( file ) => join ( process . cwd ( ) , file ) ) ,
109
+ env : [ ] ,
110
+ wasm : [ ] ,
111
+ assets : [ ] ,
112
+ }
113
+ }
114
+ return original ( params )
115
+ }
116
+ }
117
+ // @ts -expect-error protected
118
+ devServer . getEdgeFunctionInfo = getEdgeFunctionInfo (
119
+ // @ts -expect-error protected
120
+ devServer . getEdgeFunctionInfo . bind ( devServer )
121
+ )
122
+ // @ts -expect-error protected
123
+ devServer . hasMiddleware = ( ) => true
124
+ }
73
125
74
126
const routeResults = new WeakMap < any , string > ( )
75
- const routes = devServer . generateRoutes . bind ( devServer ) ( )
127
+ const routes = devServer . generateRoutes ( )
128
+ // @ts -expect-error protected
129
+ const catchAllMiddleware = devServer . generateCatchAllMiddlewareRoute ( true )
76
130
77
131
routes . matchers = new DevRouteMatcherManager (
78
132
// @ts -expect-error internal method
@@ -81,6 +135,7 @@ export async function makeResolver(dir: string, nextConfig: NextConfig) {
81
135
82
136
const router = new Router ( {
83
137
...routes ,
138
+ catchAllMiddleware,
84
139
catchAllRoute : {
85
140
match : getPathMatch ( '/:path*' ) ,
86
141
name : 'catchall route' ,
@@ -112,6 +167,7 @@ export async function makeResolver(dir: string, nextConfig: NextConfig) {
112
167
route . type === 'redirect' ||
113
168
route . type === 'header' ||
114
169
route . name === 'catchall route' ||
170
+ route . name === 'middleware catchall' ||
115
171
route . name ?. includes ( 'check' )
116
172
return matches
117
173
} )
@@ -122,9 +178,12 @@ export async function makeResolver(dir: string, nextConfig: NextConfig) {
122
178
) {
123
179
const req = new NodeNextRequest ( _req )
124
180
const res = new NodeNextResponse ( _res )
181
+ const parsedUrl = url . parse ( req . url ! , true )
182
+ // @ts -expect-error protected
183
+ devServer . attachRequestMeta ( req , parsedUrl )
125
184
; ( req as any ) . _initUrl = req . url
126
185
127
- await router . execute . bind ( router ) ( req , res , url . parse ( req . url ! , true ) )
186
+ await router . execute ( req , res , parsedUrl )
128
187
129
188
if ( ! res . originalResponse . headersSent ) {
130
189
res . setHeader ( 'x-nextjs-route-result' , '1' )
0 commit comments