diff --git a/packages/fs-detectors/src/detect-builders.ts b/packages/fs-detectors/src/detect-builders.ts index 759d2d9dc..00a24dc96 100644 --- a/packages/fs-detectors/src/detect-builders.ts +++ b/packages/fs-detectors/src/detect-builders.ts @@ -99,7 +99,7 @@ export async function detectBuilders( const errors: ErrorResponse[] = []; const warnings: ErrorResponse[] = []; - const apiBuilders: Builder[] = []; + let apiBuilders: Builder[] = []; let frontendBuilder: Builder | null = null; const functionError = validateFunctions(options); @@ -305,6 +305,24 @@ export async function detectBuilders( }; } + // Exclude the middleware builder for Next.js apps since @vercel/next + // will build middlewares. + // + // While maybeGetApiBuilder() excludes the middleware builder, however, + // we need to check if it's a Next.js app here again for the case where + // `projectSettings.framework == null`. + if ( + framework === null && + frontendBuilder?.use === '@vercel/next' && + apiBuilders.length > 0 + ) { + apiBuilders = apiBuilders.filter(builder => { + const isMiddlewareBuilder = + builder.use === '@vercel/node' && builder.config?.middleware; + return !isMiddlewareBuilder; + }); + } + const builders: Builder[] = []; if (apiBuilders.length) { diff --git a/packages/fs-detectors/test/unit.builds-and-routes-detector.test.ts b/packages/fs-detectors/test/unit.builds-and-routes-detector.test.ts index dc80d8677..dc12e363d 100644 --- a/packages/fs-detectors/test/unit.builds-and-routes-detector.test.ts +++ b/packages/fs-detectors/test/unit.builds-and-routes-detector.test.ts @@ -2281,6 +2281,31 @@ describe('Test `detectBuilders` with `featHandleMiss=true`', () => { }, ]); }); + + it('should not add middleware builder when building "nextjs"', async () => { + const files = ['package.json', 'pages/index.ts', 'middleware.ts']; + const pkg = { + scripts: { build: 'next build' }, + dependencies: { next: '12.2.0' }, + }; + const projectSettings = { + framework: null, // "Other" framework + createdAt: Date.parse('2020-02-01'), + }; + const { builders } = await detectBuilders(files, pkg, { + projectSettings, + featHandleMiss, + }); + expect(builders).toEqual([ + { + use: '@vercel/next', + src: 'package.json', + config: { + zeroConfig: true, + }, + }, + ]); + }); }); it('Test `detectRoutes`', async () => {