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:
npx create-next-app@latest next-blog --typescript --tailwind --eslint --app --src-dir --import-alias "@/*"
Key choices explained:
@/*Add OpenNext to your project:
npm 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:
{ "scripts": { "dev": "next dev --turbopack", "build": "next build", "start": "next start", "lint": "next lint", "ci:build": "opennextjs-cloudflare build", "deploy": "opennextjs-cloudflare build && opennextjs-cloudflare deploy", "preview": "opennextjs-cloudflare build && opennextjs-cloudflare preview", "cf-typegen": "wrangler types --env-interface CloudflareEnv ./cloudflare-env.d.ts" } }
Create wrangler.jsonc for Cloudflare Pages configuration:
{ "name": "next-blog", "compatibility_date": "2024-01-01", "compatibility_flags": ["nodejs_compat"] }
Update next.config.ts for optimal Cloudflare deployment:
import type { NextConfig } from "next"; const nextConfig: NextConfig = { output: "export", trailingSlash: true, images: { unoptimized: true } }; export default nextConfig;
Following the YAGNI principle, remove Tailwind CSS since we'll use Material-UI:
npm uninstall tailwindcss postcss autoprefixer
Also remove Tailwind configuration files:
rm tailwind.config.ts postcss.config.js
Start the development server:
npm run dev
Your application should now be running at http://localhost:3000.
Test the Cloudflare build process:
npm 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!