从 0.2 迁移到 0.3 版本
@opennextjs/cloudflare 适配器现在与 @opennextjs/aws 集成更加紧密。
您需要根据以下章节说明更新基于 0.2 版本的项目。
1. 更新 wrangler.toml 文件
入口点现在改为 .open-next/worker.js,请相应更新 wrangler.toml:
# 变更:新的入口点位置
main = ".open-next/worker.js"
name = "my-app"
compatibility_date = "2024-09-23"
compatibility_flags = ["nodejs_compat"]
# 当启用缓存时,绑定名称必须为 "ASSETS"
# 变更:输出文件夹位置
assets = { directory = ".open-next/assets", binding = "ASSETS" }2. 添加 open-next.config.ts 文件
在 Next.js 应用的根目录下添加 open-next.config.ts (opens in a new tab) 文件:
import type { OpenNextConfig } from "@opennextjs/aws/types/open-next.js";
import cache from "@opennextjs/cloudflare/kvCache";
const config: OpenNextConfig = {
default: {
override: {
wrapper: "cloudflare-node",
converter: "edge",
// 设置为 "dummy" 可禁用 KV 缓存
incrementalCache: async () => cache,
tagCache: "dummy",
queue: "dummy",
},
},
middleware: {
external: true,
override: {
wrapper: "cloudflare-edge",
converter: "edge",
proxyExternalRequest: "fetch",
},
},
};
export default config;您可以选择安装 @opennextjs/aws NPM 包来获取类型支持,或者将 open-next.config.ts 添加到 tsconfig.json 的 exclude (opens in a new tab) 配置项中。
3. 添加 .dev.vars 文件
在 Next.js 应用的根目录下添加一个 .dev.vars (opens in a new tab) 文件:
NEXTJS_ENV=developmentNEXTJS_ENV 变量定义了加载 Next.js .env 文件时使用的环境。当未定义时,默认为 "production"。
4. 更新 package.json
CLI 的名称已更改为 opennextjs-cloudflare:
"build:worker": "opennextjs-cloudflare",5. 将 .open-next 添加到 .gitignore
你应该在 .gitignore 文件中将 .worker-next 改为 .open-next,以防止构建输出被提交到代码仓库。
现在可以安全地删除已不再使用的 .worker-next 目录中的内容。