Skip to content

Commit

Permalink
fix #232
Browse files Browse the repository at this point in the history
  • Loading branch information
Diablohu committed Apr 1, 2020
1 parent de28a89 commit 4b32f4b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 58 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
76 changes: 41 additions & 35 deletions packages/koot/i18n/server/generate-html-redirect-metas.js
Original file line number Diff line number Diff line change
@@ -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 `<link rel="alternate" hreflang="${l}" href="${thisHref}" />`
if (__DEV__)
thisHref = thisHref
.replace('://localhost', '://127.0.0.1')
.replace(/^https:\/\//, 'http://');
return `<link rel="alternate" hreflang="${l}" href="${thisHref}" />`;
})
.join('')
.join('');

if (isUseRouter) {
html += `<base href="/${localeId}">`
html += `<base href="/${localeId}">`;
}

return html
}
return html;
};

export default generateHtmlRedirectMetas
export default generateHtmlRedirectMetas;
48 changes: 25 additions & 23 deletions packages/koot/i18n/server/use-router-redirect.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 4b32f4b

Please sign in to comment.