Skip to content

Commit b98dd6b

Browse files
Fix error loading .env.local into ALL builds, regardless of environment (#4621)
# Description and Motivation <!--- bulleted, high level items. use keywords (eg "closes #144" or "fixes #4323") --> This pull request resolves an issue where Vite's default behavior of loading .env.local caused local development environment variables to inadvertently affect other build processes. The solution involves renaming the local environment file to .env.localhost and systematically updating all related scripts, code, and documentation. This change ensures a clear separation of local development configurations from other environments, leading to more robust and predictable builds. Highlights - Environment Configuration Refinement: Renamed frontend/.env.local to frontend/.env.localhost to prevent unintended loading by Vite in non-local builds, ensuring environment variable isolation. - Script and Code Alignment: Updated npm scripts in package.json and internal code references in DataFetcher.ts and Environment.ts to reflect the new localhost naming convention. - Documentation Update: Modified README.md to guide developers on using the new npm run localhost command and the .env.localhost file. - Git Ignore Enhancement: Added frontend/.env.local to .gitignore to prevent accidental commits of the old, problematic file name. ## Has this been tested? How? need to merge and deploy to full test; success will be demonstrated when network calls from the PROD website no longer route to `dev.healthequitytracker.org` as they are currently. This was because the base API en variable was set in the now deprecated `.env.local` file, the assumption being that file loaded ONLY in local development. However, Vite loads that file whenever it's present, and merges with other loaded files, so this file was being loaded in deploy preview and dev environments, but ALSO in prod making the data_server URL incorrect ## Screenshots (if appropriate) ERROR <img width="1568" height="516" alt="image" src="https://github.com/user-attachments/assets/46665166-9e1e-4fde-a4ad-5d519ee8dad7" /> ## Types of changes (leave all that apply) - Bug fix - Refactor / chore ## New frontend preview link is below in the Netlify comment 😎
1 parent f59119e commit b98dd6b

6 files changed

Lines changed: 27 additions & 41 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ config/.terraform.lock.hcl
2525

2626
# Environment Files
2727
.env.secrets
28+
/frontend/.env.local
2829
/frontend_server/.env.local
2930
/frontend_server/.env.development
3031
/scripts/.env.secrets

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ Note: If you are using VSCode or one of its forks, ensure you install the recomm
119119
1. While still in the `health-equity-tracker/frontend/` folder, run
120120

121121
```bash
122-
npm run local
122+
npm run localhost
123123
```
124124

125125
2. In your browser, visit <http://localhost:3000>
@@ -267,7 +267,7 @@ The frontend uses multiple environments to assist with development, testing, and
267267
268268
| Environment | .env File | Frontend Deployment | Backend GCP Project | Description |
269269
|-------------|-----------|---------------------|---------------------|-------------|
270-
| Local Development | `.env.local` (create from template) | Local machine's <http://localhost:3000> | het-infra-test | For developer workstations. |
270+
| Local Development | `.env.localhost` | Local machine's <http://localhost:3000> | het-infra-test | For developer workstations. |
271271
| PR Preview | `.env.deploy_preview` | Netlify PR Preview; URL in GitHub PR comment | het-infra-test | Temporary deployments for pull request reviews. |
272272
| Development | `.env.dev` | dev.healthequitytracker.org | het-infra-test | Stable environment for testing features before production. |
273273
| Production | `.env.production` | healthequitytracker.org | het-infra-prod | Live environment for end users. |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Configuration for local development environment with connection to het-infra-test backend
55

66
# Deployment context identifier
7-
VITE_DEPLOY_CONTEXT=local
7+
VITE_DEPLOY_CONTEXT=localhost
88

99
# API configuration
1010
# Base URL for API requests in local development

frontend/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@
7777
},
7878
"scripts": {
7979
"build:deploy_preview": "env-cmd -f .env.deploy_preview npm run build",
80-
"build:local": "env-cmd -f .env.local npm run build",
80+
"build:localhost": "env-cmd -f .env.localhost npm run build",
8181
"build:prod": "env-cmd -f .env.prod npm run build",
8282
"build:dev": "env-cmd -f .env.dev npm run build",
8383
"build": "vite build",
8484
"cleanup": "npx @biomejs/biome check --write --unsafe ./src",
8585
"cspell": "cspell '../**/*.{ts,tsx,css,md,py}' --no-progress --no-must-find-files --gitignore",
86-
"dev": "npm run local",
87-
"local": "env-cmd -f .env.local npm run start -- --host & npm run tsc:watch",
86+
"dev": "npm run localhost",
87+
"localhost": "env-cmd -f .env.localhost npm run start -- --host & npm run tsc:watch",
8888
"e2e": "npx playwright install chromium && npx playwright test --project=E2E_NIGHTLY --workers 4",
8989
"e2e-dev": "npx playwright install chromium && cross-env E2E_BASE_URL=https://dev.healthequitytracker.org npx playwright test --project=E2E_NIGHTLY --workers 5",
9090
"e2e-prod": "npx playwright install chromium && cross-env E2E_BASE_URL=https://healthequitytracker.org npx playwright test --project=E2E_NIGHTLY --workers 5",
@@ -95,9 +95,9 @@
9595
"e2e:new": "npm run e2e-new",
9696
"e2e:nightly": "npx playwright install chromium && npx playwright test --project=E2E_NIGHTLY --workers 2",
9797
"e2e:watch": "npx playwright install chromium && cross-env PWTEST_WATCH=1 npx playwright test --project=E2E_NIGHTLY --workers 2",
98-
"preview": "npm run build:local && vite preview",
98+
"preview": "npm run build:localhost && vite preview",
9999
"start:deploy_preview": "env-cmd -f .env.deploy_preview npm run start",
100-
"start:local": "env-cmd -f .env.local npm run start",
100+
"start:localhost": "env-cmd -f .env.localhost npm run start",
101101
"start": "vite",
102102
"test:coverage": "vitest run --coverage",
103103
"test:watch": "vitest watch",
@@ -121,4 +121,4 @@
121121
"last 1 safari version"
122122
]
123123
}
124-
}
124+
}

