remix.runcloudflare-pages

Environment variable access in Remix run framwork with cloudflare pages on server files


I am struggling to get the env. variable set up in my remix app. Here is the code from session.server.ts file as

import { createCookieSessionStorage } from "@remix-run/cloudflare";

export const { getSession, commitSession, destroySession } = createCookieSessionStorage({
  cookie: {
    name: "remix_session",
    secrets: [`${process.env.SESSION_SECRET}`],
    sameSite: "lax",
    path: "/",
    secure: process.env.NODE_ENV === "production",
    httpOnly: true,
    maxAge: 60 * 60 * 24 * 30, // 30 days
  },
});

The error is "process os not defined" when I deploy on cloudflare pages. Can somebody please help me resolve this issue? Thank you

Checked the documents and seems functions can't access the process env but unsure how to access these vars.


Solution

  • Cloudflare doesn't support process. You get the current environment via context.

    export async function loader({
      context,
    }: LoaderFunctionArgs) {
      const { MY_KV } = context.cloudflare.env;
      const value = await MY_KV.get("my-key");
      return json({ value });
    }
    

    https://remix.run/docs/en/main/guides/vite#cloudflare

    As for sessions, there's no global context (you only get context per request via loader and action).

    There is a big thread here that discusses different techniques to manage context and sessions.

    https://github.com/remix-run/remix/issues/6868