Skip to content

fix: read VITE_API_BASE_URL from .env for Vite dev proxy#482

Open
octo-patch wants to merge 1 commit into666ghj:mainfrom
octo-patch:fix/issue-480-vite-proxy-env-var
Open

fix: read VITE_API_BASE_URL from .env for Vite dev proxy#482
octo-patch wants to merge 1 commit into666ghj:mainfrom
octo-patch:fix/issue-480-vite-proxy-env-var

Conversation

@octo-patch
Copy link
Copy Markdown

Fixes #480

Problem

The Vite development proxy target in frontend/vite.config.js was hardcoded to http://localhost:5001. Even when users set VITE_API_BASE_URL in their .env file to point to a custom backend host or port, the dev proxy continued routing to localhost:5001 — making the variable half-effective during development. Additionally, VITE_API_BASE_URL was absent from .env.example, so users had no way to discover that the variable existed.

Solution

  • frontend/vite.config.js: Switched from defineConfig({}) to the function form defineConfig(({ mode }) => { ... }) and used Vite's loadEnv() to read VITE_API_BASE_URL from the project root .env file. The proxy target now falls back to http://localhost:5001 when the variable is unset, preserving existing default behaviour.
  • .env.example: Added a commented-out VITE_API_BASE_URL entry with a short explanation, so users deploying on non-default hosts can discover and enable it without digging through source code.

Testing

  • Default behaviour (no VITE_API_BASE_URL set): proxy still targets http://localhost:5001 — no regression.
  • Custom host (e.g. VITE_API_BASE_URL=http://192.168.1.100:5001 in .env): npm run dev now proxies API requests to the configured host, matching what the axios client already does at runtime.

…j#480)

The Vite development proxy target was hardcoded to http://localhost:5001,
ignoring the VITE_API_BASE_URL environment variable that the axios client
already respects. This caused the dev proxy to still route to localhost even
when users configured a remote or custom backend address.

- Use loadEnv() in vite.config.js to read VITE_API_BASE_URL from the project
  root .env file and pass it as the proxy target (fallback: localhost:5001)
- Document VITE_API_BASE_URL in .env.example so users know the variable exists
@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

为什么要写死后端的端口呢?

1 participant