frontend/src/data/loading/DataFetcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class ApiDataFetcher implements DataFetcher {
4242
*/
4343
private shouldFetchAsStaticFile(fileName: string) {
4444
return (
45-
(this.environment.deployContext === 'local' &&
45+
(this.environment.deployContext === 'localhost' &&
4646
!this.environment.getBaseApiUrl()) ||
4747
this.environment.forceFetchDatasetAsStaticFile(fileName)
4848
)

frontend/src/utils/Environment.ts

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
1-
type DeployContext =
2-
// Production environment in GCP
3-
| 'prod'
4-
5-
// The "dev" GCP environment that gets auto-deployed from main branch to dev.healthequitytracker.org
6-
| 'dev'
7-
8-
// Deploy previews. Currently, all netlify deploys are considered deploy
9-
// previews.
10-
| 'deploy_preview'
11-
12-
// When running npm run dev, or when running Docker locally
13-
| 'local'
14-
15-
// Unit or integration tests
16-
| 'test'
17-
18-
// Unknown deploy context. This generally shouldn't happen.
19-
| 'unknown'
1+
const DEPLOY_CONTEXTS = [
2+
'prod', // Production environment in GCP (healthequitytracker.org)
3+
'dev', // Dev environment, auto-deployed from main (dev.healthequitytracker.org)
4+
'deploy_preview', // Netlify PR previews
5+
'localhost', // Local development
6+
'test', // Unit/integration tests
7+
'unknown', // Fallback — should not occur in practice
8+
] as const
9+
type DeployContext = (typeof DEPLOY_CONTEXTS)[number]
2010

2111
export interface Environment {
2212
/** The context the frontend is currently running in. */
@@ -39,10 +29,8 @@ export interface Environment {
3929

4030
/**
4131
* Whether to fetch the dataset as a static file from the public/tmp/
42-
* directory.
43-
*
44-
* This should only be used for local development or in-progress datasets. In
45-
* production, all datasets should be fetched from the data server.
32+
* directory. Should only be used for local development or in-progress
33+
* datasets not yet on the data server.
4634
*/
4735
forceFetchDatasetAsStaticFile: (fileName: string) => boolean
4836
}
@@ -67,9 +55,11 @@ class HetEnvironment implements Environment {
6755
}
6856

6957
getBaseApiUrl() {
70-
// If the API url isn't provided, requests are relative to current domain.
58+
// Empty string means API calls are relative to the current domain.
59+
// NOTE: Vite always loads .env.local regardless of mode — use .env.localhost
60+
// for local dev to avoid contaminating prod builds.
7161
const apiBaseUrl = this.getEnvVariable('BASE_API_URL')
72-
if (!apiBaseUrl && this.deployContext === 'local') {
62+
if (!apiBaseUrl && this.deployContext === 'localhost') {
7363
console.warn(
7464
'\n\n.ENV MISSING\n\n\nBASE_API_URL environment variable is not set. See the repo README for more information.',
7565
)
@@ -95,14 +85,9 @@ function getDeployContext(): DeployContext {
9585
return 'test'
9686
}
9787

98-
if (import.meta.env.NODE_ENV === 'local') {
99-
return 'local'
100-
}
101-
10288
const deployContextVar = import.meta.env.VITE_DEPLOY_CONTEXT
10389
if (deployContextVar) {
104-
const expectedContexts = ['prod', 'dev', 'deploy_preview', 'local']
105-
if (!expectedContexts.includes(deployContextVar)) {
90+
if (!DEPLOY_CONTEXTS.includes(deployContextVar as DeployContext)) {
10691
throw new Error('Invalid value for deploy context environment variable')
10792
}
10893
return deployContextVar as DeployContext

0 commit comments

Comments
 (0)