cloudflare-opennext
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCloudflare OpenNext
Cloudflare OpenNext
Deploy Next.js applications to Cloudflare Workers using the adapter with full support for App Router, Pages Router, ISR, SSG, and Cloudflare bindings.
@opennextjs/cloudflare使用适配器将Next.js应用部署到Cloudflare Workers,全面支持App Router、Pages Router、ISR、SSG以及Cloudflare绑定。
@opennextjs/cloudflareWhen to Use
适用场景
- Creating new Next.js apps for Cloudflare Workers
- Migrating existing Next.js apps to Cloudflare
- Configuring ISR/SSG caching with R2, KV, or D1
- Accessing Cloudflare bindings (KV, R2, D1, Durable Objects, AI)
- Using databases and ORMs (Drizzle, Prisma) in Next.js
- Troubleshooting deployment issues or bundle size problems
- 创建面向Cloudflare Workers的全新Next.js应用
- 将现有Next.js应用迁移到Cloudflare
- 配置基于R2、KV或D1的ISR/SSG缓存
- 访问Cloudflare绑定(KV、R2、D1、Durable Objects、AI)
- 在Next.js中使用数据库与ORM(Drizzle、Prisma)
- 排查部署问题或包体积相关故障
Getting Started
快速开始
New App
全新应用
bash
npm create cloudflare@latest -- my-next-app --framework=next --platform=workers
cd my-next-app
npm run dev # Local development with Next.js
npm run preview # Preview in Workers runtime
npm run deploy # Deploy to Cloudflarebash
npm create cloudflare@latest -- my-next-app --framework=next --platform=workers
cd my-next-app
npm run dev # 使用Next.js进行本地开发
npm run preview # 在Workers运行时中预览
npm run deploy # 部署到CloudflareExisting App Migration
现有应用迁移
bash
undefinedbash
undefined1. Install dependencies
1. 安装依赖
npm install @opennextjs/cloudflare@latest
npm install --save-dev wrangler@latest
npm install @opennextjs/cloudflare@latest
npm install --save-dev wrangler@latest
2. Create wrangler.jsonc (see Configuration section)
2. 创建wrangler.jsonc(查看配置章节)
3. Create open-next.config.ts
3. 创建open-next.config.ts
4. Update next.config.ts
4. 更新next.config.ts
5. Add scripts to package.json
5. 向package.json添加脚本
6. Deploy
6. 部署
npm run deploy
undefinednpm run deploy
undefinedCore Concepts
核心概念
How OpenNext Works
OpenNext工作原理
The adapter:
@opennextjs/cloudflare- Runs to generate the Next.js build output
next build - Transforms the build output to work in Cloudflare Workers runtime
- Outputs to directory with
.open-next/entry pointworker.js - Uses Workers Static Assets for static files (,
_next/static)public
@opennextjs/cloudflare- 运行生成Next.js构建产物
next build - 转换构建产物以适配Cloudflare Workers运行时
- 将输出文件写入目录,入口文件为
.open-next/worker.js - 使用Workers静态资源托管静态文件(、
_next/static)public
Node.js Runtime (Not Edge)
Node.js运行时(非Edge)
Critical: OpenNext uses Next.js Node.js runtime, NOT the Edge runtime:
typescript
// ❌ Remove this - Edge runtime not supported
export const runtime = "edge";
// ✅ Default Node.js runtime - fully supported
// No export needed, this is the defaultThe Node.js runtime provides:
- Full Node.js API compatibility via flag
nodejs_compat - More Next.js features than Edge runtime
- Access to all Cloudflare bindings
重点:OpenNext使用Next.js的Node.js运行时,而非Edge运行时:
typescript
// ❌ 请移除该行 - 不支持Edge运行时
export const runtime = "edge";
// ✅ 默认Node.js运行时 - 完全支持
// 无需额外导出,这是默认配置Node.js运行时提供以下特性:
- 通过标志实现完整的Node.js API兼容性
nodejs_compat - 比Edge运行时支持更多Next.js特性
- 可访问所有Cloudflare绑定
Configuration Files
配置文件
wrangler.jsonc
wrangler.jsonc
Minimal configuration for OpenNext:
jsonc
{
"$schema": "node_modules/wrangler/config-schema.json",
"name": "my-nextjs-app",
"main": ".open-next/worker.js",
"compatibility_date": "2024-12-30",
"compatibility_flags": [
"nodejs_compat", // Required for Node.js APIs
"global_fetch_strictly_public" // Security: prevent local IP fetches
],
"assets": {
"directory": ".open-next/assets", // Static files
"binding": "ASSETS"
},
"services": [
{
"binding": "WORKER_SELF_REFERENCE",
"service": "my-nextjs-app" // Must match "name" above
}
],
"images": {
"binding": "IMAGES" // Optional: Enable image optimization
}
}Required settings:
- compatibility flag
nodejs_compat - >=
compatibility_date2024-09-23 - service binding (must match worker name)
WORKER_SELF_REFERENCE - and
mainpaths should not be changedassets
See references/configuration.md for complete configuration with R2, KV, D1 bindings.
OpenNext的最小化配置:
jsonc
{
"$schema": "node_modules/wrangler/config-schema.json",
"name": "my-nextjs-app",
"main": ".open-next/worker.js",
"compatibility_date": "2024-12-30",
"compatibility_flags": [
"nodejs_compat", // Node.js API支持所需
"global_fetch_strictly_public" // 安全设置:禁止本地IP请求
],
"assets": {
"directory": ".open-next/assets", // 静态文件目录
"binding": "ASSETS"
},
"services": [
{
"binding": "WORKER_SELF_REFERENCE",
"service": "my-nextjs-app" // 必须与上方的"name"一致
}
],
"images": {
"binding": "IMAGES" // 可选:启用图片优化
}
}必填设置:
- 兼容性标志
nodejs_compat - 需大于等于
compatibility_date2024-09-23 - 服务绑定必须与Worker名称一致
WORKER_SELF_REFERENCE - 和
main路径请勿修改assets
查看references/configuration.md获取包含R2、KV、D1绑定的完整配置。
open-next.config.ts
open-next.config.ts
Configure caching and OpenNext behavior:
typescript
import { defineCloudflareConfig } from "@opennextjs/cloudflare";
import r2IncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cache/r2-incremental-cache";
export default defineCloudflareConfig({
incrementalCache: r2IncrementalCache,
});This file is auto-generated if not present. See references/caching.md for cache options.
配置缓存与OpenNext行为:
typescript
import { defineCloudflareConfig } from "@opennextjs/cloudflare";
import r2IncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cache/r2-incremental-cache";
export default defineCloudflareConfig({
incrementalCache: r2IncrementalCache,
});若该文件不存在,系统会自动生成。查看references/caching.md获取缓存选项详情。
next.config.ts
next.config.ts
Initialize OpenNext for local development:
typescript
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
// Your Next.js configuration
};
export default nextConfig;
// Enable bindings access during `next dev`
import { initOpenNextCloudflareForDev } from "@opennextjs/cloudflare";
initOpenNextCloudflareForDev();初始化OpenNext以支持本地开发:
typescript
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
// 你的Next.js配置
};
export default nextConfig;
// 在`next dev`期间启用绑定访问
import { initOpenNextCloudflareForDev } from "@opennextjs/cloudflare";
initOpenNextCloudflareForDev();.dev.vars
.dev.vars
Environment variables for local development:
bash
undefined本地开发使用的环境变量:
bash
undefined.dev.vars
.dev.vars
NEXTJS_ENV=development
The `NEXTJS_ENV` variable selects which Next.js `.env` file to load:
- `development` → `.env.development`
- `production` → `.env.production` (default)NEXTJS_ENV=development
`NEXTJS_ENV`变量用于选择Next.js加载的`.env`文件:
- `development` → `.env.development`
- `production` → `.env.production`(默认值)Accessing Cloudflare Bindings
访问Cloudflare绑定
Use to access bindings in any route:
getCloudflareContext()typescript
import { getCloudflareContext } from "@opennextjs/cloudflare";
// Route Handler (App Router)
export async function GET(request: Request) {
const { env, cf, ctx } = getCloudflareContext();
// Access KV
const value = await env.MY_KV.get("key");
// Access R2
const object = await env.MY_BUCKET.get("file.txt");
// Access D1
const result = await env.DB.prepare("SELECT * FROM users").all();
// Access Durable Objects
const stub = env.MY_DO.idFromName("instance-1");
const doResponse = await stub.fetch(request);
// Access request info
const country = cf?.country;
// Background tasks
ctx.waitUntil(logAnalytics());
return Response.json({ value });
}
// API Route (Pages Router)
export default async function handler(req, res) {
const { env } = getCloudflareContext();
const data = await env.MY_KV.get("key");
res.json({ data });
}
// Server Component
export default async function Page() {
const { env } = getCloudflareContext();
const data = await env.MY_KV.get("key");
return <div>{data}</div>;
}在任意路由中使用访问绑定:
getCloudflareContext()typescript
import { getCloudflareContext } from "@opennextjs/cloudflare";
// 路由处理器(App Router)
export async function GET(request: Request) {
const { env, cf, ctx } = getCloudflareContext();
// 访问KV
const value = await env.MY_KV.get("key");
// 访问R2
const object = await env.MY_BUCKET.get("file.txt");
// 访问D1
const result = await env.DB.prepare("SELECT * FROM users").all();
// 访问Durable Objects
const stub = env.MY_DO.idFromName("instance-1");
const doResponse = await stub.fetch(request);
// 访问请求信息
const country = cf?.country;
// 后台任务
ctx.waitUntil(logAnalytics());
return Response.json({ value });
}
// API路由(Pages Router)
export default async function handler(req, res) {
const { env } = getCloudflareContext();
const data = await env.MY_KV.get("key");
res.json({ data });
}
// 服务端组件
export default async function Page() {
const { env } = getCloudflareContext();
const data = await env.MY_KV.get("key");
return <div>{data}</div>;
}SSG Routes with Async Context
带异步上下文的SSG路由
For Static Site Generation routes, use async mode:
typescript
// In SSG route (generateStaticParams, etc.)
const { env } = await getCloudflareContext({ async: true });
const products = await env.DB.prepare("SELECT * FROM products").all();Warning: During SSG, secrets from and local binding values are included in the static build. Be careful with sensitive data.
.dev.vars对于静态站点生成路由,使用异步模式:
typescript
// 在SSG路由中(如generateStaticParams等)
const { env } = await getCloudflareContext({ async: true });
const products = await env.DB.prepare("SELECT * FROM products").all();警告:在SSG过程中,中的密钥和本地绑定值会被包含在静态构建产物中,请谨慎处理敏感数据。
.dev.varsTypeScript Types
TypeScript类型
Generate types for your bindings:
bash
npx wrangler types --env-interface CloudflareEnv cloudflare-env.d.tsAdd to :
package.jsonjson
{
"scripts": {
"cf-typegen": "wrangler types --env-interface CloudflareEnv cloudflare-env.d.ts"
}
}Run after any binding changes in .
wrangler.jsonc为绑定生成类型:
bash
npx wrangler types --env-interface CloudflareEnv cloudflare-env.d.ts向添加脚本:
package.jsonjson
{
"scripts": {
"cf-typegen": "wrangler types --env-interface CloudflareEnv cloudflare-env.d.ts"
}
}在中修改绑定后运行该脚本。
wrangler.jsoncCLI Commands
CLI命令
The CLI wraps Wrangler with OpenNext-specific behavior:
opennextjs-cloudflarebash
undefinedopennextjs-cloudflarebash
undefinedBuild the Next.js app and transform for Workers
构建Next.js应用并转换为Workers适配格式
npx opennextjs-cloudflare build
npx opennextjs-cloudflare build
Build and preview locally with Wrangler
构建并通过Wrangler本地预览
npm run preview
npm run preview
or
或
npx opennextjs-cloudflare preview
npx opennextjs-cloudflare preview
Build and deploy to Cloudflare
构建并部署到Cloudflare
npm run deploy
npm run deploy
or
或
npx opennextjs-cloudflare deploy
npx opennextjs-cloudflare deploy
Build and upload as a version (doesn't deploy)
构建并上传为版本(不部署)
npm run upload
npm run upload
or
或
npx opennextjs-cloudflare upload
npx opennextjs-cloudflare upload
Populate cache (called automatically by preview/deploy/upload)
填充缓存(预览/部署/上传时自动调用)
npx opennextjs-cloudflare populateCache local # Local bindings
npx opennextjs-cloudflare populateCache remote # Remote bindings
**Recommended package.json scripts**:
```json
{
"scripts": {
"dev": "next dev",
"build": "next build",
"preview": "opennextjs-cloudflare build && opennextjs-cloudflare preview",
"deploy": "opennextjs-cloudflare build && opennextjs-cloudflare deploy",
"upload": "opennextjs-cloudflare build && opennextjs-cloudflare upload",
"cf-typegen": "wrangler types --env-interface CloudflareEnv cloudflare-env.d.ts"
}
}npx opennextjs-cloudflare populateCache local # 本地绑定
npx opennextjs-cloudflare populateCache remote # 远程绑定
**推荐的package.json脚本**:
```json
{
"scripts": {
"dev": "next dev",
"build": "next build",
"preview": "opennextjs-cloudflare build && opennextjs-cloudflare preview",
"deploy": "opennextjs-cloudflare build && opennextjs-cloudflare deploy",
"upload": "opennextjs-cloudflare build && opennextjs-cloudflare upload",
"cf-typegen": "wrangler types --env-interface CloudflareEnv cloudflare-env.d.ts"
}
}Caching Strategies
缓存策略
OpenNext supports Next.js caching with Cloudflare storage:
| Cache Type | Use Case | Storage Options |
|---|---|---|
| Incremental Cache | ISR/SSG page data | R2, KV, Static Assets |
| Queue | Time-based revalidation | Durable Objects, Memory |
| Tag Cache | On-demand revalidation | D1, Durable Objects |
Quick setup examples:
typescript
// Static Site (SSG only)
import staticAssetsCache from "@opennextjs/cloudflare/overrides/incremental-cache/static-assets-incremental-cache";
export default defineCloudflareConfig({
incrementalCache: staticAssetsCache,
enableCacheInterception: true,
});
// Small Site with ISR
import r2IncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cache/r2-incremental-cache";
export default defineCloudflareConfig({
incrementalCache: r2IncrementalCache,
queue: doQueue,
tagCache: d1NextTagCache,
});See references/caching.md for complete caching patterns including regional cache and sharded tag cache
OpenNext支持通过Cloudflare存储实现Next.js缓存:
| 缓存类型 | 适用场景 | 存储选项 |
|---|---|---|
| 增量缓存 | ISR/SSG页面数据 | R2、KV、静态资源 |
| 队列 | 基于时间的重新验证 | Durable Objects、内存 |
| 标签缓存 | 按需重新验证 | D1、Durable Objects |
快速设置示例:
typescript
// 静态站点(仅SSG)
import staticAssetsCache from "@opennextjs/cloudflare/overrides/incremental-cache/static-assets-incremental-cache";
export default defineCloudflareConfig({
incrementalCache: staticAssetsCache,
enableCacheInterception: true,
});
// 带ISR的小型站点
import r2IncrementalCache from "@opennextjs/cloudflare/overrides/incremental-cache/r2-incremental-cache";
export default defineCloudflareConfig({
incrementalCache: r2IncrementalCache,
queue: doQueue,
tagCache: d1NextTagCache,
});查看references/caching.md获取完整缓存模式,包括区域缓存和分片标签缓存
Image Optimization
图片优化
Enable Cloudflare Images for automatic image optimization:
jsonc
// wrangler.jsonc
{
"images": {
"binding": "IMAGES"
}
}Next.js components will automatically use Cloudflare Images. Additional costs apply.
<Image>Compatibility notes:
- Supports: PNG, JPEG, WEBP, AVIF, GIF, SVG
- not supported
minimumCacheTTL - not supported
dangerouslyAllowLocalIP
启用Cloudflare Images实现自动图片优化:
jsonc
// wrangler.jsonc
{
"images": {
"binding": "IMAGES"
}
}Next.js 组件将自动使用Cloudflare Images。会产生额外费用。
<Image>兼容性说明:
- 支持格式:PNG、JPEG、WEBP、AVIF、GIF、SVG
- 不支持
minimumCacheTTL - 不支持
dangerouslyAllowLocalIP
Database and ORM Patterns
数据库与ORM模式
Critical Rule: Never create global database clients in Workers. Create per-request:
typescript
// ❌ WRONG - Global client causes I/O errors
import { Pool } from "pg";
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
// ✅ CORRECT - Per-request client
import { cache } from "react";
import { Pool } from "pg";
export const getDb = cache(() => {
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
maxUses: 1, // Don't reuse connections across requests
});
return drizzle({ client: pool, schema });
});
// Usage in route
export async function GET() {
const db = getDb();
const users = await db.select().from(usersTable);
return Response.json(users);
}See references/database-orm.md for Drizzle and Prisma patterns.
关键规则:切勿在Workers中创建全局数据库客户端,应按请求创建:
typescript
// ❌ 错误 - 全局客户端会导致I/O错误
import { Pool } from "pg";
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
// ✅ 正确 - 按请求创建客户端
import { cache } from "react";
import { Pool } from "pg";
export const getDb = cache(() => {
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
maxUses: 1, // 不要跨请求复用连接
});
return drizzle({ client: pool, schema });
});
// 在路由中使用
export async function GET() {
const db = getDb();
const users = await db.select().from(usersTable);
return Response.json(users);
}查看references/database-orm.md获取Drizzle和Prisma的配置模式。
Critical Rules
关键规则
✅ DO
✅ 建议
- Use Node.js runtime - Default runtime, remove any
export const runtime = "edge" - Create DB clients per-request - Use React's for request-scoped instances
cache() - Enable nodejs_compat - Required compatibility flag with date >= 2024-09-23
- Use getCloudflareContext() - Access bindings, not getRequestContext from next-on-pages
- Add .open-next to .gitignore - Build output should not be committed
- Use wrangler.jsonc - Not wrangler.toml (JSONC supports comments and validation)
- Set WORKER_SELF_REFERENCE - Service binding must match worker name
- Add public/_headers - Configure static asset caching headers
- 使用Node.js运行时 - 默认运行时,移除所有代码
export const runtime = "edge" - 按请求创建数据库客户端 - 使用React的创建请求作用域的实例
cache() - 启用nodejs_compat - 必需的兼容性标志,日期需大于等于2024-09-23
- 使用getCloudflareContext() - 访问绑定,而非next-on-pages中的getRequestContext
- 将.open-next添加到.gitignore - 构建产物无需提交到版本库
- 使用wrangler.jsonc - 不要使用wrangler.toml(JSONC支持注释和验证)
- 设置WORKER_SELF_REFERENCE - 服务绑定必须与Worker名称一致
- 添加public/_headers - 配置静态资源缓存头
❌ DON'T
❌ 禁止
- Don't use Edge runtime - Remove from all routes
export const runtime = "edge" - Don't use Turbopack - Use , not
next buildnext build --turbo - Don't create global DB clients - Causes "Cannot perform I/O" errors
- Don't exceed 10 MiB - Worker size limit (3 MiB on free plan)
- Don't use next-on-pages - Different adapter, use @opennextjs/cloudflare instead
- Don't commit .open-next/ - Build output directory
- Don't use Node Middleware - Not supported (Next.js 15.2+ feature)
- 不要使用Edge运行时 - 从所有路由中移除
export const runtime = "edge" - 不要使用Turbopack - 使用,而非
next buildnext build --turbo - 不要创建全局数据库客户端 - 会导致"无法执行I/O"错误
- 不要超过10 MiB - Worker体积限制(免费计划为3 MiB)
- 不要使用next-on-pages - 这是不同的适配器,请使用@opennextjs/cloudflare
- 不要提交.open-next/ - 构建产物目录
- 不要使用Node Middleware - 不支持(Next.js 15.2+新增特性)
Supported Features
支持的特性
| Feature | Support | Notes |
|---|---|---|
| App Router | ✅ Full | All features supported |
| Pages Router | ✅ Full | Including API routes |
| Route Handlers | ✅ Full | GET, POST, etc. |
| Dynamic Routes | ✅ Full | |
| SSG | ✅ Full | Static Site Generation |
| SSR | ✅ Full | Server-Side Rendering |
| ISR | ✅ Full | Incremental Static Regeneration |
| PPR | ✅ Full | Partial Prerendering |
| Middleware | ✅ Partial | Standard middleware works, Node Middleware (15.2+) not supported |
| Image Optimization | ✅ Full | Via Cloudflare Images binding |
| Composable Caching | ✅ Full | |
| next/font | ✅ Full | Font optimization |
| after() | ✅ Full | Background tasks |
| Turbopack | ❌ No | Use standard build |
Supported Next.js versions:
- Next.js 15: All minor and patch versions
- Next.js 14: Latest minor version only
| 特性 | 支持状态 | 说明 |
|---|---|---|
| App Router | ✅ 完全支持 | 所有特性均支持 |
| Pages Router | ✅ 完全支持 | 包括API路由 |
| 路由处理器 | ✅ 完全支持 | GET、POST等方法 |
| 动态路由 | ✅ 完全支持 | |
| SSG | ✅ 完全支持 | 静态站点生成 |
| SSR | ✅ 完全支持 | 服务端渲染 |
| ISR | ✅ 完全支持 | 增量静态再生 |
| PPR | ✅ 完全支持 | 部分预渲染 |
| 中间件 | ✅ 部分支持 | 标准中间件可用,Node Middleware(15.2+)不支持 |
| 图片优化 | ✅ 完全支持 | 通过Cloudflare Images绑定实现 |
| 可组合缓存 | ✅ 完全支持 | |
| next/font | ✅ 完全支持 | 字体优化 |
| after() | ✅ 完全支持 | 后台任务 |
| Turbopack | ❌ 不支持 | 使用标准构建命令 |
支持的Next.js版本:
- Next.js 15: 所有小版本和补丁版本
- Next.js 14: 仅最新小版本
Development Workflow
开发流程
bash
undefinedbash
undefinedLocal development with Next.js dev server
使用Next.js开发服务器进行本地开发
npm run dev
npm run dev
Preview in Workers runtime (faster than deploy)
在Workers运行时中预览(比部署更快)
npm run preview
npm run preview
Deploy to production
部署到生产环境
npm run deploy
npm run deploy
Update TypeScript types after binding changes
修改绑定后更新TypeScript类型
npm run cf-typegen
**Local Development Notes**:
- `next dev` - Uses Node.js runtime, bindings available via `initOpenNextCloudflareForDev()`
- `npm run preview` - Uses Workers runtime with Wrangler, closer to production
- Both support hot reloadingnpm run cf-typegen
**本地开发说明**:
- `next dev` - 使用Node.js运行时,通过`initOpenNextCloudflareForDev()`可访问绑定
- `npm run preview` - 使用Wrangler运行Workers运行时,更贴近生产环境
- 两者均支持热重载Detailed References
详细参考文档
- references/configuration.md - Complete wrangler.jsonc, environment variables, TypeScript types
- references/caching.md - ISR, SSG, R2/KV/D1 caches, tag cache, queues, cache purge
- references/database-orm.md - Drizzle, Prisma setup with D1, PostgreSQL, Hyperdrive
- references/troubleshooting.md - Size limits, bundle analysis, common errors
- references/configuration.md - 完整的wrangler.jsonc、环境变量、TypeScript类型配置
- references/caching.md - ISR、SSG、R2/KV/D1缓存、标签缓存、队列、缓存清理
- references/database-orm.md - Drizzle、Prisma与D1、PostgreSQL、Hyperdrive的配置
- references/troubleshooting.md - 体积限制、包分析、常见错误
Migration from @cloudflare/next-on-pages
从@cloudflare/next-on-pages迁移
If migrating from :
@cloudflare/next-on-pages- Uninstall and
@cloudflare/next-on-pageseslint-plugin-next-on-pages - Install
@opennextjs/cloudflare - Update :
next.config.ts- Remove calls
setupDevPlatform() - Replace with
initOpenNextCloudflareForDev()
- Remove
- Update imports:
- Replace from
getRequestContext@cloudflare/next-on-pages - Use from
getCloudflareContext@opennextjs/cloudflare
- Replace
- Remove Edge runtime exports ()
export const runtime = "edge" - Update wrangler.jsonc with required OpenNext settings
- Remove next-on-pages eslint rules
若从迁移:
@cloudflare/next-on-pages- 卸载和
@cloudflare/next-on-pageseslint-plugin-next-on-pages - 安装
@opennextjs/cloudflare - 更新:
next.config.ts- 移除调用
setupDevPlatform() - 替换为
initOpenNextCloudflareForDev()
- 移除
- 更新导入:
- 替换中的
@cloudflare/next-on-pagesgetRequestContext - 使用中的
@opennextjs/cloudflaregetCloudflareContext
- 替换
- 移除Edge运行时导出()
export const runtime = "edge" - 更新wrangler.jsonc以添加OpenNext必需设置
- 移除next-on-pages的ESLint规则
Examples
示例
Official examples in the @opennextjs/cloudflare repository:
- - Basic Next.js starter
create-next-app - - Middleware usage
middleware - - SSG blog example
vercel-blog-starter
官方示例位于@opennextjs/cloudflare仓库:
- - 基础Next.js启动模板
create-next-app - - 中间件使用示例
middleware - - SSG博客示例
vercel-blog-starter
Best Practices
最佳实践
- Start simple - Use Static Assets cache for SSG-only sites
- Add caching gradually - Enable R2 cache when you need ISR
- Monitor bundle size - Stay under 10 MiB compressed (use ESBuild Bundle Analyzer)
- Use TypeScript - Run to get binding types
cf-typegen - Test with preview - Use before deploying
npm run preview - Cache database clients - Use React's for per-request instances
cache() - Enable observability - Add to wrangler.jsonc for logging
observability - Use remote bindings for build - Enable for ISR with real data
- 从简单开始 - 仅SSG的站点使用静态资源缓存
- 逐步添加缓存 - 需要ISR时再启用R2缓存
- 监控包体积 - 保持压缩后体积在10 MiB以内(使用ESBuild Bundle Analyzer)
- 使用TypeScript - 运行获取绑定类型
cf-typegen - 预览后再部署 - 部署前使用测试
npm run preview - 缓存数据库客户端 - 使用React的创建按请求实例
cache() - 启用可观测性 - 在wrangler.jsonc中添加配置以启用日志
observability - 构建时使用远程绑定 - 为ISR启用真实数据访问
Common Patterns
常见模式
See references/configuration.md for complete examples including:
- Custom Worker with multiple handlers (fetch, scheduled, queue)
- Environment-specific configuration (staging, production)
- Remote bindings for build-time data access
查看references/configuration.md获取完整示例,包括:
- 带多个处理器的自定义Worker(fetch、scheduled、queue)
- 环境专属配置( staging、production)
- 构建时数据访问的远程绑定