Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ts): match MiddlewareConfig with documentation #61718

Merged
merged 3 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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