@@ -5,6 +5,7 @@ import type {
5
5
import type { EdgeSSRMeta } from '../loaders/get-module-build-info'
6
6
import type { MiddlewareMatcher } from '../../analysis/get-page-static-info'
7
7
import { getNamedMiddlewareRegex } from '../../../shared/lib/router/utils/route-regex'
8
+ import { getDefaultMiddlewareMatcher } from '../../../shared/lib/router/utils/get-default-middleware-matcher'
8
9
import { getModuleBuildInfo } from '../loaders/get-module-build-info'
9
10
import { getSortedRoutes } from '../../../shared/lib/router/utils'
10
11
import { webpack , sources } from 'next/dist/compiled/webpack/webpack'
@@ -36,6 +37,7 @@ import type { CustomRoutes } from '../../../lib/load-custom-routes'
36
37
import { isInterceptionRouteRewrite } from '../../../lib/generate-interception-routes-rewrites'
37
38
import { getDynamicCodeEvaluationError } from './wellknown-errors-plugin/parse-dynamic-code-evaluation-error'
38
39
import { getModuleReferencesInOrder } from '../utils'
40
+ import type { NextConfigComplete } from '../../../server/config-shared'
39
41
40
42
const KNOWN_SAFE_DYNAMIC_PACKAGES =
41
43
require ( '../../../lib/known-edge-safe-packages.json' ) as string [ ]
@@ -195,21 +197,29 @@ function getCreateAssets(params: {
195
197
continue
196
198
}
197
199
198
- const matcherSource = metadata . edgeSSR ?. isAppDir
199
- ? normalizeAppPath ( page )
200
- : page
201
-
202
- const catchAll = ! metadata . edgeSSR && ! metadata . edgeApiFunction
203
-
204
- const { namedRegex } = getNamedMiddlewareRegex ( matcherSource , {
205
- catchAll,
206
- } )
207
- const matchers = metadata ?. edgeMiddleware ?. matchers ?? [
208
- {
209
- regexp : namedRegex ,
210
- originalSource : page === '/' && catchAll ? '/:path*' : matcherSource ,
211
- } ,
212
- ]
200
+ let matchers : MiddlewareMatcher [ ]
201
+ if ( metadata ?. edgeMiddleware ?. matchers ) {
202
+ matchers = metadata . edgeMiddleware . matchers
203
+ } else {
204
+ // For middleware at root with no explicit matchers, use getDefaultMiddlewareMatcher
205
+ // which respects skipMiddlewareNextInternalRoutes config
206
+ const catchAll = ! metadata . edgeSSR && ! metadata . edgeApiFunction
207
+ if ( page === '/' && catchAll ) {
208
+ matchers = [ getDefaultMiddlewareMatcher ( opts . nextConfig ) ]
209
+ } else {
210
+ const matcherSource = metadata . edgeSSR ?. isAppDir
211
+ ? normalizeAppPath ( page )
212
+ : page
213
+ matchers = [
214
+ {
215
+ regexp : getNamedMiddlewareRegex ( matcherSource , {
216
+ catchAll,
217
+ } ) . namedRegex ,
218
+ originalSource : matcherSource ,
219
+ } ,
220
+ ]
221
+ }
222
+ }
213
223
214
224
const isEdgeFunction = ! ! ( metadata . edgeApiFunction || metadata . edgeSSR )
215
225
const edgeFunctionDefinition : EdgeFunctionDefinition = {
@@ -818,19 +828,28 @@ interface Options {
818
828
sriEnabled : boolean
819
829
rewrites : CustomRoutes [ 'rewrites' ]
820
830
edgeEnvironments : EdgeRuntimeEnvironments
831
+ nextConfig : NextConfigComplete
821
832
}
822
833
823
834
export default class MiddlewarePlugin {
824
835
private readonly dev : Options [ 'dev' ]
825
836
private readonly sriEnabled : Options [ 'sriEnabled' ]
826
837
private readonly rewrites : Options [ 'rewrites' ]
827
838
private readonly edgeEnvironments : EdgeRuntimeEnvironments
828
-
829
- constructor ( { dev, sriEnabled, rewrites, edgeEnvironments } : Options ) {
839
+ private readonly nextConfig : Options [ 'nextConfig' ]
840
+
841
+ constructor ( {
842
+ dev,
843
+ sriEnabled,
844
+ rewrites,
845
+ edgeEnvironments,
846
+ nextConfig,
847
+ } : Options ) {
830
848
this . dev = dev
831
849
this . sriEnabled = sriEnabled
832
850
this . rewrites = rewrites
833
851
this . edgeEnvironments = edgeEnvironments
852
+ this . nextConfig = nextConfig
834
853
}
835
854
836
855
public apply ( compiler : webpack . Compiler ) {
@@ -886,6 +905,7 @@ export default class MiddlewarePlugin {
886
905
rewrites : this . rewrites ,
887
906
edgeEnvironments : this . edgeEnvironments ,
888
907
dev : this . dev ,
908
+ nextConfig : this . nextConfig ,
889
909
} ,
890
910
} )
891
911
)
0 commit comments