-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
netlify edge functions SSR environment variable lookup #5105
Comments
This may be related to #4416 (comment) |
I have created a PR #5103 to address the access to runtime issue (for cloudflare). Is there a way to do a similar thing with Netlify too? Than we can generalize this a little more, since that should be a task for the adapter to handle. |
It probably can't be generic as each runtime does this a bit differently. @bluwy is there any control we have over when |
Sorry I did not mean to generalize what the adapter is doing, I meant the access to the specific runtime additions. Like ˋAstro.getRuntime()ˋ and each adapter can add to that runtime via a ˋSymbolˋ or ˋAstro.setRuntime()ˋ or even context, since it is also request related? I will adjust the draft PR. |
Yeah it's currently being transformed to
We could expose an adapter option to customize this. I think Cloudflare has a special case where functions env vars can only be accessed throught the Either ways I think it would still be a win. |
@AirBorne04 we would need an RFC for a generic solution. Your current cloudflare-only PR I think is fine without one. That's a bit different from the environment variable problem being discussed here though. |
@bluwy oh, I didn't realize it was us. In that case I concur that an option that adapters can implement is the way to go here. |
Maybe |
Rough usage example: 'astro:config:setup': ({ updateConfig, mode }) => {
if(mode === 'build') {
updateConfig({
build: {
envKey: (key: string) => `Deno.env.get("${key}")`
}
})
}
} |
When deploying pages you need to access it via the function parameters. Only in the workers you can read them globally. Pages and workers are working very similar but unfortunately not exactly the same. I ran a quick test in pages and it outputs:
so definitely functions parameter for the pages platform, for workers it is either function parameter or global. |
@matthewp just wondered if it makes sense to combine the |
@AirBorne04 I was talking about this with @bluwy, would it be possible, make sense for the adapter to assign the const handler = (request, env) => {
globalThis.WORKER_ENV = env;
// ...
} And then in the hook: 'astro:config:setup': ({ updateConfig, mode }) => {
if(mode === 'build') {
updateConfig({
build: {
envKey: (key: string) => `globalThis.WORKER_ENV["${key}"]`
}
})
}
} I know this is weird but I can't think of another way to wire up Am I right that the |
@matthewp Yes it is possible to assign them like this to a global. I am not sure how the
Actually i am not a fan of implicit merging of env variables into
I checked this and the env vars are static per deployment |
@AirBorne04 no, if you notice, the Thinking about it, it might need to be a little more complex than that. If someone tries to use it at the top level (outside of a request) then the Very well noted about differentiating between runtime vs. build-time env vars. I agree it's confusing / not clear. But that's the existing behavior so I think we have to stick with that until a better way is figured out. |
Ah ok, if that is pushing them into the For the guard i will also check what can be done there. |
Thinking about how we should implement this, it looks like currently we support:
I don't see a usecase yet for the two ❌ and it seems to have worked well for us so far. I think we can go with @AirBorne04 have you started working on this? I'm also planning to test some things out soon. |
Thanks @AirBorne04. Looks like you got the implementation pretty close. We'd need to replace using
I'll start working on a PR based on your branch too and credit you as a co-author. Will also implement a similar fix for Deno environments. |
Turns out supporting It also looks like we can't support runtime env vars when someone does Currently working on this branch which simplifies env var replacement & perf, but it still has some failing test. Once that's resolved we can start implementing the cloudflare and deno env support. |
@bluwy I did a little more digging and see now why there is an issue. I have compiled the
So for cloudflare there would be the path of adding the env vars to the
This would cause all the env vars to be included in the build and bloat the file size a little (3Kb in my case) but since this is server side that should be fine. This would also eliminate the need for |
@AirBorne04 I'm currently close to a PR too with https://github.com/withastro/astro/tree/env-key and I actually found |
What version of
astro
are you using?1.5.0
Are you using an SSR adapter? If so, which one?
@astrojs/netlify/edge-functions
What package manager are you using?
npm
What operating system are you using?
Linux
Describe the Bug
Using
import.meta.env
with ssr and@astrojs/netlify/edge-functions
doesn't work.For example if you have some code that references
import.meta.env.FOO
.The generated code has calls to
process.env.FOO
but to look up environment values in the netlify edge runtime it needs to callDeno.env.get("FOO")
.Link to Minimal Reproducible Example
https://stackblitz.com/edit/github-gubo61?file=src/pages/index.astro
Participation
The text was updated successfully, but these errors were encountered: