DEV Community

Cover image for Cloudflare Pages and Next.js: I'm Not Recommending It
Thomas Desmond
Thomas Desmond

Posted on • Originally published at thetombomb.com

Cloudflare Pages and Next.js: I'm Not Recommending It

As a developer, I am always looking for ways to test the limits of different hosting providers and see how well they support different frameworks and technologies. I recently tested Cloudflare Pages with my Next.js app, and the results differed from what I expected. In this article, I'll walk you through my experience and the changes I had to make to get my app to work on Cloudflare Pages.

Next.js Host Check App

Before I dive into my experience with Cloudflare Pages, let me give you some background on my app. I have built an app that tests 13 of the most essential features of Next.js Pages Architecture. These features include server-side rendering, image optimization, and API routes, among others. I've put Cloudflare Pages to the test. I'm deploying this application on different hosting providers to see how well they support Next.js and what features they do and don't support.

My app is a Next.js Pages Architecture application built with version 13.4.16. I attempted to deploy it out of the box to different hosting providers. However, I ran into some difficulties with Cloudflare Pages.

GitHub: Host-Check-Next.js

My Experience with Next.js on Cloudflare Pages

The biggest thing I came across was the edge runtime requirement. All server-side routes in your Next.js project must be configured as edge runtime routes. When running on Cloudflare Pages, you must add export const runtime=edge to each server route.

This means you are limited to the edge runtime for Incremental Static Regeneration (ISR), getServerSideProps(), API Routes, and everything else server-side.

❓ What is the Edge Runtime? From Vercel, the edge runtime provides a subset of web APIs such as fetch, request, and response. This lightweight API layer is built to be performant and execute code with minimal latency. See Vercel's documentation for a full list of available APIs.

So, I added the line export const runtime=edge to every file, but the build still failed. Instead, I had to use export const runtime=experimental-edge, which got my Next.js app to at least build in Cloudflare Pages.

But that was not the end of my problems. I had to add a nodejs_compat flag within Cloudflare Pages. I'm not sure why, but I did.

Cloudflare Pages Node Compatability Error

After one more final rebuild, my app was built and deployed correctly. I could access my application live and deploy it to the web. Finally, it was time to test what features of Next.js worked.

Next.js Feature Support in Cloudflare Pages

With the host-check-nextjs app deployed to Cloudflare Pages, I could now run the automated Cypress tests against the deployed app.
Here are the results of the automated testing. These are the worst results so far in my testing of Next.js hosting providers.

Next.js Feature Support
Next.js Version 13.4.16 Pages Architecture
getStaticProps()
getServerSideProps() ⚠ (Edge Runtime Only)
Image Optimization
Incremental Static Regeneration (ISR) ⚠ (Edge Runtime Only)
API Routes
Middleware Redirects ⚠ (Edge Runtime Only)
Edge Runtime
Dynamic Routes ⚠ (Edge Runtime Only)
next.config.js Redirects
next.config.js Rewrites
Sub-path Internationalization
Sitecore JSS Image Optimization

While still working, the edge runtime limits anything that runs on the server, like ISR and getServerSideProps(). You can also see standard API routes are not working and no out of the box image optimization.

The absence of image optimization is a major issue for me. Optimized images are essential to creating a modern and efficient website. The fact that Cloudflare Pages does not utilize the built-in next/image components, which are designed to optimize images for the web, is a deal breaker. This could lead to slower page load times and a less optimal user experience.

Conclusion

In conclusion, my experience with Cloudflare Pages and Next.js was challenging, but I got my app to work with some specific changes. Cloudflare Pages only supports the edge runtime for its server-side routes, so you are limited to the available APIs. However, Cloudflare Pages might be perfect if you're looking for a more performant edge runtime with a smaller minimal latency API layer— I don't expect the number of people with that specific need to be very high.

Understanding the limitations and requirements when working with Cloudflare Pages and Next.js is crucial. This post aims to provide you with valuable insights from my experience, equipping you with the knowledge you need for your own testing and deployment of Next.js apps.

Interested in other Next.js hosting providers? Check out my other test runs:

AWS Amplify Support for Next.js

How Well Does Azure Static Web Apps (SWA) Support Next.js?

Or watch the video version of this article: Why Cloudflare Pages May Not Be the Best For Next.js 🤷‍♂️

Top comments (4)

Collapse
 
fyodorio profile image
Fyodor

Insightful, thanks for the heads up 👍 is Netlify on the list? Would be interesting to know what is the real situation with their support for the corresponding Next features.

Collapse
 
thetombomb profile image
Thomas Desmond

Netlify video is out! youtu.be/ItyUwNVT1jI

Collapse
 
thetombomb profile image
Thomas Desmond

Netlify is on the list! Netlify released their version 5 runtime for Next.js and I do want to give it a try.

Collapse
 
neurabot profile image
Neurabot

Morning. I have some issues. I don't know where I must put on servernames Cloudflares. and my website is pending. I choose free plan.