Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
fix: 修复i18n跳转错误
Browse files Browse the repository at this point in the history
  • Loading branch information
IronKinoko committed Sep 17, 2020
1 parent 944907c commit 412d3c3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
3 changes: 3 additions & 0 deletions app/localeSubpaths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const localeSubpaths = { zh: 'zh-cn', th: 'th' }

module.exports = localeSubpaths
3 changes: 1 addition & 2 deletions app/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ const { nextI18NextRewrites } = require('next-i18next/rewrites')
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
})

const localeSubpaths = { zh: 'zh-cn', th: 'th' }
const localeSubpaths = require('./localeSubpaths')
const nextConfig = {
// target: 'serverless',
// assetPrefix: isProd ? (isVercel ? '' : '/e-hentai-view') : '',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "e-hentai-view",
"productName": "EhentaiView",
"version": "3.9.2",
"version": "3.9.3",
"main": "server/src/app.js",
"license": "MIT",
"homepage": "https://exhentai.appspot.com",
Expand Down
28 changes: 26 additions & 2 deletions server/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require('path')
const next = require('next')
const createProxyMiddleware = require('http-proxy-middleware')
.createProxyMiddleware
// const nextI18next = require('../../app/i18n')
const localeSubpaths = require('../../app/localeSubpaths')
const devProxy = {
'/api': {
target: 'http://localhost:8080/', // 端口自己配置合适的
Expand All @@ -20,13 +20,37 @@ const handle = app.getRequestHandler()
await app.prepare()

const server = dev ? require('express')() : require('./app')

if (dev && devProxy) {
const cookieParser = require('cookie-parser')
server.use(cookieParser())
Object.keys(devProxy).forEach(function (context) {
server.use(createProxyMiddleware(context, devProxy[context]))
})
}

server.get('/', (req, res) => {
if (!req.cookies['next-i18next']) {
const lang = req.headers['accept-language'].slice(0, 2)
if (localeSubpaths[lang]) {
res.redirect('/' + localeSubpaths[lang])
} else {
res.cookie('next-i18next', 'en', {
maxAge: 6 * 31 * 24 * 60 * 60 * 1000,
path: '/',
})
res.redirect('/')
}
} else {
/** @type {string} */
const lang = req.cookies['next-i18next']
if (lang !== 'en') {
res.redirect('/' + localeSubpaths[lang])
} else {
handle(req, res)
}
}
})

// await nextI18next.initPromise

server.get('/service-worker.js', (req, res) => {
Expand Down

0 comments on commit 412d3c3

Please sign in to comment.