Skip to content

fix(middleware): enable wasm in bundled middleware #514

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 2 commits into from
Mar 28, 2025
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
5 changes: 5 additions & 0 deletions .changeset/curly-bikes-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/cloudflare": patch
---

fix(middleware): enable wasm in bundled middleware
2 changes: 1 addition & 1 deletion packages/cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"homepage": "https://github.com/opennextjs/opennextjs-cloudflare",
"dependencies": {
"@dotenvx/dotenvx": "catalog:",
"@opennextjs/aws": "https://pkg.pr.new/@opennextjs/aws@798",
"@opennextjs/aws": "https://pkg.pr.new/@opennextjs/aws@802",
"enquirer": "^2.4.1",
"glob": "catalog:"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/cloudflare/src/cli/build/bundle-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export async function bundleServer(buildOpts: BuildOptions): Promise<void> {
// Apply updater updates, must be the last plugin
updater.plugin,
] as Plugin[],
external: ["./middleware/handler.mjs"],
external: ["./middleware/handler.mjs", "*.wasm"],
alias: {
// Note: it looks like node-fetch is actually not necessary for us, so we could replace it with an empty shim
// but just to be safe we replace it with a module that re-exports the native fetch
Expand Down
25 changes: 14 additions & 11 deletions packages/cloudflare/src/cli/build/open-next/createServerBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import fs from "node:fs";
import path from "node:path";

import { loadMiddlewareManifest } from "@opennextjs/aws/adapters/config/util.js";
import { bundleNextServer } from "@opennextjs/aws/build/bundleNextServer.js";
import { compileCache } from "@opennextjs/aws/build/compileCache.js";
import { copyTracedFiles } from "@opennextjs/aws/build/copyTracedFiles.js";
import { generateEdgeBundle } from "@opennextjs/aws/build/edge/createEdgeBundle.js";
import { copyMiddlewareResources, generateEdgeBundle } from "@opennextjs/aws/build/edge/createEdgeBundle.js";
import * as buildHelper from "@opennextjs/aws/build/helper.js";
import { installDependencies } from "@opennextjs/aws/build/installDeps.js";
import type { CodePatcher } from "@opennextjs/aws/build/patch/codePatcher.js";
Expand Down Expand Up @@ -138,13 +139,11 @@ async function generateBundle(
// `.next/standalone/package/path` (ie. `.next`, `server.js`).
// We need to output the handler file inside the package path.
const packagePath = buildHelper.getPackagePath(options);
fs.mkdirSync(path.join(outputPath, packagePath), { recursive: true });
const outPackagePath = path.join(outputPath, packagePath);
fs.mkdirSync(outPackagePath, { recursive: true });

const ext = fnOptions.runtime === "deno" ? "mjs" : "cjs";
fs.copyFileSync(
path.join(options.buildDir, `cache.${ext}`),
path.join(outputPath, packagePath, "cache.cjs")
);
fs.copyFileSync(path.join(options.buildDir, `cache.${ext}`), path.join(outPackagePath, "cache.cjs"));

if (fnOptions.runtime === "deno") {
addDenoJson(outputPath, packagePath);
Expand All @@ -153,7 +152,7 @@ async function generateBundle(
// Bundle next server if necessary
const isBundled = fnOptions.experimentalBundledNextServer ?? false;
if (isBundled) {
await bundleNextServer(path.join(outputPath, packagePath), appPath, {
await bundleNextServer(outPackagePath, appPath, {
minify: options.minify,
});
}
Expand All @@ -162,12 +161,16 @@ async function generateBundle(
if (!config.middleware?.external) {
fs.copyFileSync(
path.join(options.buildDir, "middleware.mjs"),
path.join(outputPath, packagePath, "middleware.mjs")
path.join(outPackagePath, "middleware.mjs")
);

const middlewareManifest = loadMiddlewareManifest(path.join(options.appBuildOutputPath, ".next"));

copyMiddlewareResources(options, middlewareManifest.middleware["/"], outPackagePath);
}

// Copy open-next.config.mjs
buildHelper.copyOpenNextConfig(options.buildDir, path.join(outputPath, packagePath), true);
buildHelper.copyOpenNextConfig(options.buildDir, outPackagePath, true);

// Copy env files
buildHelper.copyEnvFile(appBuildOutputPath, packagePath, outputPath);
Expand Down Expand Up @@ -241,7 +244,7 @@ async function generateBundle(

openNextEdgePlugins({
nextDir: path.join(options.appBuildOutputPath, ".next"),
isInCloudfare: true,
isInCloudflare: true,
}),
];

Expand Down Expand Up @@ -322,7 +325,7 @@ function addMonorepoEntrypoint(outputPath: string, packagePath: string) {

fs.writeFileSync(
path.join(outputPath, "index.mjs"),
`export * from "./${normalizePath(packagePath)}/index.mjs";`
`export { handler } from "./${normalizePath(packagePath)}/index.mjs";`
);
}

Expand Down
Loading