opennextjs-cloudflare: [BUG] API Routes don't work with pages router

Describe the bug

Hey team 👋🏼,

Redirects/rewrites from the middleware.ts file to Next.js’s pages router throws an error.

 ⨯ Error: Dynamic require of "/.next/server/pages/api/login.js" is not supported

      at null.<anonymous>
  (file:///Users/REDACTED/Projects/opennextjs-cloudflare/examples/create-next-app/.wrangler/tmp/dev-FpJJge/worker.js:22:9)
      at Object.load
  (file:///Users/REDACTED/Projects/opennextjs-cloudflare/examples/create-next-app/.open-next/server-functions/default/examples/create-next-app/handler.mjs:110733:32)
      at RouteModuleLoader.load
  (file:///Users/REDACTED/Projects/opennextjs-cloudflare/examples/create-next-app/.open-next/server-functions/default/examples/create-next-app/handler.mjs:110757:38)
      at Object.runApi
  (file:///Users/REDACTED/Projects/opennextjs-cloudflare/examples/create-next-app/.open-next/server-functions/default/examples/create-next-app/handler.mjs:114126:68)
      at Object.handleApiRequest
  (file:///Users/REDACTED/Projects/opennextjs-cloudflare/examples/create-next-app/.open-next/server-functions/default/examples/create-next-app/handler.mjs:114292:21)
      at Object.handleCatchallRenderRequest
  (file:///Users/REDACTED/Projects/opennextjs-cloudflare/examples/create-next-app/.open-next/server-functions/default/examples/create-next-app/handler.mjs:113812:42)
      at async Object.runImpl
  (file:///Users/REDACTED/Projects/opennextjs-cloudflare/examples/create-next-app/.open-next/server-functions/default/examples/create-next-app/handler.mjs:22338:9)
      at async Object.handleRequestImpl
  (file:///Users/REDACTED/Projects/opennextjs-cloudflare/examples/create-next-app/.open-next/server-functions/default/examples/create-next-app/handler.mjs:22271:18)
      at async processRequest
  (file:///Users/REDACTED/Projects/opennextjs-cloudflare/examples/create-next-app/.open-next/server-functions/default/examples/create-next-app/handler.mjs:116257:5)
      at null.<anonymous> (async
  file:///Users/REDACTED/Projects/opennextjs-cloudflare/examples/create-next-app/.wrangler/tmp/dev-FpJJge/worker.js:132420:5)


[wrangler:inf] GET / 500 Internal Server Error (76ms)

I have tested this with the new App router by migrating the API route to a Route handler and it works as expected.

Steps to reproduce

  1. Using the create-next-app example.
  2. Create a middleware.ts on the root of the ./src.
  3. Add the following:
import { NextRequest, NextResponse } from 'next/server';

export async function middleware(req: NextRequest) {
  const url = new URL('/api/login', req.url);
  return NextResponse.rewrite(url);
}
  1. Create an API route on the pages router ./src/pages/api/login.ts.
  2. Add the following:
import { NextApiRequest, NextApiResponse } from 'next';

export default function login(_: NextApiRequest, res: NextApiResponse) {
  res.send('Hello, world!');
}
  1. Run next dev to verify that the middleware redirects correctly.
  2. Run pnpm preview.
  3. Navigate to the local env, the error thrown.
Error: Dynamic require of "/.next/server/pages/api/login.js" is not supported

Expected behavior

The adapter should support both App Router, and Pages router

@opennextjs/cloudflare version

0.5.1

Wrangler version

3.109.1

next info output

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.2.0: Fri Dec  6 19:04:03 PST 2024; root:xnu-11215.61.5~2/RELEASE_ARM64_T8132
  Available memory (MB): 32768
  Available CPU cores: 10
Binaries:
  Node: 20.9.0
  npm: 10.9.1
  Yarn: 1.22.19
  pnpm: 9.14.3
Relevant Packages:
  next: 14.2.24 // An outdated version detected (latest is 15.1.7), upgrade is highly recommended!
  eslint-config-next: N/A
  react: 18.3.1
  react-dom: 18.3.1
  typescript: N/A
Next.js Config:
  output: N/A
 ⚠ An outdated version detected (latest is 15.1.7), upgrade is highly recommended!
   Please try the latest canary version (`npm install next@canary`) to confirm the issue still exists before creating a new issue.
   Read more - https://nextjs.org/docs/messages/opening-an-issue

Additional context

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

i could work on a pr for this if nobody else is

The adapter only supports Next 14 & 15.

should be solved in #431

Not at the moment, we barely need it in aws to be fair. It can be useful for this specific case https://github.com/opennextjs/opennextjs-aws/blob/5b314b13b2907a4510e7f0cca6959f8da4eab310/packages/open-next/src/core/routingHandler.ts#L150-L155, we could add it, but it will need to be computed from scratch (shouldn’t be that hard in theory) I’ve created https://github.com/opennextjs/opennextjs-aws/issues/744

@vicb The error is likely coming from here https://github.com/opennextjs/opennextjs-cloudflare/blob/531ec1eea96640f38c5f1201d69b919d9a4b748a/packages/cloudflare/src/cli/build/patches/plugins/require-page.ts#L30 Page router api route are not in any manifest, so the plugin will fail for these route

Please add a repro as a link to a gh repository. We have little time to try to follow the instructions, install the mentioned version so we prioritize issues with a repro.

Thanks