Skip to content

Commit

Permalink
fix #157
Browse files Browse the repository at this point in the history
  • Loading branch information
Diablohu committed Nov 27, 2019
1 parent 9552498 commit 72cb5a4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
- 现在开发环境下会默认启用 _Service Worker_
- 更新 TS 定义 ([#191](https://github.com/cmux/koot/issues/191))
- 多语言 / i18n: 如果翻译函数 (`__()`) 获得了确定的结果,函数会被自动转换成字符串 ([#187](https://github.com/cmux/koot/issues/187))
- 服务器: 优化服务器代码的文件尺寸 ([#172](https://github.com/cmux/koot/issues/172), [#186](https://github.com/cmux/koot/issues/186))
- 服务器
- 优化服务器代码的文件尺寸 ([#172](https://github.com/cmux/koot/issues/172), [#186](https://github.com/cmux/koot/issues/186))
- 如果 URL 中开头的斜线 `/` 过多,现在会自动跳转到正确的 URL ([#157](https://github.com/cmux/koot/issues/157))
- `node-sass` 现在改为 `optionalDependencies`,如果安装失败,不会影响 _Koot.js_ 的安装
- 组件 CSS 文件现在会按照 ES Module 格式输出,同时支持 ES Module 格式的文件引用

Expand Down
3 changes: 3 additions & 0 deletions packages/koot/libs/create-koa-app.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
const Koa = require('koa');
const helmet = require('koa-helmet');

const removeSlashes = require('./koa-middlewares/remove-slashes');

/**
* 创建 Koa App
* @returns {Object} app
*/
const create = () => {
const app = new Koa();
app.use(helmet());
app.use(removeSlashes);

return app;
};
Expand Down
13 changes: 13 additions & 0 deletions packages/koot/libs/koa-middlewares/remove-slashes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* KOA 中间件: 移除 URL 起始部分的多余斜线
* @param {Object} koaStaticConfig
* @return {Function}
*/
const removeSlashesMiddleware = async (ctx, next) => {
if (/^\/{2}/.test(ctx.url)) {
return ctx.redirect(ctx.origin + ctx.url.replace(/^\/{2}/, '/'));
}
await next();
};

module.exports = removeSlashesMiddleware;
18 changes: 16 additions & 2 deletions test/cases/react-base/need-in-order.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,22 @@ const doTest = async (port, dist, settings = {}) => {
expect(valueHasChanged).toBe(true);
}

// 测试: 服务器跳转多余 /
if (!isDev) {
const context = await browser.createIncognitoBrowserContext();
const page = await context.newPage();
await page.goto(origin, {
waitUntil: 'networkidle2'
});
const title = await page.evaluate(async () => {
return document.title;
});

await context.close();

expect(title).toBe('Koot Boilerplate (Simple)');
}

await puppeteerTestStyles(page);
await puppeteerTestCustomEnv(page, customEnv);
await puppeteerTestInjectScripts(page);
Expand Down Expand Up @@ -661,8 +677,6 @@ describe('测试: React 同构项目', () => {
await afterTest(dir, 'ENV: prod');
});

return;

test(`ENV: dev`, async () => {
await beforeTest(dir);

Expand Down

0 comments on commit 72cb5a4

Please sign in to comment.