Skip to content

Version Trap: between Next.js 16’s new Proxy architecture and OpenNext’s current Cloudflare adapter #13755

@eagoyi

Description

@eagoyi

What versions & operating system are you using?

wrangler version wrangler 4.86.0
Nodejs v22.20.0
Ubuntu v24
nextjs 16

Please provide a link to a minimal reproduction

https://github.com/eagoyi/next-cloudflare-bug

Describe the Bug

The Bug Description

When using Next.js 16 with @cloudflare/next-on-pages, implemented several temporary fix and removed sanity and had to rename proxy to middleware because I was running in a loop of changing proxy runtime to edge and when I do it returns error that it needs nodejs, and when I eventually find a temporary work around all API routes return 500 errors due to a runtime crash in the generated middleware bundle.

The error originates from middleware.func.js attempting to import a Node.js-only module (async_hooks), which is not available in the Cloudflare Workers runtime.

i.e The error whem using proxy.ts with edge runtime


○  (Static)   prerendered as static content
ƒ  (Dynamic)  server-rendered on demand

ERROR Node.js middleware is not currently supported. Consider switching to Edge Middleware.

The error when I remove edge runtime

> Build error occurred
Error: Route segment config is not allowed in Proxy file at "./proxy.ts". Proxy always runs on Node.js runtime. Learn more: https://nextjs.org/docs/messages/middleware-to-proxy
    at ignore-listed frames
node:internal/errors:983
  const err = new Error(message);
              ^

