Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 使用环境变量UMI_DEV_SERVER_COMPRESS来控制dev server是否进行压缩 #12166

Merged
merged 5 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/docs/docs/guides/env-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ $ UMI_PLUGINS=./path/to/plugin1,./path/to/plugin2 umi dev
$ UMI_PRESETS=./path/to/preset1,./path/to/preset2 umi dev
```

### UMI_DEV_SERVER_COMPRESS

默认 Umi 开发服务器自带 [compress](https://github.com/expressjs/compression) 压缩中间件,这会使开发时 SSE 数据的传输 [无法流式获取](https://github.com/umijs/umi/issues/12144) ,通过指定 `UMI_DEV_SERVER_COMPRESS=none` 来关闭 compress 压缩功能:

```bash
UMI_DEV_SERVER_COMPRESS=none umi dev
```

### WEBPACK_FS_CACHE_DEBUG

开启 webpack 的物理缓存 debug 日志。
Expand Down
1 change: 1 addition & 0 deletions examples/with-no-compress-for-sse/.umirc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {};
15 changes: 15 additions & 0 deletions examples/with-no-compress-for-sse/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@example/with-no-compress-for-sse",
"private": true,
"scripts": {
"build": "umi build",
"dev": "umi dev",
"dev:nocompress": "cross-env UMI_DEV_SERVER_COMPRESS=none npm run dev"
},
"dependencies": {
"umi": "workspace:*"
},
"devDependencies": {
"cross-env": "^7.0.3"
}
}
52 changes: 52 additions & 0 deletions examples/with-no-compress-for-sse/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React, { useEffect, useState } from 'react';

class Event {
data: string;
timeString: string;

constructor(data: string) {
this.data = data;
this.timeString = new Date().toLocaleTimeString();
}
}

export default function HomePage() {
const [events, setEvents] = useState<Event[]>([]);

useEffect(() => {
console.log('开始请求');
const eventSource = new EventSource('/events/number');
let startEvent = new Event('开始请求');
setEvents((prev) => [...prev, startEvent]);
eventSource.onmessage = function (e: any) {
let item = new Event(e.data);
setEvents((prev) => [...prev, item]);
};
eventSource.onerror = (e) => {
console.log('EventSource failed:', e);
eventSource.close();
};
}, []);

return (
<div>
<h3>{`演示:当默认存在 compress 时,数据无法流式获取。`}</h3>
<table>
<thead>
<tr>
<th>事件内容</th>
<th>接收时间</th>
</tr>
</thead>
<tbody>
{events.map((event, index) => (
<tr key={index}>
<td>{event.data}</td>
<td>{event.timeString}</td>
</tr>
))}
</tbody>
</table>
</div>
);
}
8 changes: 8 additions & 0 deletions examples/with-no-compress-for-sse/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { IApi } from 'umi';
import { sseMiddleware } from './sse-middleware';

export default (api: IApi) => {
api.onBeforeMiddleware(({ app }) => {
sseMiddleware(app);
});
};
9 changes: 9 additions & 0 deletions examples/with-no-compress-for-sse/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# with-no-compress-for-sse

### 背景

[来源](https://github.com/umijs/umi/issues/12144),在开发环境下由于 umi dev server 内置了 `compress` 中间件,导致 SSE 流在开发时传递不符合预期。

### 解决

本示例演示了此问题,并通过启动时附加 `UMI_DEV_SERVER_COMPRESS=none` 来关闭 `compress` 中间件,使 SSE 在本地开发时正常运作。
29 changes: 29 additions & 0 deletions examples/with-no-compress-for-sse/sse-middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { Express } from '@umijs/bundler-utils/compiled/express';

export const sseMiddleware = (app: Express) => {
app.get('/events/number', (req, res) => {
console.log('new connection');
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
Connection: 'keep-alive',
});

let counter = 1;
const intervalId = setInterval(() => {
if (counter === 5) {
clearInterval(intervalId);
res.end(`data: 事件${counter}\n\n`);
return;
}
res.write(`data: 事件${counter}\n\n`);

counter++;
}, 1000);

req.on('close', () => {
clearInterval(intervalId);
res.end();
});
});
};
6 changes: 4 additions & 2 deletions packages/bundler-webpack/src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ export async function createServer(opts: IOpts): Promise<any> {
}),
);

// compression
app.use(require('@umijs/bundler-webpack/compiled/compression')());
// See https://github.com/umijs/umi/issues/12144
if (process.env.UMI_DEV_SERVER_COMPRESS !== 'none') {
app.use(require('@umijs/bundler-webpack/compiled/compression')());
}

// debug all js file
app.use((req, res, next) => {
Expand Down
17 changes: 10 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading