Fixing the dreaded "invariant HTML in an API route" error when moving from Cloudflare to Vercel
We begrudgingly migrated our Project Tracker's deploy from Cloudflare to Vercel. Here's why and what we learned along the way.
This wasn't a casual platform switch - it was driven by a specific technical roadblock:
The migration was focused and tactical - solving the compile errors while maintaining our architecture.
Our project management chat system isn't just a chatbox. It's a complex integration of:
The migration was more straightforward than we initially thought:
No complex architecture changes were needed - just targeted fixes to address the specific compiler errors.
The migration required methodical isolation:
Root Cause:
# Error in Cloudflare logs
Error: Invariant: HTML functionality was requested but not enabledTest Case:
wrangler environmentnext dev but breaking when deployedvercel environment as a production build and it just workedIt seems that a part of the OpenNext/Cloudflare translation (wherein it takes the output of next build and re-packages it for Cloudflare compatibility) is mis-mapping... but it's difficult to isolate. Very possibly a problem within Next.js or React itself.
After the migration, we solved our critical issue:
The primary win was simple: our chat system actually worked in production again.
For anyone dealing with Next.js Edge Runtime errors or OpenNext/Cloudflare issues:
The most important takeaway: when facing compiler issues that only appear in production, focus on build configuration first. Your actual code is probably fine.
If you're using Next.js with Edge Runtime in non-Vercel environments, watch for these specific error patterns:
# Classic "invariant HTML" error
Error: Invariant: HTML functionality was requested but not enabled.
at /node_modules/next/dist/server/base-server.jsThese errors typically indicate an underlying translation/compiler issue with the build output. Thankfully, the OpenNext team is aware of it: https://github.com/opennextjs/opennextjs-cloudflare/issues/333
This is just the beginning of our blarticles. Coming soon:
Need help with your own migration? Contact us for a technical consultation.
Sometimes the most important technical migrations aren't about grand architectural changes—they're about fixing that one compiler error that's blocking everything else.
Important notes:
I will probably migrate it back to Cloudflare once this specific issue is resolved: https://github.com/opennextjs/opennextjs-cloudflare/issues/333
It's always risky binding to "not ready for production" packages, but such is the cost of pushing what's possible.
I'm currently working on improving the chat system to use a proper tRPC API path (instead of a Next.js API route) and enhancing the onboarding flow before opening up the link. If you're interested in a beta, hit me up for an invite!