-
Notifications
You must be signed in to change notification settings - Fork 541
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
Support cloudflare environment variables #272
Comments
I'm not sure if it will change anything for this issue, but the new |
Since D1 hit public alpha, I've been trying to get something happening based on @marshallswain's discussion. This WIP ESM preset is working, but as discussed, needs a way to access the environment to use service bindings, D1 etc: timhanlon@fabba1f I'm keen to help push this forward, but unsure of an approach that would work for Nitro, given Cloudflare is one of many supported providers. |
This looks really promising with the ESM worker. I wonder if this can also be ported over to the Cloudflare Pages version too? These types of things are going to be a game changer to get for example vuefire-nuxt working (eventually and maybe 🙈). I also ran into the situation with the I think a workaround with namespaced I quickly tested it locally with a patched version of |
I like the idea of binding to But we need to find also a way to make it work for runtimeConfig |
With CF worker format, we have another limitation that ENV is only accessible within a request. For this we might introduce |
I'm hitting this issue now and it's really inconvenient ... let apiKey = useRuntimeConfig().openaiApiKey as string
if (apiKey.length === 0) {
apiKey = event.context.cloudflare.env.NUXT_OPENAI_API_KEY
} For the module syntax and pages, we could unblock this quite easily by doing something like this in the Nitro runtime cloudflare presets :
setRuntimeConfig would proxy the variables like This doesn't work with service workers syntax, but we should deprecate it anyways, pretty sure there's no need to maintain both syntaxes in Nitro Then forwarding the request to Nitro where user code would work as expected with useRuntimeConfig. Another things that is cloudflare specific that we could do is copy the user That way everything would work both in dev and while testing the build locally. I can open a PR with this change @pi0 |
@Hebilicious fix is needed (please merge his pr) |
When will this be fixed, any ideas? @pi0 |
Hi, are there any updates concerning this issue? |
@DidoMarchet you can use |
Edit : After running further tests, it turns out that NITRO_OPENAI_API_KEY=abc #useRuntimeConfig(event).openaiApiKey will work with nuxt and nitro standalone
NUXT_OPENAI_API_KEY=abc #useRuntimeConfig(event).openaiApiKey will work with nuxt
OPENAI_API_KEY=abc #useRuntimeConfig(event).openaiApiKey will not work @lukamo1996 @DidoMarchet could you confirm that |
so.. if I am using
what do I do for cases that I do not have an event, or how does this work out for the client? |
@beaudryj this will work, see working example here : https://github.com/Hebilicious/nuxt-authjs-google |
I had that, but it does not seem to work. I want to note that i do my preset build in my CI and ship my dist directory to cloudflare. If i was building on cloudflare do the envvars get set on their wokers at a build time? Or does nitro set the values there on page request |
You need to manually set the variables, you can use wrangler or the dashboard, see instructions in PR here |
// In development
export default defineEventHandler((event) => {
useRuntimeConfig(event).helloThere //general
useRuntimeConfig(event).secret //undefined
// Module syntax (cloudflare_module, cloudflare_pages)
event.context.cloudflare.env.NITRO_HELLO_THERE //general
event.context.cloudflare.env.SECRET //secret
// Service worker syntax (cloudflare)
NITRO_HELLO_THERE //general
SECRET //secret
}); this block you have here though would not work for pages without events? what do I do in those scenarios or is it more my nuxt.config.ts should be setup as
|
You don't need to do that, |
Hard to tell more without a reproduction, but I think your variables need to be prefixed by https://nuxt.com/docs/guide/going-further/runtime-config#environment-variables |
in my server route sample: server/routes/signin.js import { createNonce } from '~/utils/auth';
import buildUrl from 'build-url';
import { defineEventHandler, setCookie } from 'h3';
export default defineEventHandler(async (event) => {
const runtimeConfig = useRuntimeConfig(event);
const nonce = createNonce();
setCookie(event, 'nonce', nonce, { secure: true });
const url = buildUrl(`https://${runtimeConfig.auth0.domain}`, {
path: 'authorize',
queryParams: {
client_id: runtimeConfig.auth0.clientID,
response_type: '',
redirect_uri: runtimeConfig.auth0.redirectUrl,
scope: '',
audience: runtimeConfig.auth0.audience,
nonce: nonce,
},
});
return sendRedirect(event, url, 302);
}); app.vue sample <template>
<NuxtLayout>
<NuxtLoadingIndicator />
<NuxtPage />
</NuxtLayout>
</template>
<script setup lang="ts">
const route = useRoute();
const runtimeConfig = useRuntimeConfig();
const domain = runtimeConfig.public.domain;
useHead({
titleTemplate: '%s - Leagued',
viewport: 'width=device-width, initial-scale=1',
charset: 'utf-8',
htmlAttrs: {
lang: 'en',
},
link: [
{
rel: 'apple-touch-icon',
sizes: '180x180',
href: '/favicon/apple-touch-icon.png',
},
{
rel: 'icon',
type: 'image/png',
sizes: '32x32',
href: '/favicon/favicon-32x32.png',
},
{
rel: 'manifest',
type: 'image/png',
sizes: '16x16',
href: '/favicon/favicon-16x16.png',
},
{ rel: 'manifest', href: '/favicon/site.webmanifest' },
{
rel: 'mask-icon',
color: '#5bbad5',
href: '/favicon/safari-pinned-tab.svg',
},
{ rel: 'shortcut icon', href: '/favicon/favicon.ico' },
{ name: 'msapplication-TileColor', content: '#ffc40d' },
{ name: 'msapplication-config', href: '/favicon/browserconfig.xml' },
{ name: 'theme-color', content: '#ffffff' },
{
rel: 'canonical',
href: () => `https://example.com${route.path}`,
},
],
});
</script>
|
@beaudryj If you are confident that there's a bug, I recommend that you open an issue in the Nuxt repository with a minimal reproduction attached. |
@Hebilicious thank you for the back and fourth on this. I found what was tripping us up - https://v2.nuxt.com/docs/configuration-glossary/configuration-env/#processenv-- further digging into the docs I noticed this -
So I did a deployment using |
Reference docs: https://developers.cloudflare.com/workers/platform/environment-variables/
Related: nuxt/nuxt#14011
Normally, we allow extending runtime config using Node.js environment variables (
process.env.FOO
) while cloudflare exposes env as global constant variables (accessible asFOO
orglobalThis.FOO
).We can add support for globalThis but since without prefix can be dangerous, I propose to support
ENV_*
variables for globalThis support. (SettingENV_FOO
in Cloudflare is same asFOO=x node
when using Node.js) but the downside is makes usage harder and needing more docs. We might only enable prefixless support for Cloudflare.The text was updated successfully, but these errors were encountered: