Hard-coding the login endpoint as http://localhost:3000/login works on the development machine. But send the page to a colleague and localhost in their browser points to their own computer. Where should the backend URL live? My first question is: Do you know the address before release, or will you deploy the same frontend to several environments and fill in the address at deployment time?
If you build separately for each environment, use an environment variable
If staging and production already run vite build separately, one VITE_BACKEND_URL variable is enough. For development, add this line to the frontend project's .env.development:
VITE_BACKEND_URL=http://localhost:3000
For production, the release script can supply the address at build time:
VITE_BACKEND_URL=https://api.example.com npm run build
Both environments use the same frontend code. The convention here is to leave the trailing slash off the configured address:
const backendUrl = import.meta.env.VITE_BACKEND_URL
if (!backendUrl) throw new Error('缺少 VITE_BACKEND_URL')
const loginUrl = `${backendUrl}/login`
Developers can change the address locally and the release script can pass a value directly, without maintaining a configuration loader for one string. Vite reads the .env files on startup, so restart the dev server after changing them. A production build puts the address into the generated JavaScript. Changing only the static server's environment variable afterward leaves the browser requesting the old address. Users can inspect any configuration you send to the browser. Keep secrets on the backend.1
If the same build goes to multiple sites, resolve the address later
The simplest case is when each site can forward /api/ to its own backend. The frontend requests /api/login directly, and the browser uses the current site's domain. During development, Vite can proxy that same path. Staging and production can then serve identical frontend files.2
If the browser must reach a separate API domain for each environment, I'd have the deployment script put a /config.json file in the site root, for example {"backendUrl":"https://api.example.com"}. On startup, the page first reads it with fetch('/config.json'), then uses the returned address for login. Switching environments requires replacing only that file. The distinction is how you read it: import config from './config.json' in the source bundles the value with the code. Requesting the file separately after startup lets you change the value at deployment time.3
YAML could also store the URL, but the browser would need a parser after fetching the YAML text. For just one URL, I'd use JSON, which the browser can read directly using response.json(). If operations already manages deployment parameters in YAML, keep using it. At release time, extract the address and pass it to Vite or generate the JSON file described above.4
I used the first approach in AssetMan. My May 3 commit replaced a hard-coded http://localhost:3000/login in the login request with import.meta.env.VITE_BACKEND_URL + '/login'. Switching backends now requires only supplying the build command with a new address. The login component can be reused as is.5
References
Footnotes
-
Vite contributors. (n.d.). Env variables and modes. Vite 6. Environment files, loading priorities, and build modes. ↩
-
Vite contributors. (n.d.). Server options. Vite 6. The dev server's server.proxy option. ↩
-
Vite contributors. (n.d.). Features. Vite 6. Importing JSON from source code. ↩
-
nsax91 & wolfstevent. (2012, December 9; 2014, March 26). Reading from YAML file in Javascript [Question and answer]. Stack Overflow. Example of invoking a parser after fetching YAML in the browser. ↩
-
CaiBorui [S2thend]. (2025, May 3). Add i18n feature; add configuration of backend url using env variable [Git commit]. GitHub. The article is dated to this change. The environment file and build command are teaching examples. Login request. ↩