-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge commit 'c952494d991919dcc9963f3c6335d23553b92a89' into support/…
…en-docs
- Loading branch information
Showing
61 changed files
with
1,779 additions
and
359 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 在本地开发时正常运作。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"version": "4.1.1", | ||
"version": "4.1.4", | ||
"workspaces": ["packages/*"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.