Skip to content

Commit fd34618

Browse files
committed
Skip internal routes in middleware
1 parent c388db4 commit fd34618

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

packages/next/src/server/config-schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
676676
serverExternalPackages: z.array(z.string()).optional(),
677677
serverRuntimeConfig: z.record(z.string(), z.any()).optional(),
678678
skipMiddlewareUrlNormalize: z.boolean().optional(),
679+
skipMiddlewareNextInternalRoutes: z.boolean().optional(),
679680
skipTrailingSlashRedirect: z.boolean().optional(),
680681
staticPageGenerationTimeout: z.number().optional(),
681682
expireTime: z.number().optional(),

packages/next/src/server/config-shared.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,12 @@ export interface NextConfig {
12361236

12371237
skipMiddlewareUrlNormalize?: boolean
12381238

1239+
/**
1240+
* Skip Next.js internals route `/_next` from middleware.
1241+
* @default true
1242+
*/
1243+
skipMiddlewareNextInternalRoutes?: boolean
1244+
12391245
skipTrailingSlashRedirect?: boolean
12401246

12411247
modularizeImports?: Record<
@@ -1515,6 +1521,7 @@ export const defaultConfig = Object.freeze({
15151521
},
15161522
htmlLimitedBots: undefined,
15171523
bundlePagesRouterDependencies: false,
1524+
skipMiddlewareNextInternalRoutes: true,
15181525
} satisfies NextConfig)
15191526

15201527
export async function normalizeConfig(phase: string, config: any) {

packages/next/src/server/next-server.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,13 +1455,13 @@ export default class NextNodeServer extends BaseServer<
14551455
const middlewareModule = await this.loadNodeMiddleware()
14561456

14571457
if (middlewareModule) {
1458+
const matchers = middlewareModule.config?.matchers || [
1459+
{ regexp: '.*', originalSource: '/:path*' },
1460+
]
14581461
return {
1459-
match: getMiddlewareRouteMatcher(
1460-
middlewareModule.config?.matchers || [
1461-
{ regexp: '.*', originalSource: '/:path*' },
1462-
]
1463-
),
1462+
match: getMiddlewareRouteMatcher(matchers),
14641463
page: '/',
1464+
matchers,
14651465
}
14661466
}
14671467

@@ -1471,6 +1471,7 @@ export default class NextNodeServer extends BaseServer<
14711471
return {
14721472
match: getMiddlewareMatcher(middleware),
14731473
page: '/',
1474+
matchers: middleware.matchers,
14741475
}
14751476
}
14761477

@@ -1615,6 +1616,17 @@ export default class NextNodeServer extends BaseServer<
16151616
url?: string
16161617
}) {}
16171618

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+
16181630
/**
16191631
* This method gets all middleware matchers and execute them when the request
16201632
* matches. It will make sure that each middleware exists and is compiled and
@@ -1679,6 +1691,18 @@ export default class NextNodeServer extends BaseServer<
16791691
return { finished: false }
16801692
}
16811693

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+
16821706
await this.ensureMiddleware(params.request.url)
16831707
const middlewareInfo = this.getEdgeFunctionInfo({
16841708
page: middleware.page,

0 commit comments

Comments
 (0)