|
1 | 1 | /* eslint-disable @typescript-eslint/no-var-requires */
|
2 | 2 | const express = require('express');
|
3 | 3 | const next = require('next');
|
| 4 | +const cookieParser = require('cookie-parser'); |
4 | 5 |
|
5 | 6 | const app = next({ dev: false });
|
6 | 7 | const handle = app.getRequestHandler();
|
7 | 8 |
|
8 | 9 | const PORT = process.env.PORT || 3000;
|
| 10 | +const DEFAULT_LOCALE = 'zh'; |
| 11 | +const FALLBACK_LOCALE = 'en'; |
| 12 | +const SUPPORTED_LOCALES = ['zh', 'en']; |
9 | 13 |
|
10 | 14 | async function startServer() {
|
11 | 15 | try {
|
12 | 16 | await app.prepare();
|
13 | 17 | const server = express();
|
| 18 | + server.use(cookieParser()); |
14 | 19 |
|
15 |
| - // 中间件处理尾部斜杠但不重定向 |
| 20 | + // 中间件处理语言选择和路由 |
16 | 21 | server.use((req, res, next) => {
|
17 |
| - if (req.url.length > 1 && req.url.endsWith('/')) { |
18 |
| - req.url = req.url.slice(0, -1); |
| 22 | + if (req.url.startsWith('/_next') || req.url.startsWith('/static')) { |
| 23 | + next(); |
| 24 | + } else { |
| 25 | + let locale = req.cookies.locale; |
| 26 | + |
| 27 | + // 解析 URL 中的语言设置 |
| 28 | + const urlParts = req.path.split('/'); |
| 29 | + const urlLocale = SUPPORTED_LOCALES.includes(urlParts[1]) |
| 30 | + ? urlParts[1] |
| 31 | + : null; |
| 32 | + |
| 33 | + if (!locale) { |
| 34 | + // 如果没有 cookie,使用 URL 中的语言或浏览器偏好 |
| 35 | + const browserLocale = req.acceptsLanguages(SUPPORTED_LOCALES); |
| 36 | + locale = urlLocale || browserLocale || FALLBACK_LOCALE; |
| 37 | + |
| 38 | + // 如果浏览器语言不是中文,默认使用英语 |
| 39 | + if (browserLocale && browserLocale !== 'zh') { |
| 40 | + locale = FALLBACK_LOCALE; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + // 确保 locale 是支持的语言之一 |
| 45 | + locale = SUPPORTED_LOCALES.includes(locale) ? locale : FALLBACK_LOCALE; |
| 46 | + if (urlLocale) { |
| 47 | + // URL 中有语言前缀 |
| 48 | + if (urlLocale !== locale) { |
| 49 | + // URL 语言与 cookie 不匹配,更新 locale |
| 50 | + locale = urlLocale; |
| 51 | + } |
| 52 | + if (locale === DEFAULT_LOCALE) { |
| 53 | + // 默认语言不应该在 URL 中显示,重定向到无前缀的 URL |
| 54 | + const newUrl = req.url.replace(`/${DEFAULT_LOCALE}`, '') || '/'; |
| 55 | + return res.redirect(307, newUrl); |
| 56 | + } |
| 57 | + } else if (locale !== DEFAULT_LOCALE) { |
| 58 | + // URL 中没有语言前缀,且不是默认语言,添加前缀并重定向 |
| 59 | + let newUrl = `/${locale}${req.url}`; |
| 60 | + if (newUrl.endsWith('/')) { |
| 61 | + // 如果以 '/' 结尾,去掉末尾的 '/' |
| 62 | + newUrl = newUrl.slice(0, -1); |
| 63 | + } |
| 64 | + return res.redirect(307, newUrl); |
| 65 | + } |
| 66 | + |
| 67 | + // 设置或更新 cookie |
| 68 | + res.cookie('locale', locale, { |
| 69 | + maxAge: 365 * 24 * 60 * 60 * 1000, |
| 70 | + }); |
| 71 | + |
| 72 | + req.locale = locale; |
| 73 | + |
| 74 | + // 处理尾部斜杠并重定向 |
| 75 | + if (req.url.length > 1 && req.url.endsWith('/')) { |
| 76 | + const newUrl = |
| 77 | + req.url.slice(0, -1) + |
| 78 | + (req.url.includes('?') ? req.url.slice(req.url.indexOf('?')) : ''); |
| 79 | + return res.redirect(301, newUrl); |
| 80 | + } |
| 81 | + |
| 82 | + next(); |
19 | 83 | }
|
20 |
| - next(); |
21 | 84 | });
|
22 | 85 |
|
23 | 86 | // 处理所有其他请求
|
24 |
| - server.all('*', (req, res) => handle(req, res)); |
| 87 | + server.all('*', (req, res) => { |
| 88 | + return handle(req, res); |
| 89 | + }); |
25 | 90 |
|
26 | 91 | server.listen(PORT, (err) => {
|
27 | 92 | if (err) throw err;
|
|
0 commit comments