@@ -1455,13 +1455,13 @@ export default class NextNodeServer extends BaseServer<
1455
1455
const middlewareModule = await this . loadNodeMiddleware ( )
1456
1456
1457
1457
if ( middlewareModule ) {
1458
+ const matchers = middlewareModule . config ?. matchers || [
1459
+ { regexp : '.*' , originalSource : '/:path*' } ,
1460
+ ]
1458
1461
return {
1459
- match : getMiddlewareRouteMatcher (
1460
- middlewareModule . config ?. matchers || [
1461
- { regexp : '.*' , originalSource : '/:path*' } ,
1462
- ]
1463
- ) ,
1462
+ match : getMiddlewareRouteMatcher ( matchers ) ,
1464
1463
page : '/' ,
1464
+ matchers,
1465
1465
}
1466
1466
}
1467
1467
@@ -1471,6 +1471,7 @@ export default class NextNodeServer extends BaseServer<
1471
1471
return {
1472
1472
match : getMiddlewareMatcher ( middleware ) ,
1473
1473
page : '/' ,
1474
+ matchers : middleware . matchers ,
1474
1475
}
1475
1476
}
1476
1477
@@ -1615,6 +1616,17 @@ export default class NextNodeServer extends BaseServer<
1615
1616
url ?: string
1616
1617
} ) { }
1617
1618
1619
+ protected isInternalRequest ( pathname : string ) : boolean {
1620
+ const { basePath } = this . nextConfig
1621
+
1622
+ let checkPath = pathname
1623
+ if ( basePath && pathname . startsWith ( basePath ) ) {
1624
+ checkPath = pathname . slice ( basePath . length )
1625
+ }
1626
+
1627
+ return checkPath . startsWith ( '/_next/' )
1628
+ }
1629
+
1618
1630
/**
1619
1631
* This method gets all middleware matchers and execute them when the request
1620
1632
* matches. It will make sure that each middleware exists and is compiled and
@@ -1679,6 +1691,18 @@ export default class NextNodeServer extends BaseServer<
1679
1691
return { finished : false }
1680
1692
}
1681
1693
1694
+ // Skip middleware for internal routes when using default catch-all matcher
1695
+ if (
1696
+ this . isInternalRequest ( params . parsedUrl . pathname ) &&
1697
+ middleware . matchers ?. length === 1 &&
1698
+ middleware . matchers [ 0 ] . originalSource === '/:path*' &&
1699
+ // TODO: Can we align the default matcher to be either `^/.*$` or `.*`?
1700
+ ( middleware . matchers [ 0 ] . regexp === '^/.*$' ||
1701
+ middleware . matchers [ 0 ] . regexp === '.*' )
1702
+ ) {
1703
+ return { finished : false }
1704
+ }
1705
+
1682
1706
await this . ensureMiddleware ( params . request . url )
1683
1707
const middlewareInfo = this . getEdgeFunctionInfo ( {
1684
1708
page : middleware . page ,
0 commit comments