我已在 Cloudflare Pages 上部署了 Next.js 13 应用程序。由于它有一些 API 路由,我必须从每个路由导出运行时变量,如下所示。
export const runtime = "edge"
但是,当我进行本地开发时,我需要将其更改为“nodejs”。因此,我尝试通过 Next.js 环境变量提供此值,但它在 Cloudflare 上不起作用。
有人知道在生产和开发这两个运行时之间切换的更简单方法吗?预先感谢!
我认为我们可以通过使用 .env 文件来配置生产或开发中的变量来存档您的问题。
.env
用于生产,.env.local
用于开发。
更新: 转到文件
next.config.js
并添加配置:
const nextConfig = {
...
serverRuntimeConfig: {
runtime: process.env.RUNTIME
}
...
}
现在我们可以将此配置与
next/config
包一起使用。例如。
// /app/pages.tsx
import getConfig from 'next/config';
...
const {serverRuntimeConfig} = getConfig();
console.log(serverRuntimeConfig.runtime); => the value depends on .env or .env.locale file.
它对我有用。