-
Notifications
You must be signed in to change notification settings - Fork 127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Environment variables and bindings on env
#1
Comments
Why not extend NextRequest? export default {
async fetch(request, env, context) {
const { pathname } = new URL(request.url);
const routes = routesMatcher({ request }, __CONFIG__.routes);
// link env to request object
request.env = env;
for (const route of routes) {
if ("middlewarePath" in route && route.middlewarePath in __MIDDLEWARE__) {
return await __MIDDLEWARE__[route.middlewarePath].entrypoint.default(
request,
context
);
}
}
for (const { matchers, entrypoint } of Object.values(__FUNCTIONS__)) {
let found = false;
for (const matcher of matchers) {
if (matcher.regexp) {
if (pathname.match(new RegExp(matcher?.regexp))) {
found = true;
break;
}
}
}
if (found) {
return entrypoint.default(request, context);
}
}
return env.ASSETS.fetch(request);
},
} as ExportedHandler<{ ASSETS: Fetcher }>; and we have import { CfNextRequest } from '@cloudflare/next-on-pages'
export const config = {
runtime: 'experimental-edge'
}
export default async function(req: CfNextRequest) {
const value = req.env.MY_ENV_VAR
} |
Any plans to add env to this package soon or are pull request along the lines of @bluebeel's suggestion welcome? This is a pretty critical feature in my opinion. |
For the life of me I cannot figure out how to get |
What is the current way in which you access ENV variables in a NextJS experimental-edge app on Cloudflare? I can't seem to figure this out... |
@mrkstwrt I see you made a fork with that change. Did it work for you? It doesn't seem to do anything for me... |
@GregBrimble this is super frustrating. Just wasted a few hours trying to get this to work as it makes Functions basically useless to me. Is there any workaround at the moment? |
Hi @Hades32, const { EnvironmentPlugin } = require('webpack');
/** @type {import('next').NextConfig} */
const nextConfig = {
// ...
webpack(config) {
config.plugins.push(new EnvironmentPlugin(['API_KEY', 'OTHER_SECRET']));
return config;
},
}; |
Isn't that the same as using the env config and will expose all the provided secrets in the frontend bundle too? |
@mrkstwrt Thanks for the hint! To be honest, I didn't think of that 😅 But I just checked, as long as you don't put |
@luca-rath thanks, but does that really work for binding, too? That's the real issue I'm facing |
I am very much looking forward to this. |
Wow, have been waiting for this since Next 13 release. Thanks @luca-rath and you all. 🚀 |
For me still doesn't work for binding... 😢 |
Here's the changes needed. Hopefully this person makes a PR, this is very valuable. main...YuheiNakasaka:next-on-pages:feature/add-env-to-functions |
Tried to run this in the build process (using the Github Action
😞 |
Yeah apparently this doesn't work -spoke with the author on Twitter. Excited about |
Super frustrating that this isn't included. I would expect all my defined environment variables to be available in |
@GregBrimble please tell me key value is implemented or not and geo location or req.body.cf implemented or not |
Hey folks! 👋 We've just merged a PR (#58) which will add the ability to access environment variables and bindings from within your SSR pages/API handlers. This will be available with the next release ( Apologies for it taking so long! We know it was a point of frustration for a lot of people. It doesn't quite make up for that, but here's part of the reason why: 1. Putting the Cloudflare Workers environment on
|
This is fantastic to hear, thanks @GregBrimble. 🎉 One of the other largest blockers for people in my experience is the output size, as documented at #47. It's regularly reported in the Discord too, and also contributes to issues like #54. Seeing some movement on these in the near future would be awesome. |
Thanks @GregBrimble. |
Just saw that v0.3.0 was released. I've just re-built and deployed my project that was using |
I have tested and can confirm env vars are working as expected. |
Hey guys seems bindings work fine after you deploy. Anyone know how to get them to work locally? |
They need to be in your environment/session firstly, then you can call |
import { KVNamespace } from '@cloudflare/workers-types';
import { NextResponse } from 'next/server';
export async function GET(request: Request) {
const { DOMAINS } = process.env as any as { DOMAINS: KVNamespace };
const data = await DOMAINS.get('test'); // throw error when build
return NextResponse.json({ data });
} |
import { KVNamespace } from '@cloudflare/workers-types';
import { NextResponse } from 'next/server';
export async function GET(request: Request) {
const { DOMAINS } = process.env as any as { DOMAINS: KVNamespace };
await DOMAINS?.put('test', 'value');
const data = await DOMAINS?.get('test');
return NextResponse.json({ data });
} codes above can be built. but got: run cmd: |
@willin I think that I just created this app to test it out: kv-app-dir-13.4.4 I run it locally with the So I am not sure what issues you're encountering, could you provide more details? if you could provide a minimal reproduction please do so as that would really help 🙂 |
Next.js has it's own API design for
getServerSideProps()
and the Edge API Routes and Middleware take only theNextRequest
object as its single parameter. This means that passing Workers'env
object which contains environment variables and bindings isn't immediately obvious.One idea is to 'pollute' the global space and access things by request. This has the advantage of being similar to how it works today, but might be too far into the uncanny valley.
For example:
Another might be to use the
@cloudflare/next-on-pages
package as a runtime library which is different enough to be intentional, but still fundamentally works the same way:What do y'all think? Any preferences? Any other ideas?
The text was updated successfully, but these errors were encountered: