CloudflareDeploymentPerformance

Deploying Next.js to Cloudflare Pages: A Complete Guide

M

Marcus Johnson

January 15, 2025

9 min read

Why Cloudflare Pages?

Cloudflare Pages has become one of the most compelling platforms for deploying web applications. Running your code on Cloudflare's global network of 300+ data centers means your users experience sub-millisecond response times regardless of their location.

Key advantages include zero cold starts (Workers run instantly), a global edge network (code runs 50ms from 95% of the world's internet users), a generous free tier (100,000 requests per day with unlimited bandwidth), built-in DDoS protection, and integrated KV, R2, and D1 edge-native storage solutions.

Setting Up @cloudflare/next-on-pages

The @cloudflare/next-on-pages package transforms your Next.js build output to run on Cloudflare's Edge Runtime. Install it with: npm install @cloudflare/next-on-pages wrangler

Update your next.config.ts to set images.unoptimized to true, which is required for Cloudflare Pages since it doesn't support Next.js Image Optimization natively.

Create a wrangler.toml file with your project name, compatibility_date set to 2024-09-23, the nodejs_compat compatibility flag, and pages_build_output_dir pointing to .vercel/output/static.

Build and Deploy Scripts

Add build scripts to your package.json. The cf:build script runs npx @cloudflare/next-on-pages. The cf:preview script builds and then previews locally with wrangler pages dev. The deploy script builds and deploys to Cloudflare Pages.

Edge Runtime Considerations

Cloudflare Workers use the Edge Runtime, which is more restricted than Node.js. You cannot use the Node.js fs module for file reading at runtime, native Node.js modules are not available, and Request/Response use the Web standard API.

For blog content, use static data in TypeScript files or connect to a CMS API. Using generateStaticParams to pre-render all blog post pages at build time is the recommended approach.

Using Cloudflare D1 for Data

D1 is Cloudflare's edge-native SQLite database. Access it from your Next.js app using the getRequestContext function from @cloudflare/next-on-pages. This gives you access to the env object which contains your D1 binding.

Performance Optimization Tips

Use generateStaticParams for blog posts to pre-render at build time. Leverage Cloudflare Cache by setting appropriate Cache-Control headers. Use Cloudflare Images to transform and optimize images at the edge. Enable Smart Placement so Cloudflare automatically routes requests to optimal workers.

Monitoring and Analytics

Cloudflare Pages includes built-in analytics covering real-time request logs, Core Web Vitals tracking, error rate monitoring, and geographic traffic distribution.

Conclusion

Cloudflare Pages is an excellent choice for Next.js applications that need global performance, zero cold starts, and competitive pricing. The @cloudflare/next-on-pages adapter makes the migration straightforward, and the edge-native storage options provide everything you need for a full-featured application.

Tagged with

CloudflareDeploymentPerformance