Skip to content

Commit eafdcc0

Browse files
committed
feat: fronted env
1 parent d7d7477 commit eafdcc0

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

.env.example

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
VITE_INCLUDE_START_PACK=false
1+
# Vite 客户端可访问的环境变量
2+
VITE_BACKEND_URL="http://localhost:3000"
3+
VITE_PUBLIC_URL="http://localhost:5174"

.github/workflows/deploy-pages.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ jobs:
4040

4141
- name: Build for GitHub Pages
4242
run: yarn build:pages
43+
env:
44+
NODE_ENV: production
45+
VITE_BACKEND_URL: ${{ secrets.VITE_BACKEND_URL }}
46+
VITE_PUBLIC_URL: ${{ secrets.VITE_PUBLIC_URL }}
4347

4448
- name: Setup Pages
4549
# 使用官方 Action 配置 GitHub Pages

docs/05-build-and-deploy.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,48 @@ yarn preview
104104
* 它会执行 `build:pages` 脚本来构建项目。
105105
* 构建成功后,它会将 `dist` 目录下的内容自动部署到您的 GitHub Pages。
106106

107+
**配置 GitHub Secrets:**
108+
109+
在部署之前,您需要在 GitHub 仓库中配置以下 Secrets:
110+
111+
1. **进入仓库设置**: 在您的 GitHub 仓库页面,点击 `Settings` 标签。
112+
2. **找到 Secrets**: 在左侧菜单中,点击 `Secrets and variables` -> `Actions`
113+
3. **添加 Secrets**: 点击 `New repository secret` 按钮,添加以下两个 Secrets:
114+
115+
**`VITE_BACKEND_URL`**
116+
- ****: 您的后端 API 生产环境地址
117+
- **示例**: `https://api.your-domain.com`
118+
- **说明**: 前端应用将使用此地址连接后端服务
119+
120+
**`VITE_PUBLIC_URL`**
121+
- ****: 您的 GitHub Pages 应用完整 URL
122+
- **示例**: `https://your-username.github.io/your-repo-name`
123+
- **说明**: 用于生成应用内的绝对路径链接
124+
107125
**如何使用:**
108126
1. 确保您的仓库已经开启了 GitHub Pages 功能(在 `Settings` -> `Pages` 中,将 `Source` 设置为 `GitHub Actions`)。
109-
2. 将您的代码推送到 `main` 分支。
110-
3. 稍等片刻,GitHub Actions 完成后,您的游戏就会在 GitHub Pages 网址上生效。
127+
2. 配置上述 GitHub Secrets。
128+
3. 将您的代码推送到 `main` 分支。
129+
4. 稍等片刻,GitHub Actions 完成后,您的游戏就会在 GitHub Pages 网址上生效。
111130

112131
这个自动化流程取代了原有的 `yarn deploy` 手动部署方式,让部署过程更可靠、更高效。
132+
133+
**环境变量说明:**
134+
135+
- **开发环境**: 前端默认连接到 `http://localhost:3000`
136+
- **生产环境**: 前端连接到您在 `VITE_BACKEND_URL` Secret 中配置的地址
137+
- **本地开发**: 如需自定义后端地址,可在项目根目录创建 `.env` 文件并设置 `VITE_BACKEND_URL`
138+
139+
**失败回退机制:**
140+
141+
如果环境变量未配置或配置错误,系统会按以下优先级回退:
142+
143+
1. **`VITE_BACKEND_URL` 未设置时**:
144+
- 开发环境: 回退到 `http://localhost:3000`
145+
- 生产环境: 回退到当前页面域名 (`window.location.origin`)
146+
147+
2. **`VITE_PUBLIC_URL` 未设置时**:
148+
- 回退到当前页面域名 (`window.location.origin`)
149+
150+
3. **GitHub Secrets 未配置时**:
151+
- 构建会失败,需要正确配置 Secrets 才能部署

src/games/demo-with-backend/services/trpc.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import { createTRPCProxyClient, httpBatchLink } from '@trpc/client';
22
import type { AppRouter } from '@tobenot/basic-web-game-backend-contract';
33

44
const getBaseUrl = () => {
5-
return import.meta.env.VITE_BACKEND_URL || 'http://localhost:3000';
5+
if (import.meta.env.VITE_BACKEND_URL) {
6+
return import.meta.env.VITE_BACKEND_URL;
7+
}
8+
if (import.meta.env.DEV) {
9+
return 'http://localhost:3000';
10+
}
11+
return window.location.origin;
612
};
713

814
export const trpc = createTRPCProxyClient<AppRouter>({

0 commit comments

Comments
 (0)