Skip to content

Commit a6f759b

Browse files
committed
fix: server load issue
1 parent e7f7b97 commit a6f759b

File tree

6 files changed

+752
-47
lines changed

6 files changed

+752
-47
lines changed

client/next.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ const getServerApiUrl = () => {
1818
const nextConfig = {
1919
assetPrefix: config.CLIENT_ASSET_PREFIX || '/',
2020
i18n: {
21-
locales: locales && locales.length > 0 ? locales : ['zh', 'en'],
22-
defaultLocale: defaultLocale || 'zh',
21+
locales,
22+
defaultLocale,
2323
},
2424
env: {
2525
SERVER_API_URL: getServerApiUrl(),

client/server.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const port = config.CLIENT_PORT || 3001;
77
try {
88
cli.nextDev(['-p', port]);
99
console.log(`[ReactPress] 客户端已启动,端口:${port}`);
10+
// 自动打开
11+
require('open')(`http://localhost:${port}`);
1012
} catch (err) {
1113
console.log(`[ReactPress] 客户端启动失败!${err.message || err}`);
1214
}

config/src/i18n.ts

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,34 @@ interface I18nResult {
1313

1414
function parseI18n(): I18nResult {
1515
// 本地的国际化文案
16-
const localesDir = path.join(__dirname, '../../locales');
16+
// 尝试多种路径来确保在不同运行环境下都能找到locales目录
17+
const possiblePaths = [
18+
path.join(__dirname, '../../locales'),
19+
path.join(__dirname, '../../../locales'),
20+
path.join(__dirname, '../locales'),
21+
path.join(process.cwd(), 'locales'),
22+
path.join(process.cwd(), '../locales'),
23+
path.join(process.cwd(), '../../locales')
24+
];
1725

18-
if (!fs.existsSync(localesDir)) {
26+
let localesDir = '';
27+
for (const possiblePath of possiblePaths) {
28+
if (fs.existsSync(possiblePath)) {
29+
localesDir = possiblePath;
30+
break;
31+
}
32+
}
33+
34+
if (!localesDir) {
35+
// 如果在所有预期路径都找不到,则使用项目根目录的locales
36+
const projectRoot = findProjectRoot();
37+
const fallbackPath = path.join(projectRoot, 'locales');
38+
if (fs.existsSync(fallbackPath)) {
39+
localesDir = fallbackPath;
40+
}
41+
}
42+
43+
if (!localesDir) {
1944
return { messages: {}, locales: [], defaultLocale: '' };
2045
}
2146

@@ -32,4 +57,34 @@ function parseI18n(): I18nResult {
3257
return { messages, locales, defaultLocale };
3358
}
3459

35-
export const { messages, locales, defaultLocale } = parseI18n();
60+
function findProjectRoot(): string {
61+
// 查找项目根目录(包含package.json的目录)
62+
let currentDir = __dirname;
63+
64+
// 向上查找最多10层目录
65+
for (let i = 0; i < 10; i++) {
66+
const packageJsonPath = path.join(currentDir, 'package.json');
67+
if (fs.existsSync(packageJsonPath)) {
68+
try {
69+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
70+
// 检查是否是主项目package.json
71+
if (packageJson.name === 'reactpress' || packageJson.workspaces) {
72+
return currentDir;
73+
}
74+
} catch (e) {
75+
// 解析失败,继续向上查找
76+
}
77+
}
78+
const parentDir = path.dirname(currentDir);
79+
// 如果已经到达文件系统根目录,停止查找
80+
if (parentDir === currentDir) {
81+
break;
82+
}
83+
currentDir = parentDir;
84+
}
85+
86+
// 如果找不到项目根目录,返回当前工作目录
87+
return process.cwd();
88+
}
89+
90+
export const { messages, locales, defaultLocale } = parseI18n();

0 commit comments

Comments
 (0)