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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add how to use environment variables with worker to README.md #1

Merged
merged 1 commit into from
Jun 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ tsconfig.tsbuildinfo

# Cloudflare
wrangler.dev.toml
.dev.vars
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,56 @@ export let loader: LoaderFunction = ({ context }) => {
}
```

## Environment Variables (Secrets) 🔐

### Adding Worker Environment Variables

1. `cd packages/worker`
2. Inside the `worker` package directory, run `wrangler secret put <VARIABLE_NAME>` to add a secret variable to the worker project.
3. You will see the added environment variable shown under the `Settings` section in the Cloudflare Worker project dashboard.

Example:

```shell
wrangler secret put SESSION_SECRET
# There will be a prompt for you to type or paste the value into the terminal.
# After hitting `Enter` the key-value pair will be uploaded to the worker.
```

Now, we can access SESSION_SECRET via `context.env.SESSION_SECRET` in loader and action functions in the production environment.

### Setting Worker Local Environment Variables

In this template, you can set local environment variables in one of these two ways.

#### Setting variables in wrangler.dev.toml

This is the default way to consume local environment variables by this template.

If the dev script uses `wrangler dev` with `--config wrangler.dev.toml` option, wrangler will consume the `wrangler.dev.toml` which is ignored by Git.

So, we can put any secret we do not want to commit into this config file.

1. Create a `wrangler.dev.toml` inside `packages/wrangler` directory.

2. Then set variables based on the example in the `packages/wrangler/wrangler.example.toml`.

#### Using .dev.vars

Instead of `.env`, wrangler consumes `.dev.vars`.

Just create a file called `.dev.vars` in `packages/worker` package directory with `NAME = VALUE` pairs just like a typical `.env` file.

Don't commit the `.dev.vars` into the git repo.

Example:

```
SESSION_SECRET="should-be-secure-in-prod"
```

With one of these two methods, we can access `SESSION_SECRET` via `context.env.SESSION_SECRET` in loader and action functions in development environment.

## Turbo Setup ✨

This repository is used in the `npx create-turbo@latest` command and selected when choosing which package manager you wish to use with your monorepo (npm).
Expand Down