Skip to content

Commit

Permalink
fix(ts): match MiddlewareConfig with documentation (#61718)
Browse files Browse the repository at this point in the history
### What?

Fix the user-facing `MiddlewareConfig` interface.

~While in the codebase, I also made the incoming config object type a
bit more strict by converting from `any` to `unknown`.~ Reverted, as we
do a config assertion already in a [different
place](https://github.com/vercel/next.js/blob/canary/packages/next-swc/crates/next-custom-transforms/src/transforms/page_config.rs/#L171-L180).

### Why?

The interface we previously exposed was the one we used internally,
_after_ we did some parsing on the config object, which is different
from what the user is expected to pass.

### How?

I separated the internal type to its own `MiddlewareConfigParsed`
interface.

Closes NEXT-2375
Fixes #61705

Ref: #61576
  • Loading branch information
balazsorban44 authored Feb 7, 2024
1 parent d04cfb6 commit 53fd5ac
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
27 changes: 18 additions & 9 deletions packages/next/src/build/analysis/get-page-static-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,22 @@ 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 MiddlewareConfigParsed
extends Omit<MiddlewareConfig, 'matcher'> {
matchers?: MiddlewareMatcher[]
}

/**
* This interface represents the exported `config` object in a `middleware.ts` file.
*
* Read more: [Next.js Docs: Middleware `config` object](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
* Read more: [Next.js Docs: Middleware `matcher`](https://nextjs.org/docs/app/api-reference/file-conventions/middleware#matcher),
* [Next.js Docs: Middleware matching paths](https://nextjs.org/docs/app/building-your-application/routing/middleware#matching-paths)
*/
matchers?: MiddlewareMatcher[]
matcher?: string | string[] | MiddlewareMatcher[]
unstable_allowDynamicGlobs?: string[]
regions?: string[] | string
}
Expand All @@ -50,7 +59,7 @@ export interface PageStaticInfo {
ssr?: boolean
rsc?: RSCModuleType
generateStaticParams?: boolean
middleware?: MiddlewareConfig
middleware?: MiddlewareConfigParsed
amp?: boolean | 'hybrid'
extraConfig?: Record<string, any>
}
Expand Down Expand Up @@ -377,8 +386,8 @@ function getMiddlewareConfig(
pageFilePath: string,
config: any,
nextConfig: NextConfig
): Partial<MiddlewareConfig> {
const result: Partial<MiddlewareConfig> = {}
): Partial<MiddlewareConfigParsed> {
const result: Partial<MiddlewareConfigParsed> = {}

if (config.matcher) {
result.matchers = getMiddlewareMatchers(config.matcher, nextConfig)
Expand Down Expand Up @@ -508,14 +517,14 @@ export async function getPageStaticInfo(params: {
const rsc = rscInfo.type

// default / failsafe value for config
let config: any
let config: any // TODO: type this as unknown
try {
config = extractExportedConstValue(swcAST, 'config')
} catch (e) {
if (e instanceof UnsupportedValueError) {
warnAboutUnsupportedValue(pageFilePath, page, e)
}
// `export config` doesn't exist, or other unknown error throw by swc, silence them
// `export config` doesn't exist, or other unknown error thrown by swc, silence them
}

const extraConfig: Record<string, any> = {}
Expand Down
3 changes: 2 additions & 1 deletion packages/next/src/build/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { EdgeAppRouteLoaderQuery } from './webpack/loaders/next-edge-app-ro
import type { NextConfigComplete } from '../server/config-shared'
import type { webpack } from 'next/dist/compiled/webpack/webpack'
import type {
MiddlewareConfigParsed,
MiddlewareConfig,
MiddlewareMatcher,
PageStaticInfo,
Expand Down Expand Up @@ -332,7 +333,7 @@ export function getEdgeServerEntry(opts: {
isServerComponent: boolean
page: string
pages: MappedPages
middleware?: Partial<MiddlewareConfig>
middleware?: Partial<MiddlewareConfigParsed>
pagesType: PAGE_TYPES
appDirLoader?: string
hasInstrumentationHook?: boolean
Expand Down
6 changes: 3 additions & 3 deletions test/production/middleware-typescript/app/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export const middleware: NextMiddleware = function (request) {
}
}

export const config: MiddlewareConfig = {
matchers: [],
export const config = {
matcher: [],
regions: [],
unstable_allowDynamicGlobs: undefined,
}
} satisfies MiddlewareConfig

0 comments on commit 53fd5ac

Please sign in to comment.