You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 86cfa9e
Browse filesBrowse the repository at this point in the historyBrowse files
Install Vercel CLI if not already done. Linking it will create a .vercel directory in the project with local configuration details. This directory is gitignored.
6
+
7
+
```bash
8
+
npm install -g vercel
9
+
vercel link
10
+
```
11
+
12
+
Pull env vars from Vercel:
13
+
14
+
```bash
15
+
vercel env pull .env
16
+
```
17
+
18
+
See [Deployment](./DEPLOYMENT_SETUP.md) for information on adding environment variables to the GitHub repository for use in workflow Actions.
19
+
20
+
## Loading
21
+
22
+
Astro uses Vite to load environment variables, which in turn uses `dotenv` to load them from several `.env` files located in the project's root directory. The specific files loaded, and their priority, depend on the current mode (development or production). All `.env*` files are gitignored.
23
+
24
+
- `.env`: Loaded in all cases, providing general environment variables.
25
+
- `.env.local`: Loaded in all cases, intended for local development settings.
26
+
- `.env.[mode]`: Loaded only in the specified mode (`.env.development` when running the Astro dev server, and `.env.production` when running `astro build`).
27
+
- `.env.[mode].local`: Loaded only in the specified mode, providing local overrides for a specific mode.
28
+
29
+
## Availability
30
+
31
+
1. Auto-loaded environment variables are not available in **config files**. The files are excluded from the ESLint linting rule forbidding `process.env`. They can use Vite's `loadEnv()` method to load `.env*` files.
32
+
2. All environment variables are available in **server-side code**.
33
+
3. Only environment variables prefixed with `PUBLIC_` are available in **client-side code**. Vite extracts these variables from `.env*` files during loading and bundles them into the client script.
34
+
35
+
## Typing
36
+
37
+
There are two ways in an Astro project to type environmental variables in Astro:
38
+
39
+
1. Add them to `src/lib/config/environmentalVariableValidation.ts`. This provides validation for the variables. Astro will generate a `.astro/env.d.ts` file with the types exported as `const` from `astro sync`, ran automatically on `astro dev` and `astro build`. They will be available as tokens from `astro:env/server` and `astro:env/client` respectively.
40
+
2. Add them to `src/env.d.ts`. This will provide TypeScript typing for intellisense but no validation. Environmental variables added this way will not be made available on the `astro:env` imports.
41
+
42
+
## Use in Code
43
+
44
+
Import environment variables defined in `environmentalVariableValidation` directly.
45
+
46
+
All variables:
47
+
48
+
```typescript
49
+
import { ENV_VAR } from 'astro:env/server'
50
+
```
51
+
52
+
Client side variables:
53
+
54
+
```typescript
55
+
import { PUBLIC_ENV_VAR } from 'astro:env/client'
56
+
```
57
+
58
+
The `process.env` and `import.meta.env` methods of accessing environment variables will generate linting errors if used outside of the root-level configuration files where they are necessary.
59
+
60
+
## Determining Site URL
61
+
62
+
The site URL can vary based on whether code is being ran from the Astro dev server, being ran in production (including being ran with `astro serve`), being ran from Vitest in unit files,
63
+
64
+
- Use the `getSiteUrl()` method from `src/components/scripts/utils/siteUrlClient.ts` to determine the site URL in **client code**.
65
+
- Use the `getSiteUrl()` method from `src/lib/config/siteUrlServer.ts` to determine the site URL in **server code**.
66
+
67
+
## Determining Mode
68
+
69
+
Use well tested methods to determine what mode (dev, prod, ci, vitest, playwright) code is operating in:
70
+
71
+
- Use methods in `src/components/scripts/utils/environmentClient.ts` to determine mode in **client code**.
72
+
- Use methods in `src/lib/config/environmentServer.ts` to determine mode in **server code**.
0 commit comments