Skip to content

Commit cc30776

Browse files
committed
fix: server run bug
1 parent dbe5d1c commit cc30776

File tree

6 files changed

+44
-198
lines changed

6 files changed

+44
-198
lines changed

next-i18next.config.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
//next-i18next.config.js
22

33
module.exports = {
4-
// debug: true,
54
i18n: {
65
defaultLocale: 'zh',
76
locales: ['zh', 'en'],
87
},
9-
108
// eslint-disable-next-line @typescript-eslint/no-var-requires
119
localePath: require('path').resolve('./public/locales'), // 指定翻译文件的路径
1210
};

next.config.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ const { i18n } = require('./next-i18next.config');
99

1010
module.exports = withBundleAnalyzer({
1111
i18n,
12+
reactStrictMode: true,
13+
compress: true, // 启用压缩
1214
eslint: {
1315
dirs: ['src'],
1416
},
15-
16-
reactStrictMode: false,
17-
1817
// Uncoment to add domain whitelist
1918
images: {
2019
domains: [
@@ -43,11 +42,10 @@ module.exports = withBundleAnalyzer({
4342
test: /\.md$/,
4443
use: 'raw-loader',
4544
});
46-
4745
return config;
4846
},
49-
5047
// experimental: {
48+
// disableOptimizedLoading: true,
5149
// scrollRestoration: true,
5250
// },
5351
});

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
"next": "^12.1.6",
3737
"next-i18next": "^15.3.0",
3838
"nprogress": "^0.2.0",
39-
"pino-http": "^8.2.1",
4039
"raw-loader": "^4.0.2",
4140
"rc-image": "^7.0.0-2",
4241
"rc-util": "^5.34.1",

server.js

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,37 @@
11
/* eslint-disable @typescript-eslint/no-var-requires */
2+
const express = require('express');
23
const next = require('next');
4+
35
const dev = process.env.NEXT_PUBLIC_ENV !== 'production';
46
const app = next({ dev });
57
const handle = app.getRequestHandler();
68

7-
const { parse } = require('url');
8-
9-
const express = require('express');
10-
const expressRouter = express.Router();
11-
const server = express();
12-
// this is the logger for the server
13-
var logger = require('pino-http')();
9+
const PORT = process.env.PORT || 3000;
1410

15-
const NODE_PORT = process.env.NODE_PORT | 3000;
11+
async function startServer() {
12+
try {
13+
await app.prepare();
14+
const server = express();
1615

17-
app.prepare().then(() => {
18-
expressRouter.get('*', (req, res) => {
19-
// 页面路由
20-
logger(req, res);
21-
const parsedUrl = parse(req.url, true);
22-
const { pathname, query } = parsedUrl;
23-
if (pathname.length > 1 && pathname.endsWith('/')) {
24-
return app.render(req, res, pathname.slice(0, -1), query);
25-
} else {
26-
return app.render(req, res, pathname, query);
27-
}
28-
});
29-
server.use('/', expressRouter);
16+
// 中间件处理尾部斜杠但不重定向
17+
server.use((req, res, next) => {
18+
if (req.url.length > 1 && req.url.endsWith('/')) {
19+
req.url = req.url.slice(0, -1);
20+
}
21+
next();
22+
});
3023

31-
server.all('*', (req, res) => {
32-
// Be sure to pass `true` as the second argument to `url.parse`.
33-
// This tells it to parse the query portion of the URL.
34-
const parsedUrl = parse(req.url, true);
24+
// 处理所有其他请求
25+
server.all('*', (req, res) => handle(req, res));
3526

36-
return handle(req, res, parsedUrl);
37-
});
27+
server.listen(PORT, (err) => {
28+
if (err) throw err;
29+
console.log(`> Ready on http://localhost:${PORT}`);
30+
});
31+
} catch (error) {
32+
console.error('Error starting server:', error);
33+
process.exit(1);
34+
}
35+
}
3836

39-
server.listen(NODE_PORT, () =>
40-
console.log('App listening on port ' + NODE_PORT)
41-
);
42-
});
37+
startServer();

src/pages/repository/[rid]/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const RepositoryPage: NextPage<Props> = ({ repo }) => {
4242
t={t}
4343
i18n_lang={i18n.language}
4444
/>
45-
<div className='h-8 lg:h-36'></div>
45+
<div className='h-8 lg:h-36' />
4646
</>
4747
);
4848
};

0 commit comments

Comments
 (0)