Quick Start
Create a new Hono project for Cloudflare Pages:Installation
Add Hono to your existing Cloudflare Pages project:Basic Usage
Cloudflare Pages uses afunctions directory for serverless functions. You can use Hono with the advanced mode.
Advanced Mode Setup
Createfunctions/[[route]].ts to handle all routes:
functions/[[route]].ts
The
handle adapter converts your Hono app into a Pages Functions handler.Adapter Features
Handle Function
Thehandle function wraps your Hono app for Cloudflare Pages:
functions/[[route]].ts
Middleware Handler
UsehandleMiddleware to create middleware for Pages Functions:
functions/_middleware.ts
Accessing EventContext
Access the Cloudflare PageseventContext:
Static Files
Serve static files from thepublic directory:
functions/[[route]].ts
Connection Info
Get connection information:Accessing Bindings
Access KV, D1, R2, and other bindings:functions/[[route]].ts
Configuration
Configure bindings inwrangler.toml:
wrangler.toml
Deployment
1
Build your project
Build your frontend and backend:
2
Deploy with Wrangler
Deploy using Wrangler:
3
Deploy via Git
Alternatively, connect your Git repository in the Cloudflare dashboard:
- Go to Cloudflare Pages
- Click “Create a project”
- Connect your repository
- Configure build settings
- Deploy
Project Structure
Typical Cloudflare Pages + Hono project structure:Development
Run locally with Wrangler:http://localhost:8788 with hot reload.
Combining with Frontend Frameworks
Cloudflare Pages works great with frontend frameworks:With React/Vite
package.json
With Next.js
Use the@cloudflare/next-on-pages adapter for Next.js on Cloudflare Pages.
Best Practices
Separate API and static routes
Separate API and static routes
Use path prefixes like
/api/* for backend routes to avoid conflicts with static files.Use Advanced Mode
Use Advanced Mode
Advanced Mode (
functions/[[route]].ts) gives you full control over routing.Leverage bindings
Leverage bindings
Use KV, D1, and R2 for data storage instead of external databases.
Optimize bundle size
Optimize bundle size
Keep your functions directory small for faster cold starts.
Resources
Cloudflare Pages Docs
Official Cloudflare Pages documentation
Pages Functions
Learn about Pages Functions