Cloudflare Pages can host a Next.js app through the OpenNext adapter, with Cloudflare's edge network, Workers and R2 storage. It is strongest when you already use Cloudflare and want global static delivery.
Key takeaways
- Best for: content-heavy sites and apps that benefit from edge delivery and Cloudflare's storage/security products.
- Not ideal for: apps that depend on Node APIs the Workers runtime does not support, or on a long-lived local filesystem.
- Pin the Node version and keep production secrets out of the repository.
- Test auth, forms, images and redirects on a preview URL before promoting.
- Choose the region close to your database and users, and move long jobs to a worker.
What Cloudflare Pages expects from a Next.js app
A Next.js deployment has four parts: a build, a runtime, environment-specific configuration and persistent services. The build turns your routes into server and client output. The runtime serves dynamic requests and static assets. Environment values connect the app to its database and third-party APIs. Persistent services — database, object storage, queues — must live outside the app container.
| Layer | What to check before deploying |
|---|---|
| Build | Node version, install command, monorepo root, build output |
| Runtime | Serverless, container or static; timeout and memory limits |
| Configuration | Production and preview environment variables; secrets |
| Data | Managed database, connection pooling, backups and migrations |
| Files | Object storage for uploads; no reliance on the local filesystem |
| Observability | Logs, errors, uptime checks and a spending limit |
Step-by-step deployment
- Choose the adapter. Use the current Cloudflare/OpenNext adapter for Next.js rather than a static export if you need server rendering.
- Set the build command. Follow the adapter's build output and configuration; do not assume the default Next.js build works unchanged.
- Create bindings. Add KV, R2, D1 or Hyperdrive bindings for storage and database access.
- Set secrets. Use Wrangler secrets or the dashboard, and separate preview from production.
- Test the preview. Check dynamic routes, auth, forms and image handling on the Pages preview.
- Monitor. Use Workers analytics, logs and error tracking; watch CPU time and request limits.
Environment variables and secrets
- Workers have no Node
fs; use R2 or another object store. - Use Hyperdrive or an HTTP database driver for Postgres from the edge.
- Keep secrets out of
wrangler.toml.
What usually breaks in production
| Symptom | Likely cause | Fix |
|---|---|---|
| Build passes locally, fails on the host | Different Node version or missing lockfile | Pin the runtime and commit the lockfile |
| Auth callback loops | Wrong callback URL or cookie domain | Set the production URL and secure cookie settings |
| Images 404 or unoptimised | Image loader or static export mismatch | Configure the loader or use a CDN |
| Database connection errors | Wrong region, pooling or TLS settings | Use the internal URL, a pooler and the required TLS mode |
| Uploads disappear | Ephemeral filesystem | Move uploads to object storage |
| Long request times out | Work is running in the request path | Move it to a queue or background worker |
Limits and cost on Cloudflare Pages
Pages and Workers have free allowances, then usage-based pricing for requests, CPU and storage. R2 has its own storage and egress pricing.
Do not skip the preview
A preview deployment is not a production test if it uses a different database and a different auth callback. Test the real user journey with production-like data before you promote.
When to choose something else
Choose Vercel or a Node container host if you need full Node compatibility when the trade-offs above do not fit. The right platform is the one your team can operate safely at 2am, not the one with the best demo.
Pre-launch checklist
- The production domain, HTTPS and canonical URL are correct.
- Auth callbacks, cookies and email links use the production origin.
- Database migrations have run, and a restore has been tested.
- Every environment variable exists in production, not just preview.
- A test order, form or sign-up completes end to end.
- Robots, sitemap, analytics and consent behave as expected.
- The error page, 404 and a hard refresh on a deep route all work.
Rollback and recovery
| Failure | Immediate action | What to verify |
|---|---|---|
| Bad application release | Promote the previous build | The database change is backward compatible |
| Broken migration | Stop writes and restore from a verified backup | Data loss window and reconciliation |
| Auth outage | Roll back the auth configuration | Sessions and callback URLs |
| Payment errors | Disable the failing method | Pending orders and webhook retries |
| Traffic spike | Scale or enable a queue | Database connections and rate limits |
| Region incident | Fail over or restore elsewhere | DNS, data replication and RPO |
Observability after launch
| Signal | What to watch | Why |
|---|---|---|
| Errors | New error groups and affected routes | Catch regressions before support does |
| Latency | p95 and p99 by route | Find slow queries and cold starts |
| Database | Connections, slow queries and storage | The most common bottleneck |
| Jobs | Queue depth and failed retries | Silent backlog |
| Vitals | LCP, INP and CLS by device | Real-user experience |
| Cost | Spend and egress | Avoid a surprise invoice |
Frequently asked questions
Not necessarily. Check the runtime features you use: Server Actions, dynamic routes, image optimisation, middleware, WebSockets, background jobs and native dependencies. Apps that depend on Node APIs the Workers runtime does not support, or on a long-lived local filesystem.
Use a monorepo when the app shares code or types with other packages and the platform supports a root directory. Use a separate repository when the deployment is simpler and the app has no shared build pipeline. The important thing is to pin the root, install command and lockfile.
Run migrations as a separate deployment step with a lock, not on every app boot. Back up first, test against a staging database and have a rollback plan. If the platform has no release phase, run the migration from CI before promoting the app.
Error rate, p95 response time, database connections, background-job failures, Core Web Vitals and the main conversion event. Add an uptime check from outside the platform and a billing alert.
Sources and further reading
- Next.js deployment guide (nextjs.org)
- Next.js self-hosting guide (nextjs.org)
- Cloudflare Pages Next.js guide (developers.cloudflare.com)
Related reading
Need a deployment review?
Send the stack, the hosting plan and the launch date. I will flag the runtime limits, data risks and missing checks before you go live.
How to cite this guide: Nur Hossain Repon, “Deploy a Next.js app to Cloudflare Pages: step-by-step guide”, Nhrepon Portfolio, reviewed October 2026, https://nhrepon.com/posts/deploy-a-next-js-app-to-cloudflare-pages-step-by-step-guide.





Comments
No comments yet.