Skip to content

feat: node middleware #725

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

Merged
merged 16 commits into from
Feb 27, 2025
5 changes: 5 additions & 0 deletions .changeset/nasty-geckos-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": minor
---

Add support for the node middleware
4 changes: 4 additions & 0 deletions packages/open-next/src/adapters/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
loadBuildId,
loadConfig,
loadConfigHeaders,
loadFunctionsConfigManifest,
loadHtmlPages,
loadMiddlewareManifest,
loadPrerenderManifest,
Expand Down Expand Up @@ -35,3 +36,6 @@ export const MiddlewareManifest =
export const AppPathsManifest = /* @__PURE__ */ loadAppPathsManifest(NEXT_DIR);
export const AppPathRoutesManifest =
/* @__PURE__ */ loadAppPathRoutesManifest(NEXT_DIR);

export const FunctionsConfigManifest =
/* @__PURE__ */ loadFunctionsConfigManifest(NEXT_DIR);
11 changes: 11 additions & 0 deletions packages/open-next/src/adapters/config/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from "node:fs";
import path from "node:path";
import type {
FunctionsConfigManifest,
MiddlewareManifest,
NextConfig,
PrerenderManifest,
Expand Down Expand Up @@ -123,3 +124,13 @@ export function loadMiddlewareManifest(nextDir: string) {
const json = fs.readFileSync(filePath, "utf-8");
return JSON.parse(json) as MiddlewareManifest;
}

export function loadFunctionsConfigManifest(nextDir: string) {
const filePath = path.join(nextDir, "server/functions-config-manifest.json");
try {
const json = fs.readFileSync(filePath, "utf-8");
return JSON.parse(json) as FunctionsConfigManifest;
} catch (e) {
return { functions: {}, version: 1 };
}
}
2 changes: 1 addition & 1 deletion packages/open-next/src/build/compileConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export async function compileOpenNextConfig(
// We need to check if the config uses the edge runtime at any point
// If it does, we need to compile it with the edge runtime
const usesEdgeRuntime =
config.middleware?.external ||
(config.middleware?.external && config.middleware.runtime !== "node") ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment: this might not play well with Cloudflare but we probably need to rethink how we compile to edge vs node

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah i think we should rely on the wrapper to tell us how we should compile (i.e. node vs workers).
For the moment i think it's fine, node middleware is still experimental

Object.values(config.functions || {}).some((fn) => fn.runtime === "edge");
if (!usesEdgeRuntime) {
logger.debug(
Expand Down
2 changes: 2 additions & 0 deletions packages/open-next/src/build/constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//TODO: Move all other manifest path here as well
export const MIDDLEWARE_TRACE_FILE = "server/middleware.js.nft.json";
43 changes: 32 additions & 11 deletions packages/open-next/src/build/copyTracedFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import path from "node:path";
import type { NextConfig, PrerenderManifest } from "types/next-types";

import logger from "../logger.js";
import { MIDDLEWARE_TRACE_FILE } from "./constant.js";

const __dirname = url.fileURLToPath(new URL(".", import.meta.url));

Expand All @@ -24,14 +25,24 @@ function copyPatchFile(outputDir: string) {
copyFileSync(patchFile, outputPatchFile);
}

interface CopyTracedFilesOptions {
buildOutputPath: string;
packagePath: string;
outputDir: string;
routes: string[];
bundledNextServer: boolean;
skipServerFiles?: boolean;
}

// eslint-disable-next-line sonarjs/cognitive-complexity
export async function copyTracedFiles(
buildOutputPath: string,
packagePath: string,
outputDir: string,
routes: string[],
bundledNextServer: boolean,
) {
export async function copyTracedFiles({
buildOutputPath,
packagePath,
outputDir,
routes,
bundledNextServer,
skipServerFiles,
}: CopyTracedFilesOptions) {
const tsStart = Date.now();
const dotNextDir = path.join(buildOutputPath, ".next");
const standaloneDir = path.join(dotNextDir, "standalone");
Expand All @@ -58,10 +69,11 @@ export async function copyTracedFiles(
const filesToCopy = new Map<string, string>();

// Files necessary by the server
extractFiles(requiredServerFiles.files).forEach((f) => {
filesToCopy.set(f, f.replace(standaloneDir, outputDir));
});

if (!skipServerFiles) {
extractFiles(requiredServerFiles.files).forEach((f) => {
filesToCopy.set(f, f.replace(standaloneDir, outputDir));
});
}
// create directory for pages
if (existsSync(path.join(standaloneDir, ".next/server/pages"))) {
mkdirSync(path.join(outputNextDir, "server/pages"), {
Expand Down Expand Up @@ -141,6 +153,15 @@ File ${fullFilePath} does not exist
}
};

if (existsSync(path.join(dotNextDir, MIDDLEWARE_TRACE_FILE))) {
// We still need to copy the nft.json file so that computeCopyFilesForPage doesn't throw
copyFileSync(
path.join(dotNextDir, MIDDLEWARE_TRACE_FILE),
path.join(standaloneNextDir, MIDDLEWARE_TRACE_FILE),
);
computeCopyFilesForPage("middleware");
}

const hasPageDir = routes.some((route) => route.startsWith("pages/"));
const hasAppDir = routes.some((route) => route.startsWith("app/"));

Expand Down
25 changes: 22 additions & 3 deletions packages/open-next/src/build/createMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from "node:fs";
import path from "node:path";

import { loadFunctionsConfigManifest } from "config/util.js";
import logger from "../logger.js";
import type {
MiddlewareInfo,
Expand All @@ -9,6 +10,10 @@ import type {
import { buildEdgeBundle } from "./edge/createEdgeBundle.js";
import * as buildHelper from "./helper.js";
import { installDependencies } from "./installDeps.js";
import {
buildBundledNodeMiddleware,
buildExternalNodeMiddleware,
} from "./middleware/buildNodeMiddleware.js";

/**
* Compiles the middleware bundle.
Expand All @@ -32,10 +37,24 @@ export async function createMiddleware(
),
) as MiddlewareManifest;

const middlewareInfo = middlewareManifest.middleware["/"] as
const edgeMiddlewareInfo = middlewareManifest.middleware["/"] as
| MiddlewareInfo
| undefined;

if (!edgeMiddlewareInfo) {
// If there is no middleware info, it might be a node middleware
const functionsConfigManifest = loadFunctionsConfigManifest(
path.join(appBuildOutputPath, ".next"),
);

if (functionsConfigManifest?.functions["/_middleware"]) {
await (config.middleware?.external
? buildExternalNodeMiddleware(options)
: buildBundledNodeMiddleware(options));
return;
}
}

if (config.middleware?.external) {
const outputPath = path.join(outputDir, "middleware");
fs.mkdirSync(outputPath, { recursive: true });
Expand All @@ -55,7 +74,7 @@ export async function createMiddleware(
"middleware.js",
),
outfile: path.join(outputPath, "handler.mjs"),
middlewareInfo,
middlewareInfo: edgeMiddlewareInfo,
options,
overrides: {
...config.middleware.override,
Expand All @@ -77,7 +96,7 @@ export async function createMiddleware(
"edgeFunctionHandler.js",
),
outfile: path.join(options.buildDir, "middleware.mjs"),
middlewareInfo,
middlewareInfo: edgeMiddlewareInfo,
options,
onlyBuildOnce: true,
name: "middleware",
Expand Down
12 changes: 6 additions & 6 deletions packages/open-next/src/build/createServerBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,13 @@ async function generateBundle(
buildHelper.copyEnvFile(appBuildOutputPath, packagePath, outputPath);

// Copy all necessary traced files
await copyTracedFiles(
appBuildOutputPath,
await copyTracedFiles({
buildOutputPath: appBuildOutputPath,
packagePath,
outputPath,
fnOptions.routes ?? ["app/page.tsx"],
isBundled,
);
outputDir: outputPath,
routes: fnOptions.routes ?? ["app/page.tsx"],
bundledNextServer: isBundled,
});

// Build Lambda code
// note: bundle in OpenNext package b/c the adapter relies on the
Expand Down
9 changes: 4 additions & 5 deletions packages/open-next/src/build/edge/createEdgeBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
import type { OriginResolver } from "types/overrides.js";
import logger from "../../logger.js";
import { openNextEdgePlugins } from "../../plugins/edge.js";
import { openNextExternalMiddlewarePlugin } from "../../plugins/externalMiddleware.js";
import { openNextReplacementPlugin } from "../../plugins/replacement.js";
import { openNextResolvePlugin } from "../../plugins/resolve.js";
import { getCrossPlatformPathRegex } from "../../utils/regex.js";
Expand Down Expand Up @@ -88,14 +89,12 @@ export async function buildEdgeBundle({
target: getCrossPlatformPathRegex("adapters/middleware.js"),
deletes: includeCache ? [] : ["includeCacheInMiddleware"],
}),
openNextExternalMiddlewarePlugin(
path.join(options.openNextDistDir, "core/edgeFunctionHandler.js"),
),
openNextEdgePlugins({
middlewareInfo,
nextDir: path.join(options.appBuildOutputPath, ".next"),
edgeFunctionHandlerPath: path.join(
options.openNextDistDir,
"core",
"edgeFunctionHandler.js",
),
isInCloudfare,
}),
],
Expand Down
138 changes: 138 additions & 0 deletions packages/open-next/src/build/middleware/buildNodeMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import fs from "node:fs";
import path from "node:path";

import type {
IncludedOriginResolver,
LazyLoadedOverride,
OverrideOptions,
} from "types/open-next.js";
import type { OriginResolver } from "types/overrides.js";
import { getCrossPlatformPathRegex } from "utils/regex.js";
import { openNextExternalMiddlewarePlugin } from "../../plugins/externalMiddleware.js";
import { openNextReplacementPlugin } from "../../plugins/replacement.js";
import { openNextResolvePlugin } from "../../plugins/resolve.js";
import { copyTracedFiles } from "../copyTracedFiles.js";
import * as buildHelper from "../helper.js";
import { installDependencies } from "../installDeps.js";

type Override = OverrideOptions & {
originResolver?: LazyLoadedOverride<OriginResolver> | IncludedOriginResolver;
};

export async function buildExternalNodeMiddleware(
options: buildHelper.BuildOptions,
) {
const { appBuildOutputPath, config, outputDir } = options;
if (!config.middleware?.external) {
throw new Error(
"This function should only be called for external middleware",
);
}
const outputPath = path.join(outputDir, "middleware");
fs.mkdirSync(outputPath, { recursive: true });

// Copy open-next.config.mjs
buildHelper.copyOpenNextConfig(
options.buildDir,
outputPath,
await buildHelper.isEdgeRuntime(config.middleware.override),
);
const overrides = {
...config.middleware.override,
originResolver: config.middleware.originResolver,
};
const includeCache = config.dangerous?.enableCacheInterception;
const packagePath = buildHelper.getPackagePath(options);

// TODO: change this so that we don't copy unnecessary files
await copyTracedFiles({
buildOutputPath: appBuildOutputPath,
packagePath,
outputDir: outputPath,
routes: [],
bundledNextServer: false,
skipServerFiles: true,
});

function override<T extends keyof Override>(target: T) {
return typeof overrides?.[target] === "string"
? overrides[target]
: undefined;
}

// Bundle middleware
await buildHelper.esbuildAsync(
{
entryPoints: [
path.join(options.openNextDistDir, "adapters", "middleware.js"),
],
outfile: path.join(outputPath, "handler.mjs"),
external: ["./.next/*"],
platform: "node",
plugins: [
openNextResolvePlugin({
overrides: {
wrapper: override("wrapper") ?? "aws-lambda",
converter: override("converter") ?? "aws-cloudfront",
...(includeCache
? {
tagCache: override("tagCache") ?? "dynamodb-lite",
incrementalCache: override("incrementalCache") ?? "s3-lite",
queue: override("queue") ?? "sqs-lite",
}
: {}),
originResolver: override("originResolver") ?? "pattern-env",
proxyExternalRequest: override("proxyExternalRequest") ?? "node",
},
fnName: "middleware",
}),
openNextReplacementPlugin({
name: "externalMiddlewareOverrides",
target: getCrossPlatformPathRegex("adapters/middleware.js"),
deletes: includeCache ? [] : ["includeCacheInMiddleware"],
}),
openNextExternalMiddlewarePlugin(
path.join(
options.openNextDistDir,
"core",
"nodeMiddlewareHandler.js",
),
),
],
banner: {
js: [
`globalThis.monorepoPackagePath = '${packagePath}';`,
"import process from 'node:process';",
"import { Buffer } from 'node:buffer';",
"import { AsyncLocalStorage } from 'node:async_hooks';",
"import { createRequire as topLevelCreateRequire } from 'module';",
"const require = topLevelCreateRequire(import.meta.url);",
"import bannerUrl from 'url';",
"const __dirname = bannerUrl.fileURLToPath(new URL('.', import.meta.url));",
].join(""),
},
},
options,
);

// Do we need to copy or do something with env file here?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I'm not sure. I can't think of anything obvious.


installDependencies(outputPath, config.middleware?.install);
}

export async function buildBundledNodeMiddleware(
options: buildHelper.BuildOptions,
) {
await buildHelper.esbuildAsync(
{
entryPoints: [
path.join(options.openNextDistDir, "core/nodeMiddlewareHandler.js"),
],
external: ["./.next/*"],
outfile: path.join(options.buildDir, "middleware.mjs"),
bundle: true,
platform: "node",
},
options,
);
}
4 changes: 3 additions & 1 deletion packages/open-next/src/core/edgeFunctionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export default async function edgeFunctionHandler(
},
},
});
await result.waitUntil;
globalThis.__openNextAls
.getStore()
?.pendingPromiseRunner.add(result.waitUntil);
const response = result.response;
return response;
}
Loading
Loading