From f3bd767e3acab26865bd0253d333e4abf8a87069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Tue, 6 Feb 2024 15:11:58 +0100 Subject: [PATCH 1/3] fix(ts): match `MiddlewareConfig` with documentation --- .../build/analysis/get-page-static-info.ts | 50 ++++++++++++------- packages/next/src/build/entries.ts | 3 +- 2 files changed, 34 insertions(+), 19 deletions(-) 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 ab68fb4365e93..e95a789327f3a 100644 --- a/packages/next/src/build/analysis/get-page-static-info.ts +++ b/packages/next/src/build/analysis/get-page-static-info.ts @@ -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 { + 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 } @@ -50,7 +59,7 @@ export interface PageStaticInfo { ssr?: boolean rsc?: RSCModuleType generateStaticParams?: boolean - middleware?: MiddlewareConfig + middleware?: MiddlewareConfigParsed amp?: boolean | 'hybrid' extraConfig?: Record } @@ -375,24 +384,29 @@ export function getMiddlewareMatchers( function getMiddlewareConfig( pageFilePath: string, - config: any, + config: unknown, nextConfig: NextConfig -): Partial { - const result: Partial = {} +): Partial { + if (typeof config !== 'object' || config === null) { + throw new TypeError('config must be an object') + } + const result: Partial = {} - if (config.matcher) { + if ('matcher' in config) { result.matchers = getMiddlewareMatchers(config.matcher, nextConfig) } - if (typeof config.regions === 'string' || Array.isArray(config.regions)) { - result.regions = config.regions - } else if (typeof config.regions !== 'undefined') { - Log.warn( - `The \`regions\` config was ignored: config must be empty, a string or an array of strings. (${pageFilePath})` - ) + if ('regions' in config) { + if (typeof config.regions === 'string' || Array.isArray(config.regions)) { + result.regions = config.regions + } else if (typeof config.regions !== 'undefined') { + Log.warn( + `The \`regions\` config was ignored: config must be empty, a string or an array of strings. (${pageFilePath})` + ) + } } - if (config.unstable_allowDynamic) { + if ('unstable_allowDynamic' in config) { result.unstable_allowDynamicGlobs = Array.isArray( config.unstable_allowDynamic ) @@ -508,14 +522,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 = {} diff --git a/packages/next/src/build/entries.ts b/packages/next/src/build/entries.ts index 54bbed5a06694..a96cf7028af73 100644 --- a/packages/next/src/build/entries.ts +++ b/packages/next/src/build/entries.ts @@ -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, @@ -332,7 +333,7 @@ export function getEdgeServerEntry(opts: { isServerComponent: boolean page: string pages: MappedPages - middleware?: Partial + middleware?: Partial pagesType: PAGE_TYPES appDirLoader?: string hasInstrumentationHook?: boolean From 10bf5833a550ea7d06f4a1b67e826fad9898633e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Tue, 6 Feb 2024 15:12:08 +0100 Subject: [PATCH 2/3] fix test --- test/production/middleware-typescript/app/middleware.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/production/middleware-typescript/app/middleware.ts b/test/production/middleware-typescript/app/middleware.ts index cdf0780acd41a..c60846ad7d559 100644 --- a/test/production/middleware-typescript/app/middleware.ts +++ b/test/production/middleware-typescript/app/middleware.ts @@ -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 From d074b07f6aa62ae53b5549fed69d8bc2f47ea540 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Tue, 6 Feb 2024 15:32:36 +0100 Subject: [PATCH 3/3] revert to `any` --- .../build/analysis/get-page-static-info.ts | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) 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 e95a789327f3a..4420c266144c3 100644 --- a/packages/next/src/build/analysis/get-page-static-info.ts +++ b/packages/next/src/build/analysis/get-page-static-info.ts @@ -384,29 +384,24 @@ export function getMiddlewareMatchers( function getMiddlewareConfig( pageFilePath: string, - config: unknown, + config: any, nextConfig: NextConfig ): Partial { - if (typeof config !== 'object' || config === null) { - throw new TypeError('config must be an object') - } const result: Partial = {} - if ('matcher' in config) { + if (config.matcher) { result.matchers = getMiddlewareMatchers(config.matcher, nextConfig) } - if ('regions' in config) { - if (typeof config.regions === 'string' || Array.isArray(config.regions)) { - result.regions = config.regions - } else if (typeof config.regions !== 'undefined') { - Log.warn( - `The \`regions\` config was ignored: config must be empty, a string or an array of strings. (${pageFilePath})` - ) - } + if (typeof config.regions === 'string' || Array.isArray(config.regions)) { + result.regions = config.regions + } else if (typeof config.regions !== 'undefined') { + Log.warn( + `The \`regions\` config was ignored: config must be empty, a string or an array of strings. (${pageFilePath})` + ) } - if ('unstable_allowDynamic' in config) { + if (config.unstable_allowDynamic) { result.unstable_allowDynamicGlobs = Array.isArray( config.unstable_allowDynamic )