Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃殌 Feature Request: Support for ENV vars in Pages Functions(beta) #1178

Closed
s1mple opened this issue Jun 3, 2022 · 4 comments
Closed
Labels
enhancement New feature or request pages Relating to Pages

Comments

@s1mple
Copy link

s1mple commented Jun 3, 2022

Describe the solution

It would be really nice to be able to configure environment variables inside of pages functions using a wrangler.toml configuration file, similar to the workflow used for workers.

Here an idea of what the pages functions environment variable documentation could be after this feature is rolled out:

Adding environment variables locally

You can set environment variables for your project by adding env var bindings to a wrangler.toml file in the root of your pages functions project, and preview different env vars locally for development by adding --binding ENV_NAME="ENV_VALUE" to your wrangler commands.

For global values, use a [vars] section in your wrangler.toml file:

[vars]
COLOR = "ORANGE"

For separate bindings for each environment, with cascading overwrites of global [vars], use env-specific bindings:

[env.preview.vars]
COLOR = "RED"

[env.production.vars]
COLOR = "BLUE"

For previewing a different value during development, add the binding to your Wrangler commands using npx wrangler pages dev dist --binding ENV_NAME="ENV_VALUE". All of these methods allow you to then access the environment value in your component by using env.ENV_NAME.

For example, you can run npx wrangler pages dev dist --binding COLOR="BLUE" and then:

export async function onRequest({ env }) {
  return new Response(env.COLOR);
}

Note: env vars set inside of the Cloudflare Pages project control panel will always take precedence over those set in a project wrangler.toml file when the project has been deployed.

@s1mple s1mple added the enhancement New feature or request label Jun 3, 2022
@petebacondarwin petebacondarwin added the pages Relating to Pages label Jun 4, 2022
@kristianfreeman
Copy link
Contributor

This would be great!

@GregBrimble
Copy link
Member

You may be interested in #898 which is actively in progress and will hopefully be released soon.

We're still very much in the discussions stage about how we want to do config for Pages projects. It might be a wranger.toml, or something else entirely.

@nprogers
Copy link
Contributor

nprogers commented Sep 7, 2022

We recently added support for a .dev.vars file. Documentation is in progress, but it works like any .env file. This doesn't address the general .toml request, but may help with specific vars request.

#1274

@s1mple s1mple changed the title 馃殌 Feature Request: Support for wrangler.toml configuration of ENV vars in Pages Functions(beta) 馃殌 Feature Request: Support for ENV vars in Pages Functions(beta) Sep 14, 2022
@s1mple
Copy link
Author

s1mple commented Sep 14, 2022

Thank you, this is very helpful!

Please feel free to close this issue since this effectively addresses the development process gap I was experiencing when I first opened this ticket.

I will leave this open for now, just in case there are additional features around pages ENV vars you're still working on, but after looking at the new docs at https://developers.cloudflare.com/pages/platform/functions/#adding-environment-variables-locally I like where this is going :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request pages Relating to Pages
Projects
None yet
Development

No branches or pull requests

6 participants