Skip to content

Commit a482c3a

Browse files
authored
Merge pull request #86 from dsgler/betterDevEx
feat: 更好的开发体验,现在可以直接在localhost打开,而不用处理跨域问题,具体原理详见改动
2 parents b552442 + e352e29 commit a482c3a

File tree

5 files changed

+49
-16
lines changed

5 files changed

+49
-16
lines changed

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
VITE_HR_DOMAIN_BE=https://dev.back.recruitment2023.hustunique.com

.env.development

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
VITE_HR_DOMAIN_BE=/api

config/vite.config.dev.ts

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,38 @@
11
import { mergeConfig, UserConfig } from 'vite';
22
import baseConfig from './vite.config.base';
33

4-
export default mergeConfig(
5-
{
6-
server: {
7-
proxy: {
8-
'^/api': {
9-
target: 'https://dev.back.recruitment2023.hustunique.com',
10-
changeOrigin: true,
11-
credentials: 'include',
12-
rewrite: (path) => path.replace(/^\/api/, ''),
13-
cookieDomainRewrite: {
14-
'*': 'hustunique.com',
15-
},
4+
const devConfig: UserConfig = {
5+
server: {
6+
proxy: {
7+
// 将 src/constants/index.ts 中的 SERVER_DOMAIN 改为 "/api"
8+
// 通过代理解决本地调试跨域问题
9+
'^/api': {
10+
target: 'https://dev.back.recruitment2023.hustunique.com',
11+
changeOrigin: true,
12+
rewrite: (path) => path.replace(/^\/api/, ''),
13+
cookieDomainRewrite: {
14+
// 返回的 cookie 的 domain 设置为 localhost
15+
'*': 'localhost',
16+
},
17+
bypass(req, res) {
18+
if (!res) return;
19+
20+
// const { origin } = req.headers;
21+
22+
// 这两个 header 不对会 403,故直接删掉
23+
// 改成正确的可能更好,不过我懒
24+
delete req.headers.origin;
25+
delete req.headers.referer;
26+
27+
// 以下代码处理跨域情况,不过现在不跨域了所以不需要了
28+
// if (origin) {
29+
// res.setHeader('Access-Control-Allow-Headers', origin);
30+
// }
31+
// res.setHeader('Access-Control-Allow-Credentials', 'true');
1632
},
1733
},
1834
},
1935
},
20-
baseConfig as UserConfig,
21-
);
36+
};
37+
38+
export default mergeConfig(devConfig, baseConfig as UserConfig);

src/constants/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
export const HR_DOMAIN_BE: string =
2-
'https://dev.back.recruitment2023.hustunique.com';
1+
export const HR_DOMAIN_BE: string = import.meta.env.VITE_HR_DOMAIN_BE;
32

43
export const HR_DOMAIN_FE: string = 'hr2024.hustunique.com';
54

src/vite-env.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,18 @@ declare module '*.vue' {
66
const component: DefineComponent<{}, {}, any>;
77
export default component;
88
}
9+
10+
interface ViteTypeOptions {
11+
// 添加这行代码,你就可以将 ImportMetaEnv 的类型设为严格模式,
12+
// 这样就不允许有未知的键值了。
13+
strictImportMetaEnv: unknown;
14+
}
15+
16+
interface ImportMetaEnv {
17+
readonly VITE_HR_DOMAIN_BE: string;
18+
// 更多环境变量...
19+
}
20+
21+
interface ImportMeta {
22+
readonly env: ImportMetaEnv;
23+
}

0 commit comments

Comments
 (0)