Error: Command failed: npm run build
    at genericNodeError (node:internal/errors:983:15)
    at wrappedFn (node:internal/errors:537:14)
    at checkExecSyncError (node:child_process:916:11)
    at Object.execSync (node:child_process:988:15)
    at buildNextjsApp (file:///home/eagoyi/project-dep/indieinkpress/node_modules/@opennextjs/aws/dist/build/buildNextApp.js:15:8)
    at build (file:///home/eagoyi/project-dep/indieinkpress/node_modules/@opennextjs/cloudflare/dist/cli/build/build.js:50:9)
    at async buildCommand (file:///home/eagoyi/project-dep/indieinkpress/node_modules/@opennextjs/cloudflare/dist/cli/commands/build.js:40:5) {
  status: 1,
  signal: null,
  output: [ null, null, null ],
  pid: 1728508,
  stdout: null,
  stderr: null
}

The error when I switch to deprecated middleware to escape the loop

- Experiments (use with caution):
  ⨯ turbopackFileSystemCacheForDev

⚠ The "middleware" file convention is deprecated. Please use "proxy" instead. Learn more: https://nextjs.org/docs/messages/middleware-to-proxy
  Creating an optimized production build ...

> Build error occurred
Error: Turbopack build failed with 1 errors:
./middleware.ts
Middleware is missing expected function export name
This function is what Next.js runs for every request handled by this middleware.

Why this happens:
- The file exists but doesn't export a function.
- The export is not a function (e.g., an object or constant).
- There's a syntax error preventing the export from being recognized.

To fix it:
- Ensure this file has either a default or "middleware" function export.

Learn more: https://nextjs.org/docs/messages/middleware-to-proxy


    at ignore-listed frames
node:internal/errors:983
  const err = new Error(message);
              ^

Error: Command failed: npm run build
    at genericNodeError (node:internal/errors:983:15)
    at wrappedFn (node:internal/errors:537:14)
    at checkExecSyncError (node:child_process:916:11)
    at Object.execSync (node:child_process:988:15)
    at buildNextjsApp (file:///home/eagoyi/project-dep/indieinkpress/node_modules/@opennextjs/aws/dist/build/buildNextApp.js:15:8)
    at build (file:///home/eagoyi/project-dep/indieinkpress/node_modules/@opennextjs/cloudflare/dist/cli/build/build.js:50:9)
    at async buildCommand (file:///home/eagoyi/project-dep/indieinkpress/node_modules/@opennextjs/cloudflare/dist/cli/commands/build.js:40:5) {
  status: 1,
  signal: null,
  output: [ null, null, null ],
  pid: 1723886,
  stdout: null,
  stderr: null
}

Recreate

In linked repository but here is a simple one:

Create a Next.js 16 app (App Router)

Add a simple API route:

export const runtime = "edge";

export async function GET() { return new Response("ok"); }

Build using: npx @cloudflare/next-on-pages@1

Run: npx wrangler pages dev

Request: curl http://localhost:8788/api/test

API route should return "ok"

Returns 500 error with Next.js global error page Notes / Debugging Done

Work arounds

Issue persists even with minimal API route (no dependencies)
No middleware.ts or _middleware present in project
No Node.js APIs used in route
Indicates incompatibility between Next 16 output and next-on-pages adapter
switched to using opennext

Please provide any relevant error logs

Error Logs

Log 1 Trying with cloudflarepages

⚡️ Completed `npx vercel build`.

⚡️ ERROR: Failed to produce a Cloudflare Pages build from the project.
⚡️
⚡️  The following routes were not configured to run with the Edge Runtime:
⚡️    - /studio/[[...index]]
⚡️
⚡️  Please make sure that all your non-static routes export the following edge runtime route segment config:
⚡️    export const runtime = 'edge';
⚡️
⚡️  You can read more about the Edge Runtime on the Next.js documentation:
⚡️    <https://nextjs.org/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes>

After several temporary work arounds for next-pages

⎔ Starting local server...
[wrangler:info] Ready on <http://localhost:8788>
[wrangler:info] GET / 200 OK (48ms)
✘ [ERROR] [Error: No such module "__next-on-pages-dist__/functions/async_hooks".
    imported from "__next-on-pages-dist__/functions/middleware.func.js"]
[wrangler:info] GET /api/location 500 Internal Server Error (172ms)
✘ [ERROR] [Error: No such module "__next-on-pages-dist__/functions/async_hooks".
    imported from "__next-on-pages-dist__/functions/middleware.func.js"]
[wrangler:info] GET /api/test 500 Internal Server Error (16ms)
✘ [ERROR] [Error: No such module "__next-on-pages-dist__/functions/async_hooks".
    imported from "__next-on-pages-dist__/functions/middleware.func.js"]
[wrangler:info] GET /api/test 500 Internal Server Error (15ms)
✘ [ERROR] [Error: No such module "__next-on-pages-dist__/functions/async_hooks".
    imported from "__next-on-pages-dist__/functions/middleware.func.js"]
[wrangler:info] GET /api/location 500 Internal Server Error (25ms)
⎔ Shutting down local server...

Log 2 Trying with cloudflare openenxt, also after several work around for opennext

> NODE_OPTIONS=--trace-exit npx wrangler dev

 ⛅️ wrangler 4.86.0
───────────────────

✘ [ERROR] Build failed with 1 error:

  ✘ [ERROR] "import-source" is not a valid feature name for the
  "supported" setting
  
🪵  Logs were written to "/home/local/.config/.wrangler/logs/wrangler-2026-04-30_10-20-48_313.log"
(node:1516537) WARNING: Exited the environment with code 1
    at processReallyExit (/home/local/project-dep/bookpress/node_modules/wrangler/wrangler-dist/cli.js:49535:35)
    at exit (node:internal/process/per_thread:245:13)
    at /home/local/project-dep/bookpress/node_modules/wrangler/wrangler-dist/cli.js:320744:31

(node:1516530) WARNING: Exited the environment with code 1
    at exit (node:internal/process/per_thread:245:13)
    at /home/local/project-dep/bookpress/node_modules/wrangler/bin/wrangler.js:39:12
    at emit (node:events:519:28)
    at ChildProcess._handle.onexit (node:internal/child_process:293:12)

(node:1516507) WARNING: Exited the environment with code 1
    at exit (node:internal/process/per_thread:245:13)
    at /home/local/.nvm/versions/node/v22.20.0/lib/node_modules/npm/lib/cli/exit-handler.js:169:21
    at afterWrite (node:internal/streams/writable:710:5)
    at afterWriteTick (node:internal/streams/writable:696:10)
    at processTicksAndRejections (node:internal/process/task_queues:89:21)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No fields configured for Bug.

    Projects

    Status

    Untriaged

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions