Skip to content

Commit c10ff2f

Browse files
committed
feat: env url
1 parent eafdcc0 commit c10ff2f

File tree

9 files changed

+105
-5
lines changed

9 files changed

+105
-5
lines changed

.env.example

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

.github/workflows/deploy-pages.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ jobs:
4242
run: yarn build:pages
4343
env:
4444
NODE_ENV: production
45+
GITHUB_PAGES: true
4546
VITE_BACKEND_URL: ${{ secrets.VITE_BACKEND_URL }}
4647
VITE_PUBLIC_URL: ${{ secrets.VITE_PUBLIC_URL }}
48+
VITE_BASE_URL: /Basic-Web-Game/
4749

4850
- name: Setup Pages
4951
# 使用官方 Action 配置 GitHub Pages

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"build": "tsc && vite build",
99
"build:itch": "node scripts/build-itch.js",
1010
"build:pages": "cross-env GITHUB_PAGES=true vite build",
11+
"test:env": "node scripts/test-env.js",
1112
"deploy": "npm run build:pages && gh-pages -d dist",
1213
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
1314
"preview": "vite preview",

scripts/test-env.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env node
2+
3+
console.log('Testing environment variables...');
4+
5+
const env = {
6+
GITHUB_PAGES: process.env.GITHUB_PAGES,
7+
VITE_BACKEND_URL: process.env.VITE_BACKEND_URL,
8+
VITE_PUBLIC_URL: process.env.VITE_PUBLIC_URL,
9+
NODE_ENV: process.env.NODE_ENV,
10+
MODE: process.env.MODE
11+
};
12+
13+
console.log('Environment variables:', env);
14+
15+
// 模拟Vite的define配置
16+
const defineConfig = {
17+
'import.meta.env.VITE_BACKEND_URL': JSON.stringify(process.env.VITE_BACKEND_URL),
18+
'import.meta.env.VITE_PUBLIC_URL': JSON.stringify(process.env.VITE_PUBLIC_URL),
19+
'import.meta.env.BASE_URL': JSON.stringify(process.env.GITHUB_PAGES ? '/Basic-Web-Game/' : './')
20+
};
21+
22+
console.log('Define config:', defineConfig);

src/carrot/services/ResourceLoader.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@ class ResourceLoader {
22
private readonly baseUrl: string;
33

44
constructor() {
5-
this.baseUrl = import.meta.env.BASE_URL;
5+
// 优先使用环境变量中的BASE_URL,如果没有则使用import.meta.env.BASE_URL
6+
this.baseUrl = import.meta.env.BASE_URL || import.meta.env.VITE_BASE_URL || '/';
67
console.log(`ResourceLoader initialized with base URL: ${this.baseUrl}`);
8+
console.log('Environment variables:', {
9+
BASE_URL: import.meta.env.BASE_URL,
10+
VITE_BASE_URL: import.meta.env.VITE_BASE_URL,
11+
VITE_BACKEND_URL: import.meta.env.VITE_BACKEND_URL,
12+
VITE_PUBLIC_URL: import.meta.env.VITE_PUBLIC_URL,
13+
MODE: import.meta.env.MODE,
14+
DEV: import.meta.env.DEV
15+
});
716
}
817

918
/**
@@ -29,6 +38,7 @@ class ResourceLoader {
2938
*/
3039
public async loadJSON<T>(path: string): Promise<T> {
3140
const url = this.buildUrl(path);
41+
console.log(`[ResourceLoader] Loading JSON from: ${url}`);
3242
try {
3343
const response = await fetch(url);
3444
if (!response.ok) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import React from 'react';
2+
3+
export const DebugInfo: React.FC = () => {
4+
// 只在开发环境或URL包含debug参数时显示
5+
const shouldShow = import.meta.env.DEV || window.location.search.includes('debug=true');
6+
7+
if (!shouldShow) {
8+
return null;
9+
}
10+
11+
const envInfo = {
12+
VITE_BACKEND_URL: import.meta.env.VITE_BACKEND_URL,
13+
VITE_PUBLIC_URL: import.meta.env.VITE_PUBLIC_URL,
14+
BASE_URL: import.meta.env.BASE_URL,
15+
MODE: import.meta.env.MODE,
16+
DEV: import.meta.env.DEV,
17+
window_location_origin: window.location.origin,
18+
window_location_href: window.location.href,
19+
window_location_pathname: window.location.pathname
20+
};
21+
22+
return (
23+
<div className="fixed top-4 right-4 bg-black bg-opacity-75 text-white p-4 rounded-lg text-xs max-w-md z-50">
24+
<h3 className="font-bold mb-2">调试信息</h3>
25+
<pre className="whitespace-pre-wrap overflow-auto max-h-64">
26+
{JSON.stringify(envInfo, null, 2)}
27+
</pre>
28+
</div>
29+
);
30+
};

src/games/demo-with-backend/index.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
import React from 'react';
22
import { LoginScreen } from './components/LoginScreen';
33
import { Dashboard } from './components/Dashboard';
4+
import { DebugInfo } from './components/DebugInfo';
45
import { useAuth } from './hooks/useAuth';
56
import { GameShell } from '../../carrot/components/GameShell';
67

78
export const DemoWithBackend: React.FC = () => {
89
const { user, isLoading } = useAuth();
910

11+
// 添加环境变量调试信息
12+
React.useEffect(() => {
13+
console.log('DemoWithBackend Environment Variables:', {
14+
VITE_BACKEND_URL: import.meta.env.VITE_BACKEND_URL,
15+
VITE_PUBLIC_URL: import.meta.env.VITE_PUBLIC_URL,
16+
BASE_URL: import.meta.env.BASE_URL,
17+
MODE: import.meta.env.MODE,
18+
DEV: import.meta.env.DEV,
19+
window_location_origin: window.location.origin,
20+
window_location_href: window.location.href
21+
});
22+
}, []);
23+
1024
if (isLoading) {
1125
return (
1226
<GameShell orientation="landscape">
@@ -16,13 +30,15 @@ export const DemoWithBackend: React.FC = () => {
1630
<p className="text-gray-600">正在加载...</p>
1731
</div>
1832
</div>
33+
<DebugInfo />
1934
</GameShell>
2035
);
2136
}
2237

2338
return (
2439
<GameShell orientation="landscape">
2540
{user ? <Dashboard /> : <LoginScreen />}
41+
<DebugInfo />
2642
</GameShell>
2743
);
2844
};

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

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

44
const getBaseUrl = () => {
5+
console.log('Environment variables for tRPC:', {
6+
VITE_BACKEND_URL: import.meta.env.VITE_BACKEND_URL,
7+
DEV: import.meta.env.DEV,
8+
MODE: import.meta.env.MODE,
9+
window_location_origin: typeof window !== 'undefined' ? window.location.origin : 'undefined'
10+
});
11+
512
if (import.meta.env.VITE_BACKEND_URL) {
13+
console.log('Using VITE_BACKEND_URL:', import.meta.env.VITE_BACKEND_URL);
614
return import.meta.env.VITE_BACKEND_URL;
715
}
816
if (import.meta.env.DEV) {
17+
console.log('Using localhost:3000 for development');
918
return 'http://localhost:3000';
1019
}
20+
console.log('Using window.location.origin as fallback');
1121
return window.location.origin;
1222
};
1323

24+
const baseUrl = getBaseUrl();
25+
console.log('Final tRPC base URL:', baseUrl);
26+
1427
export const trpc = createTRPCProxyClient<AppRouter>({
1528
links: [
1629
httpBatchLink({
17-
url: `${getBaseUrl()}/api/trpc`,
30+
url: `${baseUrl}/api/trpc`,
1831
}),
1932
],
2033
});

vite.config.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ export default defineConfig(({ command, mode }) => {
1010
console.log('Build Config:', {
1111
mode,
1212
command,
13-
VITE_INCLUDE_START_PACK: env.VITE_INCLUDE_START_PACK
13+
VITE_INCLUDE_START_PACK: env.VITE_INCLUDE_START_PACK,
14+
VITE_BACKEND_URL: env.VITE_BACKEND_URL,
15+
VITE_PUBLIC_URL: env.VITE_PUBLIC_URL,
16+
GITHUB_PAGES: process.env.GITHUB_PAGES
1417
})
1518

1619
return {
@@ -30,7 +33,10 @@ export default defineConfig(({ command, mode }) => {
3033
}
3134
},
3235
define: {
33-
'import.meta.env.VITE_INCLUDE_START_PACK': JSON.stringify(env.VITE_INCLUDE_START_PACK)
36+
'import.meta.env.VITE_INCLUDE_START_PACK': JSON.stringify(env.VITE_INCLUDE_START_PACK),
37+
'import.meta.env.VITE_BACKEND_URL': JSON.stringify(env.VITE_BACKEND_URL),
38+
'import.meta.env.VITE_PUBLIC_URL': JSON.stringify(env.VITE_PUBLIC_URL),
39+
'import.meta.env.BASE_URL': JSON.stringify(process.env.GITHUB_PAGES ? `/${pkg.name}/` : './')
3440
}
3541
}
3642
})

0 commit comments

Comments
 (0)