|
1 | | -## Environment validation |
| 1 | +# Environment configuration |
2 | 2 |
|
3 | | -- Server env is validated with Zod in `src/config/validation.ts` and parsed in `src/env.ts` during startup. Invalid or missing values throw immediately. |
4 | | -- When `CI=true`, required keys: `VITE_SENTRY_ORG`, `VITE_SENTRY_PROJECT`, `SENTRY_AUTH_TOKEN`. |
5 | | -- When `CI` is false/omitted, Sentry env is optional and the build runs without the Sentry wrapper. |
6 | | -- `CI` is coerced to boolean, defaults to `false`. |
7 | | -- `vite.config.ts` conditionally wraps with Sentry only when all Sentry env vars are present. |
| 3 | +This project separates build-time variables (used by Vite and CI) from runtime variables (provided by Cloudflare Workers). |
8 | 4 |
|
9 | | -### Adding a new env var |
| 5 | +## Build-time env |
10 | 6 |
|
11 | | -1. Add the key to `serverEnvSchema` in `src/config/validation.ts` (mark it required/optional as needed and document any CI-only requirements). |
12 | | -2. Access it via `env.MY_KEY` from `src/env.ts`. |
13 | | -3. Restart the dev server/build; invalid values will surface as startup errors. |
| 7 | +- Purpose: tooling only (e.g., Sentry sourcemap upload in CI). |
| 8 | +- Source: `.env.local` (optional) or CI env. |
| 9 | +- Keys: `CI`, `VITE_SENTRY_ORG`, `VITE_SENTRY_PROJECT`, `SENTRY_AUTH_TOKEN`. |
| 10 | +- Validation: `parseBuildEnv` in `src/config/validation.ts` (used by `vite.config.ts`). |
| 11 | + |
| 12 | +## Runtime env (Workers) |
| 13 | + |
| 14 | +- Purpose: secrets and bindings used by server code at runtime. |
| 15 | +- Source: Cloudflare bindings; local via `.dev.vars` when running `wrangler dev`. |
| 16 | +- Keys: `AUTH_SECRET`, `AWS_REGION`, `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `SES_FROM_EMAIL`, optional `SENTRY_DSN`. |
| 17 | +- Access: `getRuntimeEnv()` from `src/config/runtimeEnv.ts` (Workers runtime only; not for client imports). |
| 18 | +- Validation: Zod schema in `runtimeEnv.ts`; errors list missing keys but never values. |
| 19 | + |
| 20 | +## Local setup |
| 21 | + |
| 22 | +- Build-time: optionally copy `.env.example` to `.env.local` and fill Sentry fields if you need CI-style sourcemap uploads locally. |
| 23 | +- Runtime: copy `.dev.vars.example` to `.dev.vars` and fill values; `wrangler dev` will load them. |
| 24 | +- Run: `pnpm dev` (uses `wrangler dev` on port 3000). `pnpm dev:vite` is available as a Vite-only fallback. |
| 25 | + |
| 26 | +## Staging/production |
| 27 | + |
| 28 | +- Secrets (`AUTH_SECRET`, AWS keys) should be set with `wrangler secret put`. |
| 29 | +- Non-secrets (`AWS_REGION`, `SES_FROM_EMAIL`, optional `SENTRY_DSN`) can be set via `vars` in `wrangler.toml` or `wrangler deploy --var KEY=value`. |
| 30 | +- Build-time Sentry vars for CI are configured in the CI environment (match `.env.example` keys). |
| 31 | +- Runtime env must be read only through `getRuntimeEnv()` inside server code. |
| 32 | + |
| 33 | +## Validation |
| 34 | + |
| 35 | +- Build-time keys are validated by `buildEnvSchema` in `src/config/validation.ts` and parsed via `parseBuildEnv` in `vite.config.ts` / `src/env.ts`. When `CI=true`, Sentry keys are required; otherwise they stay optional. |
| 36 | +- Runtime keys are validated by `getRuntimeEnv()` in `src/config/runtimeEnv.ts`; error messages list missing keys without revealing values. |
| 37 | + |
| 38 | +## Sentry defaults |
| 39 | + |
| 40 | +- Server (Workers): DSN from runtime `SENTRY_DSN` via `getRuntimeEnv()`. Sample rates: traces 0.1 / profiles 0.1 in production, 1.0 in dev/staging. Environment comes from `WORKERS_ENVIRONMENT`/`NODE_ENV`. |
| 41 | +- Client: DSN from `VITE_SENTRY_DSN` (optional). Sample rates mirror server; replays are off by default (`replaysSessionSampleRate=0`, `replaysOnErrorSampleRate=1.0`). Environment uses `import.meta.env.MODE`. |
| 42 | + |
| 43 | +## Testing |
| 44 | + |
| 45 | +- Playwright uses `pnpm dev` (wrangler) so e2e hits the Workers runtime on port 3000. `.dev.vars` must be present for runtime bindings. |
| 46 | + |
| 47 | +## Deployment |
| 48 | + |
| 49 | +- `workers_dev` is enabled for the first deploy to `*.workers.dev`. Route settings remain in `wrangler.jsonc` for attaching the custom domain afterward. |
0 commit comments