From 4b32f4bdadea03400567e02f9c4057b56177fd55 Mon Sep 17 00:00:00 2001 From: Diablohu Date: Wed, 1 Apr 2020 18:49:02 +0800 Subject: [PATCH] fix #232 --- CHANGELOG.md | 2 + .../server/generate-html-redirect-metas.js | 76 ++++++++++--------- .../koot/i18n/server/use-router-redirect.js | 48 ++++++------ 3 files changed, 68 insertions(+), 58 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d729ce18..32fa5f4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ **koot** - 错误修正 + - 开发环境中多语言相关的服务器跳转地址为 `https://` 的问题 ([#232](https://github.com/cmux/koot/issues/232)) + - 开发环境中的相关跳转地址现在会无视 `proxyRequestOrigin.protocol` 配置项,强制为 `http://` - 开发环境中组件热更新时会重新挂载 (re-mount) 的问题 ([#233](https://github.com/cmux/koot/issues/233)) ## [0.13.10] - 2020-03-25 diff --git a/packages/koot/i18n/server/generate-html-redirect-metas.js b/packages/koot/i18n/server/generate-html-redirect-metas.js index 9fc384ae..d7c49cea 100644 --- a/packages/koot/i18n/server/generate-html-redirect-metas.js +++ b/packages/koot/i18n/server/generate-html-redirect-metas.js @@ -1,74 +1,80 @@ -import { changeLocaleQueryKey } from '../../defaults/defines' -import availableLocaleIds from '../locale-ids' -import isI18nEnabled from '../../i18n/is-enabled' +import { changeLocaleQueryKey } from '../../defaults/defines'; +import availableLocaleIds from '../locale-ids'; +import isI18nEnabled from '../../i18n/is-enabled'; /** * 生成用以声明该页面其他语种 URL 的 meta 标签的 HTML 代码 - * @param {Object} options - * @param {Object} options.ctx + * @param {Object} options + * @param {Object} options.ctx * @param {Object} options.proxyRequestOrigin Koot 配置: server.proxyRequestOrigin * @param {String} options.localeId 当前语种 * @returns {String} HTML 代码 */ -const generateHtmlRedirectMetas = ({ ctx, localeId/*, proxyRequestOrigin*/ }) => { - if (!isI18nEnabled()) - return '' +const generateHtmlRedirectMetas = ({ + ctx, + localeId /*, proxyRequestOrigin*/, +}) => { + if (!isI18nEnabled()) return ''; // let { href, origin } = ctx // if (typeof proxyRequestOrigin.protocol === 'string') { // origin = origin.replace(/^http:\/\//, `${proxyRequestOrigin.protocol}://`) // href = href.replace(/^http:\/\//, `${proxyRequestOrigin.protocol}://`) // } - const { - hrefTrue: href, - originTrue: origin - } = ctx + const { hrefTrue: href, originTrue: origin } = ctx; - const isUseRouter = process.env.KOOT_I18N_URL_USE === 'router' + const isUseRouter = process.env.KOOT_I18N_URL_USE === 'router'; - let html = availableLocaleIds//getLocaleIds() - .filter(thisLocaleId => thisLocaleId !== localeId) - .map(l => { - - let thisHref = '' + let html = availableLocaleIds //getLocaleIds() + .filter((thisLocaleId) => thisLocaleId !== localeId) + .map((l) => { + let thisHref = ''; if (isUseRouter) { - thisHref = origin - + href + thisHref = + origin + + href .replace(new RegExp(`^${origin}`), '') .replace(new RegExp(`^${localeId}`), l) - .replace(new RegExp(`^/${localeId}`), '/' + l) + .replace(new RegExp(`^/${localeId}`), '/' + l); } else { thisHref = (() => { if (ctx.query[changeLocaleQueryKey] === '') { return href.replace( new RegExp(`${changeLocaleQueryKey}=`), `${changeLocaleQueryKey}=${l}` - ) + ); } if (typeof ctx.query[changeLocaleQueryKey] === 'string') return href.replace( new RegExp(`${changeLocaleQueryKey}=[a-zA-Z-_]+`), `${changeLocaleQueryKey}=${l}` - ) - return href + (ctx.querystring ? `&` : ( - href.substr(href.length - 1) === '?' + ); + return ( + href + + (ctx.querystring + ? `&` + : href.substr(href.length - 1) === '?' ? '' - : `?` - )) + `${changeLocaleQueryKey}=${l}` - })() + : `?`) + + `${changeLocaleQueryKey}=${l}` + ); + })(); } - if (__DEV__) thisHref = thisHref.replace('://localhost', '://127.0.0.1') - return `` + if (__DEV__) + thisHref = thisHref + .replace('://localhost', '://127.0.0.1') + .replace(/^https:\/\//, 'http://'); + return ``; }) - .join('') + .join(''); if (isUseRouter) { - html += `` + html += ``; } - return html -} + return html; +}; -export default generateHtmlRedirectMetas +export default generateHtmlRedirectMetas; diff --git a/packages/koot/i18n/server/use-router-redirect.js b/packages/koot/i18n/server/use-router-redirect.js index ef300f27..baf2a406 100644 --- a/packages/koot/i18n/server/use-router-redirect.js +++ b/packages/koot/i18n/server/use-router-redirect.js @@ -1,46 +1,48 @@ -import availableLocaleIds from '../locale-ids' -import getLangFromCtx from './get-lang-from-ctx' -import setCookie from '../set-cookie' -import isI18nEnabled from '../is-enabled' +import availableLocaleIds from '../locale-ids'; +import getLangFromCtx from './get-lang-from-ctx'; +import setCookie from '../set-cookie'; +import isI18nEnabled from '../is-enabled'; /** * URL 使用 router 方式时,在同构中间件流程的匹配 react 路由之前,检查是否需要跳转 * 如果需要跳转,此时发送跳转请求 - * @param {Object} ctx + * @param {Object} ctx * @returns {Boolean} 是否进行跳转 */ const useRouterRedirect = (ctx) => { - if (!isI18nEnabled()) - return false + if (!isI18nEnabled()) return false; - if (process.env.KOOT_I18N_URL_USE !== 'router') - return false + if (process.env.KOOT_I18N_URL_USE !== 'router') return false; - let pathname = ctx.path - if (pathname.substr(0, 1) === '/') pathname = pathname.substr(1) - pathname = pathname.split('/') + let pathname = ctx.path; + if (pathname.substr(0, 1) === '/') pathname = pathname.substr(1); + pathname = pathname.split('/'); if (!availableLocaleIds.includes(pathname[0])) { - const localeId = getLangFromCtx(ctx) + const localeId = getLangFromCtx(ctx); // console.log('lang', lang) // console.log('pathname', pathname) - pathname.unshift(localeId) - pathname = '/' + pathname.join('/') + pathname.unshift(localeId); + pathname = '/' + pathname.join('/'); // 生成跳转后的地址 - const newpath = ctx.originTrue - + ctx.hrefTrue + let newpath = + ctx.originTrue + + ctx.hrefTrue .replace(new RegExp(`^${ctx.originTrue}`), '') - .replace(new RegExp(`^${ctx.path}`), pathname) + .replace(new RegExp(`^${ctx.path}`), pathname); + + if (process.env.WEBPACK_BUILD_ENV === 'dev') + newpath = newpath.replace(/^https:\/\//, 'http://'); // console.log('newpath', newpath) - setCookie(localeId, ctx) - return ctx.redirect(newpath) + setCookie(localeId, ctx); + return ctx.redirect(newpath); } - return false -} + return false; +}; -export default useRouterRedirect +export default useRouterRedirect;