Cloudflare
历史版本
从 0.2 迁移到 0.3

从 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 文件

open-next.config.ts (opens in a new tab) 文件添加到您的 Next.js 应用的根目录:

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",
      // 将 `incrementalCache` 设置为 "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.jsonexclude (opens in a new tab) 配置键中。

3. 添加 .dev.vars 文件

.dev.vars (opens in a new tab) 文件添加到您的 Next.js 应用的根目录:

NEXTJS_ENV=development

NEXTJS_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 的内容。