2025-07-21 by Remi Kristelijn
In this first post of our series, I'll show you how to create a Next.js project from scratch that's optimized for Cloudflare deployment using OpenNext. This approach makes deployment incredibly simple and efficient.
OpenNext is a framework that adapts Next.js applications for edge deployment platforms like Cloudflare Pages. It handles the complexity of server-side rendering, static generation, and edge functions automatically, making deployment straightforward.
Start with the official Next.js template:
1npx create-next-app@latest next-blog --typescript --tailwind --eslint --app --src-dir --import-alias "@/*"
Key choices explained:
@/*Add OpenNext to your project:
1npm install @opennextjs/cloudflare
This package provides the tools needed to build and deploy Next.js applications to Cloudflare Pages.
Update your package.json scripts for Cloudflare deployment:
1{ 2 "scripts": { 3 "dev": "next dev --turbopack", 4 "build": "next build", 5 "start": "next start", 6 "lint": "next lint", 7 "ci:build": "opennextjs-cloudflare build", 8 "deploy": "opennextjs-cloudflare build && opennextjs-cloudflare deploy", 9 "preview": "opennextjs-cloudflare build && opennextjs-cloudflare preview", 10 "cf-typegen": "wrangler types --env-interface CloudflareEnv ./cloudflare-env.d.ts" 11 } 12}
Create wrangler.jsonc for Cloudflare Pages configuration:
1{ 2 "name": "next-blog", 3 "compatibility_date": "2024-01-01", 4 "compatibility_flags": ["nodejs_compat"] 5}
Update next.config.ts for optimal Cloudflare deployment:
1import type { NextConfig } from "next"; 2 3const nextConfig: NextConfig = { 4 output: "export", 5 trailingSlash: true, 6 images: { 7 unoptimized: true 8 } 9}; 10 11export default nextConfig;
Following the YAGNI principle, remove Tailwind CSS since we'll use Material-UI:
1npm uninstall tailwindcss postcss autoprefixer
Also remove Tailwind configuration files:
1rm tailwind.config.ts postcss.config.js
Start the development server:
1npm run dev
Your application should now be running at http://localhost:3000.
Test the Cloudflare build process:
1npm run ci:build
This command builds your application specifically for Cloudflare deployment.
next-blog/
├── src/
│ ├── app/ # Next.js App Router pages
│ ├── components/ # UI components (we'll add these later)
│ └── lib/ # Utility functions
├── public/ # Static assets
├── wrangler.jsonc # Cloudflare configuration
├── next.config.ts # Next.js configuration
└── package.json # Dependencies and scripts
In the next post, we'll set up automated deployment using GitHub Actions and Cloudflare Pages. This will enable continuous deployment whenever you push changes to your repository.
If you encounter any issues:
wrangler.jsonc configurationThis completes the initial project setup. You now have a Next.js project ready for Cloudflare deployment. In the next post, we'll automate the deployment process with GitHub Actions!