diff --git a/packages/next/server.d.ts b/packages/next/server.d.ts index a47014498321d..d5eb0a58dd801 100644 --- a/packages/next/server.d.ts +++ b/packages/next/server.d.ts @@ -7,7 +7,7 @@ declare global { export { NextFetchEvent } from 'next/dist/server/web/spec-extension/fetch-event' export { NextRequest } from 'next/dist/server/web/spec-extension/request' export { NextResponse } from 'next/dist/server/web/spec-extension/response' -export { NextMiddleware } from 'next/dist/server/web/types' +export { NextMiddleware, MiddlewareConfig } from 'next/dist/server/web/types' export { userAgentFromString } from 'next/dist/server/web/spec-extension/user-agent' export { userAgent } from 'next/dist/server/web/spec-extension/user-agent' export { URLPattern } from 'next/dist/compiled/@edge-runtime/primitives/url' diff --git a/packages/next/src/build/analysis/get-page-static-info.ts b/packages/next/src/build/analysis/get-page-static-info.ts index 57a008dbe9408..ab68fb4365e93 100644 --- a/packages/next/src/build/analysis/get-page-static-info.ts +++ b/packages/next/src/build/analysis/get-page-static-info.ts @@ -24,7 +24,12 @@ import { PAGE_TYPES } from '../../lib/page-types' // Don't forget to update the next-types-plugin file as well const AUTHORIZED_EXTRA_ROUTER_PROPS = ['maxDuration'] +/** @see https://nextjs.org/docs/app/api-reference/file-conventions/middleware#config-object-optional */ export interface MiddlewareConfig { + /** + * @see https://nextjs.org/docs/app/api-reference/file-conventions/middleware#matcher + * @see https://nextjs.org/docs/app/building-your-application/routing/middleware#matching-paths + */ matchers?: MiddlewareMatcher[] unstable_allowDynamicGlobs?: string[] regions?: string[] | string diff --git a/packages/next/src/server/web/types.ts b/packages/next/src/server/web/types.ts index 672acf3e502e1..6722641e7c78d 100644 --- a/packages/next/src/server/web/types.ts +++ b/packages/next/src/server/web/types.ts @@ -6,6 +6,8 @@ import type { CloneableBody } from '../body-streams' import type { OutgoingHttpHeaders } from 'http' import type { FetchMetrics } from '../base-http' +export type { MiddlewareConfig } from '../../build/analysis/get-page-static-info' + export interface RequestData { geo?: { city?: string diff --git a/test/production/middleware-typescript/app/middleware.ts b/test/production/middleware-typescript/app/middleware.ts index 5010b26952794..cdf0780acd41a 100644 --- a/test/production/middleware-typescript/app/middleware.ts +++ b/test/production/middleware-typescript/app/middleware.ts @@ -1,4 +1,9 @@ -import { NextMiddleware, NextResponse, URLPattern } from 'next/server' +import { + NextMiddleware, + NextResponse, + URLPattern, + MiddlewareConfig, +} from 'next/server' export const middleware: NextMiddleware = function (request) { const pattern = new URLPattern({ @@ -18,3 +23,9 @@ export const middleware: NextMiddleware = function (request) { }) } } + +export const config: MiddlewareConfig = { + matchers: [], + regions: [], + unstable_allowDynamicGlobs: undefined, +}