How do you get the env in a non request? Example in my case: ```tsx import { getRequestContext } fro
How do you get the env in a non request?
Example in my case:
Example in my case:
import { getRequestContext } from "@cloudflare/next-on-pages";
import { drizzle } from "drizzle-orm/d1";
export const runtime = 'edge';
const { env } = getRequestContext();
const db = drizzle(env.DB);
export { db };How do you get the env in a non request?all your source code is run inside a Cloudflare worker request handler, so you're basically always handling a request (i.e. you should always be able to run
getRequestContext)drizzle from working getOptionalRequestContext in that case and checking if you do or not get the request contextI managed to get next-on-pages -> drizzle -> d1 working with next auth for creating my users, so it was working fine for me? Weirdok, maybe sometimes/in some cases it works.... I really don't know to be honest
But when I was using process.env it worked on production but not locally and now that I am using env and getRequestContext it works locally but not in production lol
.env file? (those are only available/populated by next dev but not by wrangler pages dev nor in the deployed application)My .env file atm has nothing in it related to bindingsweird
env.d.ts file is only for providing the correct types, it doesn't effect what it made availabe or not (also I thought the issue could have been string bindings, not actual bindings, the latter doesn't really have anything to do with .env files)process.env.DB but if you do getRequestContext().env.DB you can't access your binding? env.d.ts doesn't matter too much, in your code you can do: (process.env as any).DB and see if it contains the binding getOptionalRequestContext instead of process.env?
getRequestContext be used for production bindings (deployed on Cloudflare pages) or only local development?getRequestContextgetRequestContext is only for dev ?_middleware.ts file inside a functions folder. I created it and for now I just tried to return the sample response and also some console.logs to see if this was working but I can't see anything working.

@cloudflare/next-on-pages, the functions directory won't work as we build our own worker for your project. You could try using Next.js API route handlers or Next.js middleware insteadsetupDevPlatform: https://github.com/cloudflare/next-on-pages/tree/main/internal-packages/next-dev#next-on-pages-next-devsetupDevBindings it is exactly the same besides the function's name and the fact that you define your bindings in a wrangler.toml instead of defining them in the function's call)setupDevPlatform documentation?getPlatformProxy (and we do defer to that page in our docs)env.d.ts
declare global {
namespace NodeJS {
interface ProcessEnv {
DB: D1Database;
DOCUMENTS_BUCKET: R2Bucket;
}
}
}
export {}env.d.ts
interface CloudflareEnv {
DB: D1Database;
DOCUMENTS_BUCKET: R2Bucket;
}import { drizzle } from "drizzle-orm/d1";
export const runtime = "edge";
const db = drizzle((process.env as any).DB);
export { db };[[durable_objects.bindings]]
name = "MY_DO"
script_name = "do-worker"
class_name = "DurableObjectClass"