Remix, Cloudflare Pages and Context/Env variables
I am working on a project with Remix and Cloudflare Pages. So far everything works fine but I can't figure out how I access context or environment variables in loader functions on Cloudflare Functions (server side). For example I want to access KV in a Cloudflare function that is called from a loader function in Remix. Everything I have tried (context.env.KV_NAME or env.KV_NAME) doesn't work in my server.ts file. Does anyone know how I access the context or env in a Cloudflare Function in a Remix project?
3 Replies
From the docs it should just be
context.env.KV_Name
. Are you getting an error that the binding isn’t found or are no values being returned?That's correct and I have read all the documents I could but no go. The problem seems to be that context.env.KVNAME is only available in a route context but not in a function context.
For example if I do in "login.tsx":
export async function loader({
request, context, params
}: LoaderFunctionArgs) {
// Add from session server
// In this example the Cookie is created separately.
const sessionCookie = createCookie("__session", {
secrets: ["r3m1xr0ck5"],
sameSite: true,
});
const { getSession, commitSession, destroySession } =
createWorkersKVSessionStorage({
// The KV Namespace where you want to store sessions
// @ts-ignore
kv: context.env.KV,
cookie: sessionCookie,
});
// END added from server
it works but if I take the server portion and put it in a server.ts file and call it via a function call it doesn't compile because the in server.ts "context.env.KV_NAME" ist not defined.
I think this Github issue here is exactly the problem:
GitHub
Environment variables on Cloudflare Pages · Issue #6868 · remix-run...
What version of Remix are you using? 1.18.1 Are all your remix dependencies & dev-dependencies using the same version? Yes Steps to Reproduce I try accessing env vars following instructions fro...