opennextjs-cloudflare: [BUG] Error: Invariant: renderHTML should not be called in minimal mode

Describe the bug

I’m trying to make a post to the following NextJS API route and get this error back after the wrangler / opennext build. Not sure what’s causing it

import { NextRequest, NextResponse } from 'next/server';
import { isValidHandle } from '@atproto/syntax'

import { client as atprotoOauthClient } from '@/lib/atproto/oauth/client'

type Session = { did: string }

export async function POST(req: NextRequest) {
  const data = await req.json()

  // Validate
  const handle = data.handle
  if (typeof handle !== 'string' || !isValidHandle(handle)) {
    return NextResponse.json({error: "invalid handle"})
  }

  // Initiate the OAuth flow
  try {
    const url = await atprotoOauthClient.authorize(handle, {
      scope: 'atproto transition:generic',
    })
    return NextResponse.json({ redirect: url.toString() })
  } catch (err) {
    return NextResponse.json({error: "oauth authorize failed", details: err, message: err.message})
  }
}

Steps to reproduce

Not sure yet, can get CF trace details

I can see about producing a minimal reproducer in the next couple of days

Expected behavior

I can post to oauth endpoint and not get errors

@opennextjs/cloudflare version

0.5.3

Wrangler version

3.109.2

next info output

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.3.0: Thu Jan  2 20:23:36 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T8112
  Available memory (MB): 24576
  Available CPU cores: 8
Binaries:
  Node: 21.7.1
  npm: 10.5.0
  Yarn: 1.22.21
  pnpm: 8.14.0
Relevant Packages:
  next: 15.1.7 // Latest available version is detected (15.1.7).
  eslint-config-next: 15.1.0
  react: 19.0.0
  react-dom: 19.0.0
  typescript: 5.7.3
Next.js Config:
  output: standalone

Additional context

running site is at https://verdverm.verdverm.workers.dev

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 3
  • Comments: 44 (8 by maintainers)

Commits related to this issue

Most upvoted comments

@vicb still seems like the real error is being hidden. I’m now seeing

✘ [ERROR] ⨯ TypeError: Cannot read properties of undefined (reading 'definition')

Though the third line on the minified stack trace looks to indicate it wants to return a string about an unsupported api usage (which is what I expect the real error to be)

I updated wrangler and opennext to the latest in my repro: https://github.com/verdverm/cf-repro

Downgrading to Next.js 15.1.7 resolved this error for me.

For me, I had to downgrade to Next.js v14 and React v18 - none of the Next.js v15 versions worked for me.

From my understanding, the Invariant: renderHTML should not be called in minimal mode error is not necessarily the issue, but something else causing the route handler crash which serves up the default Next.js 500 page, and in turn this error.

I saw the comment that top-level process.env should not be used here: https://github.com/opennextjs/opennextjs-cloudflare/issues/57#issuecomment-2627499664

I did not try changing that since that would be a bigger change on our end now compared to moving down to Next.js 14 - v14 works fine with top-level process.env.

after upgrade to nextjs 15.2.0, i faced the same error. My old project hasn’t been touched for a long time; I’ve only updated the Next.js version. However, there are still no issues with versions 15.1.7 or 15.1.6.

I created a demo application in Next.js with a simple POST API route, without using any additional libraries or packages. It runs perfectly fine during development and build time without Wrangler. However, when running in preview mode, I encounter the error mentioned in the issue title, and I’m not sure why.

Here is the project link if you’d like to take a look. I haven’t removed anything, so you should be able to understand the setup. Nothing in the code is confidential.

🔗 Project Link: https://github.com/P4rthPat3l/nextjs-isr-demo

Right, so the real issue in that case is this then: https://github.com/cloudflare/workerd/issues/3053

Is there a chance you can update the repro?

@verdverm Could you please try to build your app with yarn worker:build, then add

import process from "node:process";
Object.assign(process.versions, {node: "18.0.0"});
Object.assign(process, {version : "v18"});

at the top of ./open-next/worker.js and then yarn wrangler dev or deploy.

Details: It looks like some libs are using feature detection.

Object.assign(process.versions, {node: "18.0.0"}); is used to prevent using util.deepClone.

Object.assign(process, {version : "v18"}); is used to prevent the usage of WeakRef and FinalizationRegistry that are not supported by the Workers.

I can confirm this is fixed for me after the most recent push^

@gethassaan it is almost certainly a specific library or function you are using. I have routes in app/api that work just fine.

I’ve also been able to make progress by removing my ORM and using the D1 bindings and handwritten SQL instead. Prisma is broken for me, as is ATProto OAuth library (i’m using passwords for now, while I wait for a fix here, which should give me a better error so I know what is actually going wrong)

Was also hitting this and I finally narrowed it down to a not-found.tsx that tried to access headers:

import { headers } from 'next/headers';

export default async function NotFound() {
  const headersList = await headers();
  console.log(headersList);
  return (
    <div>
      <h2>Not Found</h2>
      <p>Could not find requested resource</p>
    </div>
  );
}

This code is pretty much straight from the next.js docs for creating a not found page. It’s the getHeaders() call that breaks the entire site and throws the renderHTML error

Thanks, This issue is on my radar as it makes debugging errors cumbersome. I’ll probably start investigating early next week.

@vicb

Here is a minimal reproducer: https://github.com/verdverm/cf-repro

hmm, have a feeling it might be dotenv, I think I saw an error at some earlier point in previous versions of the code about fs.readFile not being implemented yet…

either way the error message coming out doesn’t help me to RCA this. I did notice other endpoints which import the atprotoOauthClient (which imports dotenv) are also throwing 500 errors with the same error message


edit, dotenv does not seem to be the issue, removed it and still getting the same 500 & error message

edit2, I’ve done a bunch of edits to simplify things, still seeing the same error out of what I suspect is the ATProto package(s), I wonder if they are using some unsupported node:* package and the error is not bubbling up, or if it is the build process of these things causing the issue, or if the renderHtml is really about rendering the 500 page by next when it shouldn’t?