Skip to content

Commit 492b415

Browse files
feat(ts): expose MiddlewareConfig interface (#61576)
### What? Expose the `MiddlewareConfig` interface. ### Why? You can now `import type { MiddlewareConfig } from "next/server"` to type the `config` object in your `middleware.ts` file. Now you an type the entire file for example like so: ```ts // middleware.ts import type { NextMiddleware, MiddlewareConfig } from "next/server" export const middleware: NextMiddleware = async (req) => { //... } export const config: MiddlewareConfig = { //... } ``` ### How? Re-exported the interface from its current location via `server/web/types`, to colocate it with `NextMidldeware`. I wonder if we could somehow type this file automatically, but it might be dependent on microsoft/TypeScript#38511 Closes NEXT-2308 [Slack thread](https://vercel.slack.com/archives/C03S9JCH2Q5/p1706287433026409?thread_ts=1706058855.423019&cid=C03S9JCH2Q5), [Slack thread](https://vercel.slack.com/archives/C03KAR5DCKC/p1706659724141899)
1 parent d1fa287 commit 492b415

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

packages/next/server.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ declare global {
77
export { NextFetchEvent } from 'next/dist/server/web/spec-extension/fetch-event'
88
export { NextRequest } from 'next/dist/server/web/spec-extension/request'
99
export { NextResponse } from 'next/dist/server/web/spec-extension/response'
10-
export { NextMiddleware } from 'next/dist/server/web/types'
10+
export { NextMiddleware, MiddlewareConfig } from 'next/dist/server/web/types'
1111
export { userAgentFromString } from 'next/dist/server/web/spec-extension/user-agent'
1212
export { userAgent } from 'next/dist/server/web/spec-extension/user-agent'
1313
export { URLPattern } from 'next/dist/compiled/@edge-runtime/primitives/url'

packages/next/src/build/analysis/get-page-static-info.ts

+5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ import { PAGE_TYPES } from '../../lib/page-types'
2424
// Don't forget to update the next-types-plugin file as well
2525
const AUTHORIZED_EXTRA_ROUTER_PROPS = ['maxDuration']
2626

27+
/** @see https://nextjs.org/docs/app/api-reference/file-conventions/middleware#config-object-optional */
2728
export interface MiddlewareConfig {
29+
/**
30+
* @see https://nextjs.org/docs/app/api-reference/file-conventions/middleware#matcher
31+
* @see https://nextjs.org/docs/app/building-your-application/routing/middleware#matching-paths
32+
*/
2833
matchers?: MiddlewareMatcher[]
2934
unstable_allowDynamicGlobs?: string[]
3035
regions?: string[] | string

packages/next/src/server/web/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import type { CloneableBody } from '../body-streams'
66
import type { OutgoingHttpHeaders } from 'http'
77
import type { FetchMetrics } from '../base-http'
88

9+
export type { MiddlewareConfig } from '../../build/analysis/get-page-static-info'
10+
911
export interface RequestData {
1012
geo?: {
1113
city?: string

test/production/middleware-typescript/app/middleware.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { NextMiddleware, NextResponse, URLPattern } from 'next/server'
1+
import {
2+
NextMiddleware,
3+
NextResponse,
4+
URLPattern,
5+
MiddlewareConfig,
6+
} from 'next/server'
27

38
export const middleware: NextMiddleware = function (request) {
49
const pattern = new URLPattern({
@@ -18,3 +23,9 @@ export const middleware: NextMiddleware = function (request) {
1823
})
1924
}
2025
}
26+
27+
export const config: MiddlewareConfig = {
28+
matchers: [],
29+
regions: [],
30+
unstable_allowDynamicGlobs: undefined,
31+
}

0 commit comments

Comments
 (0)