|  | 
| 1 | 1 | import fs from "node:fs"; | 
|  | 2 | +import fsAsync from "node:fs/promises"; | 
| 2 | 3 | import path from "node:path"; | 
| 3 | 4 | 
 | 
| 4 | 5 | import logger from "../logger.js"; | 
| 5 | 6 | import type { | 
|  | 7 | +  FunctionsConfigManifest, | 
| 6 | 8 |   MiddlewareInfo, | 
| 7 | 9 |   MiddlewareManifest, | 
| 8 | 10 | } from "../types/next-types.js"; | 
| @@ -36,6 +38,42 @@ export async function createMiddleware( | 
| 36 | 38 |     | MiddlewareInfo | 
| 37 | 39 |     | undefined; | 
| 38 | 40 | 
 | 
|  | 41 | +  if (!middlewareInfo) { | 
|  | 42 | +    // If there is no middleware info, it might be a node middleware | 
|  | 43 | +    const functionsConfigManifestPath = path.join( | 
|  | 44 | +      appBuildOutputPath, | 
|  | 45 | +      ".next/server/functions-config-manifest.json", | 
|  | 46 | +    ); | 
|  | 47 | +    const functionsConfigManifest = JSON.parse( | 
|  | 48 | +      await fsAsync | 
|  | 49 | +        .readFile(functionsConfigManifestPath, "utf8") | 
|  | 50 | +        .catch(() => "{}"), | 
|  | 51 | +    ) as FunctionsConfigManifest; | 
|  | 52 | + | 
|  | 53 | +    // TODO: Handle external node middleware in the future | 
|  | 54 | +    if (functionsConfigManifest?.functions["/_middleware"]) { | 
|  | 55 | +      // If we are here, it means that we are using a node middleware | 
|  | 56 | +      await buildHelper.esbuildAsync( | 
|  | 57 | +        { | 
|  | 58 | +          entryPoints: [ | 
|  | 59 | +            path.join( | 
|  | 60 | +              options.openNextDistDir, | 
|  | 61 | +              "core", | 
|  | 62 | +              "nodeMiddlewareHandler.js", | 
|  | 63 | +            ), | 
|  | 64 | +          ], | 
|  | 65 | +          external: ["./.next/*"], | 
|  | 66 | +          outfile: path.join(options.buildDir, "middleware.mjs"), | 
|  | 67 | +          bundle: true, | 
|  | 68 | +          platform: "node", | 
|  | 69 | +        }, | 
|  | 70 | +        options, | 
|  | 71 | +      ); | 
|  | 72 | +      // We return early to not build the edge middleware | 
|  | 73 | +      return; | 
|  | 74 | +    } | 
|  | 75 | +  } | 
|  | 76 | + | 
| 39 | 77 |   if (config.middleware?.external) { | 
| 40 | 78 |     const outputPath = path.join(outputDir, "middleware"); | 
| 41 | 79 |     fs.mkdirSync(outputPath, { recursive: true }); | 
|  | 
0 commit comments