admin管理员组文章数量:1401673
How do you access environmental variables on edge runtime in NextJS using @cloudflare/next-on-pages? Right now I am using custom Cloudflare Worker deployed separately with the following code:
import Stripe from "stripe";
...
export default {
async fetch(request: Request, env: {STRIPE_SECRET_KEY: string}): Promise<Response> {
const stripe = new Stripe(env.STRIPE_SECRET_KEY, {
...
});
},
};
I can access my stripe secret key using env.STRIPE_SECRET_KEY but is it possible to do something similar directly in NextJS server components prefixed with ' "const runtime="edge" ' ?
In my API route I have to call the Cloudflare Worker right now to access environmental variable which adds extra server calls:
import { NextResponse } from "next/server";
import { CheckoutApiRequest } from "@/types/api_types";
export const runtime = "edge";
export async function POST(req: Request) {
try {
const { amount } = (await req.json()) as CheckoutApiRequest;
const res = await fetch("https://******", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ amount }),
});
if (!res.ok) {
const error = await res.json();
return NextResponse.json({ error }, { status: res.status });
}
const { sessionId } = await res.json();
return NextResponse.json({ sessionId: sessionId });
} catch (error) {
return NextResponse.json({
error
});
}
}
本文标签:
版权声明:本文标题:next.js - How to access environmental variables on NextJS server using @cloudflarenext-on-pages and "edge" run 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744281085a2598654.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论