From 15e087327b05cf923325dc6ec74116c509529e46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=87=AF=E6=96=8C?= Date: Fri, 1 Mar 2024 15:02:58 +0800 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20=E4=BD=BF=E7=94=A8=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=8F=98=E9=87=8FUMI=5FDEV=5FSERVER=5FCOMPRESS?= =?UTF-8?q?=E6=9D=A5=E6=8E=A7=E5=88=B6dev=20server=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E5=8E=8B=E7=BC=A9=20*=20feat:=20=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8FUMI=5FDEV=5FSERVER?= =?UTF-8?q?=5FCOMPRESS=E6=9D=A5=E6=8E=A7=E5=88=B6dev=20server=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E8=BF=9B=E8=A1=8C=E5=8E=8B=E7=BC=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/dev-server-no-compress/.umirc.ts | 7 +++ .../images/nocompress.png | Bin 0 -> 4394 bytes .../images/withcompress.png | Bin 0 -> 4227 bytes examples/dev-server-no-compress/package.json | 16 ++++++ .../dev-server-no-compress/pages/index.tsx | 49 ++++++++++++++++++ examples/dev-server-no-compress/readme.md | 16 ++++++ examples/dev-server-no-compress/sse_server.js | 34 ++++++++++++ packages/bundler-webpack/src/server/server.ts | 6 ++- 8 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 examples/dev-server-no-compress/.umirc.ts create mode 100644 examples/dev-server-no-compress/images/nocompress.png create mode 100644 examples/dev-server-no-compress/images/withcompress.png create mode 100644 examples/dev-server-no-compress/package.json create mode 100644 examples/dev-server-no-compress/pages/index.tsx create mode 100644 examples/dev-server-no-compress/readme.md create mode 100644 examples/dev-server-no-compress/sse_server.js diff --git a/examples/dev-server-no-compress/.umirc.ts b/examples/dev-server-no-compress/.umirc.ts new file mode 100644 index 000000000000..bcab9af1f296 --- /dev/null +++ b/examples/dev-server-no-compress/.umirc.ts @@ -0,0 +1,7 @@ +export default { + proxy: { + '/events/': { + target: 'http://localhost:3000', + }, + }, +}; diff --git a/examples/dev-server-no-compress/images/nocompress.png b/examples/dev-server-no-compress/images/nocompress.png new file mode 100644 index 0000000000000000000000000000000000000000..27d3fbae4357f6202163a0642fc35cb7fdeb4fd3 GIT binary patch literal 4394 zcmai&XH?Tmx5h&+MwCuyf*?pyN)n|gy@&(^f?%X50tp0_Cejh42~q_V{wSe$f}sh5 zAks^uh2B(@-h>c3Ts-$(?^);84>Pl7&By0?etYkUFf!CV&CJUT0)bBJ-Ow^QIpa@O z4I}l*o~M0N=H#GqH__Dq;lJ`PoLp!z>IUi{5Fz%|feqcsoyp|}${hq^Y5KFM+MM(4 zK%lb?dRpqHUe+t`tX_!}u;^f-Is5nu*Ubvwm{i6Ed!W+xkowJ_AKYMDBl_ZO4^1VUavwg5#ScFC8ItMi9@zdRe~o^JXnhNzKrYs&(g~Z zv%_sypJ^ZY)ipMMh*k9y4qQGHE;rrKx47)pvrDj@KG)D3C{tmRnfiSBV4GrLaOlU~ zdL{#=={`_5XKG|R&oBp@{HO)JA|LG3cu7Gui8==|mrZLSJi|5<{J|*VBBwgfq>v*w zIpxJoFYg?*mB=T#FW#4>apUGyG>4+FF2^m}*D0=9y#S$LTs)xq@aO<6u-dxhFce(^ zQkSm)Te0E}P2G5G9c21-(iJuYZ>#Q~O0r5+-*(tCc{cyZQK&2n4%LyAeffM4ILxJm ziIy*8O81K1i2%Py4n1sd$%QE+JZ+T|h94*v3@`Y*hp$>69~}(<*uGXpC(9j5-w2k{ zGSkHdyRiKhmNtp3L7#bq8_eNU=&rO?EJ6j)evv1mYUjg^*{z^SjxcuLQerJ4Bz&!I zqIE{4$S!^)VY#jd1z2dHmE+MZ#XCMa<*dt|UxhAfE~Qw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t-tpOluNpe2}J-R|2(``=q8>F=n>GJjY-GALZ% z!2Fy2`*}2eD#D-C?qB*+9se@T_09V`#LxS3b?ZUX*@fi8>=PgO0JkqT46*>1P_4DL zBKZW>sO4Du+J+vXKFI((gW*R`st>jyXR)pBP=n+}{JiN$YYj~gNGki?n6q3;A%LFqo?~cy z8*H5a!2~AN{9j|_--7@53Db#7K>X%?gnNBW58t_veni@-*aD3&`XSP1>YUP{zto_9 z5Mc;sGaYR0JZ$#xg}TzjUsWI|ie=50;rO&UIi>qXr11Vvr?4s2?}z%icGpNzo4=f1 zcM=c}r<^nHm)!~0+B!}n1+*wA0D44!GPSJA{#Z(v(sd-XT_T%jJ5~+4jyV?Nlo5{- zpm!(t-g{Qj4E)C9*~CTU5l&%JdpQN~g2ZY+JikSOJmTgTMsQ^80SoVOn%Srtl|0B# zbL|k3O-_%iByb*SDZI?}5As(J4J5rJ7^UyMEKO;D)8DPeM*Z0Bw_^x-EJvqg;(psA z8GcJ#>Hd|91k|gTsB*HwE3M4+>mVPyRPRdfJ9sc#Zqd{{w#!myrMf literal 0 HcmV?d00001 diff --git a/examples/dev-server-no-compress/images/withcompress.png b/examples/dev-server-no-compress/images/withcompress.png new file mode 100644 index 0000000000000000000000000000000000000000..d04e4739bc488a6118af976f1f289ec27aa95d40 GIT binary patch literal 4227 zcmb`LXEa>h!hnZFXQYT41Q8LP(I!NTmdq$4g6Q1{B0<7L4Uxi2B0B|mWc-h0&V%nAnSOP zYX^axW7WQ`a?jghHOUn=mh8zTB>RERDH{4(yyA9ZXKMH4PapMSHj7i<>cy%!mXlpy+bV zCahhlvo+p$EWbQaKOomv`^EEpx(J;Gn{?Qi=jG*{H%b$?ZYgyK*6!(uJK{~F=%cTx zHuVdvIGQUX{OQd_h$>dv*#%B}CaKpg7x`GZx>JlqG!heI+VhZMDzxs*{HBS=Y2>gF zYulu{6v0`A9?u(2$+}naZvdm&&nIM82~Q4XAiOctL=L5s<6{syh+^b=$eD=v_QMpk z+}u^5NP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QC9pxtbe~{1)5?TMk8ORgj)LO@6NxLeI^yr z-UeKJO2HF&?y7KK5>u~MTKtfuKr>q#Z=nC=_Nn4`yG!~q}^KzKQ5 z9FQ7X645ZqD1WiNw5vbtlY${Yah1y{tC^bqCMR>CJ;p?h3P7+hDB29J`IK`v-YL3M z66}D_cj@&E61CD8kMc5a(h7eLx0+gJ=0%1Th&t-66<-ix`JJm2kRAMxwdYB3&QbUAYX|t; z=N`WWKYWHHvWlnk)6_}{uc8$k;u=+bh({*La`qx&=pXIV7LoC5lLx(zzrcD+1L)+P z#%hxOAFPc^M`Mc{{K`TdPKqe2hQIi!)$d)S{BFsU8JjEH>BND{u3D98u~+OS?n-0I zmDJ=d-QZo~T`bSIyg(I=TT!j0kHuf|Z3v6L-2l9mO%5p2qdWc9 z1OfY+MFbTl`Bgd0PfYjE4m)A67}ac%zB~W6mUl?vg#j^orqE)O<)>#bhE(kRK=*DB z3D+>ny;lTC z3{?dBsqJCS&vMqpg<5voxBerk+9+$s(5JWQL3iagCpM9^(SU-ccc&n^N?GT;MF%z5 zT!SmOD<>VQ3v+xp8#Es6C^_dyM*ZRED-UhwZj37KrzN!`vg6Cmuj)aw2205vw(4AH z_FwdhBoS%~#|}!o7FlWE^JR;%CV0S2>)rTj@!3)2di_=ab$9=r+9flX_AHwvzqm zsx9cjkgSy*b(cqCjR2_)v3Ldh_i1Y8?wpKD$|tb6Kh~Aa1$65e0>lP9TWV5vWDof8 z=mXc?|0r&&XP_AHGOiH1i0tsVh{WC(Jx%UmLE>qZ9Y=mX{)+RE7I;3k=XH8u{&%21 er`M-W$n0XSDj8<0_?-SYfM~1h+%8tNdhtKk7F^{3 literal 0 HcmV?d00001 diff --git a/examples/dev-server-no-compress/package.json b/examples/dev-server-no-compress/package.json new file mode 100644 index 000000000000..6768d5bf15f7 --- /dev/null +++ b/examples/dev-server-no-compress/package.json @@ -0,0 +1,16 @@ +{ + "name": "@example/dev-server-no-compress", + "private": true, + "scripts": { + "build": "umi build", + "dev": "umi dev", + "nocompress": "cross-env UMI_DEV_SERVER_COMPRESS=none npm run dev", + "withcompress": "cross-env npm run dev" + }, + "dependencies": { + "umi": "workspace:*" + }, + "devDependencies": { + "cross-env": "^7.0.3" + } +} diff --git a/examples/dev-server-no-compress/pages/index.tsx b/examples/dev-server-no-compress/pages/index.tsx new file mode 100644 index 000000000000..0aa2819370fc --- /dev/null +++ b/examples/dev-server-no-compress/pages/index.tsx @@ -0,0 +1,49 @@ +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([]); + 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 ( +
+ + + + + + + + + {events.map((event, index) => ( + + + + + ))} + +
事件内容接收时间
{event.data}{event.timeString}
+
+ ); +} diff --git a/examples/dev-server-no-compress/readme.md b/examples/dev-server-no-compress/readme.md new file mode 100644 index 000000000000..6d966b38bf3b --- /dev/null +++ b/examples/dev-server-no-compress/readme.md @@ -0,0 +1,16 @@ +#### 本example用来验证UMI_DEV_SERVER_COMPRESS环境变量能否正确的打开和关闭dev server的压缩功能: +``` +1. 只有在UMI_DEV_SERVER_COMPRESS=none时,dev server才不会开启压缩功能。 +2. 其他任何值。包括不设置这个环境变量,都会开启压缩功能。 +``` +#### 这个feature的意义 +``` +保证在开发环境时,使用了proxy配置项配置了sse形式的接口时,能够如预期的流式输出,而不是所有event都被阻塞到最后一起输出。 +``` +#### 本example的验证方式 +1. 安装依赖并build pnpm install && pnpm run build +2. 启动sse_server.js文件:这是用来测试效果的sse服务 +3. 运行npm run withcompress, 并访问页面。按预期,sse的事件是一次性输出的,效果如下: +![](.\images\withcompress.png) +4. 运行npm run nocompress, 并访问页面。按预期,sse的事件是流式输出的,效果如下: +![](.\images\nocompress.png) diff --git a/examples/dev-server-no-compress/sse_server.js b/examples/dev-server-no-compress/sse_server.js new file mode 100644 index 000000000000..687fe6d9740e --- /dev/null +++ b/examples/dev-server-no-compress/sse_server.js @@ -0,0 +1,34 @@ +const express = require('@umijs/bundler-utils/compiled/express'); +const 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(); + }); +}); + +const PORT = 3000; +app.listen(PORT, () => { + console.log(`SSE server running at http://localhost:${PORT}`); +}); diff --git a/packages/bundler-webpack/src/server/server.ts b/packages/bundler-webpack/src/server/server.ts index 2553b66e7ded..af312b21e45c 100644 --- a/packages/bundler-webpack/src/server/server.ts +++ b/packages/bundler-webpack/src/server/server.ts @@ -44,8 +44,10 @@ export async function createServer(opts: IOpts): Promise { }), ); - // compression - app.use(require('@umijs/bundler-webpack/compiled/compression')()); + // enable compression if UMI_DEV_SERVER_COMPRESS is not none + 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) => { From 40fb5ffe2e53a2e603a7e70000976382c09bf8b9 Mon Sep 17 00:00:00 2001 From: fz6m <59400654+fz6m@users.noreply.github.com> Date: Sun, 3 Mar 2024 00:13:22 +0900 Subject: [PATCH 2/5] example: update --- examples/dev-server-no-compress/.umirc.ts | 7 ---- .../images/nocompress.png | Bin 4394 -> 0 bytes .../images/withcompress.png | Bin 4227 -> 0 bytes examples/dev-server-no-compress/readme.md | 16 --------- examples/dev-server-no-compress/sse_server.js | 34 ------------------ examples/with-no-compress-for-sse/.umirc.ts | 1 + .../package.json | 5 ++- .../pages/index.tsx | 6 ++-- examples/with-no-compress-for-sse/plugin.ts | 8 +++++ examples/with-no-compress-for-sse/readme.md | 9 +++++ .../sse-middleware.ts | 29 +++++++++++++++ pnpm-lock.yaml | 17 +++++---- 12 files changed, 63 insertions(+), 69 deletions(-) delete mode 100644 examples/dev-server-no-compress/.umirc.ts delete mode 100644 examples/dev-server-no-compress/images/nocompress.png delete mode 100644 examples/dev-server-no-compress/images/withcompress.png delete mode 100644 examples/dev-server-no-compress/readme.md delete mode 100644 examples/dev-server-no-compress/sse_server.js create mode 100644 examples/with-no-compress-for-sse/.umirc.ts rename examples/{dev-server-no-compress => with-no-compress-for-sse}/package.json (54%) rename examples/{dev-server-no-compress => with-no-compress-for-sse}/pages/index.tsx (96%) create mode 100644 examples/with-no-compress-for-sse/plugin.ts create mode 100644 examples/with-no-compress-for-sse/readme.md create mode 100644 examples/with-no-compress-for-sse/sse-middleware.ts diff --git a/examples/dev-server-no-compress/.umirc.ts b/examples/dev-server-no-compress/.umirc.ts deleted file mode 100644 index bcab9af1f296..000000000000 --- a/examples/dev-server-no-compress/.umirc.ts +++ /dev/null @@ -1,7 +0,0 @@ -export default { - proxy: { - '/events/': { - target: 'http://localhost:3000', - }, - }, -}; diff --git a/examples/dev-server-no-compress/images/nocompress.png b/examples/dev-server-no-compress/images/nocompress.png deleted file mode 100644 index 27d3fbae4357f6202163a0642fc35cb7fdeb4fd3..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4394 zcmai&XH?Tmx5h&+MwCuyf*?pyN)n|gy@&(^f?%X50tp0_Cejh42~q_V{wSe$f}sh5 zAks^uh2B(@-h>c3Ts-$(?^);84>Pl7&By0?etYkUFf!CV&CJUT0)bBJ-Ow^QIpa@O z4I}l*o~M0N=H#GqH__Dq;lJ`PoLp!z>IUi{5Fz%|feqcsoyp|}${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rJ7^UyMEKO;D)8DPeM*Z0Bw_^x-EJvqg;(psA z8GcJ#>Hd|91k|gTsB*HwE3M4+>mVPyRPRdfJ9sc#Zqd{{w#!myrMf diff --git a/examples/dev-server-no-compress/images/withcompress.png b/examples/dev-server-no-compress/images/withcompress.png deleted file mode 100644 index d04e4739bc488a6118af976f1f289ec27aa95d40..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4227 zcmb`LXEa>h!hnZFXQYT41Q8LP(I!NTmdq$4g6Q1{B0<7L4Uxi2B0B|mWc-h0&V%nAnSOP zYX^axW7WQ`a?jghHOUn=mh8zTB>RERDH{4(yyA9ZXKMH4PapMSHj7i<>cy%!mXlpy+bV zCahhlvo+p$EWbQaKOomv`^EEpx(J;Gn{?Qi=jG*{H%b$?ZYgyK*6!(uJK{~F=%cTx zHuVdvIGQUX{OQd_h$>dv*#%B}CaKpg7x`GZx>JlqG!heI+VhZMDzxs*{HBS=Y2>gF zYulu{6v0`A9?u(2$+}naZvdm&&nIM82~Q4XAiOctL=L5s<6{syh+^b=$eD=v_QMpk z+}u^5NPfSc$e7YxK;K4fuN#*qv?1=5)M91>pB&?n+pp0<=+-X@l8`W9bW&w(W*+Pd zUX*38&7sqF4ZliJf`u+FM9<2_5)_0FNSlCWtBb*;gxp({ue3O)Q56~+(VIWRbek*7 z>@&YfXtV%L&RtNfsw&#djA3&tTAxedBuOVP;^ZU1ufVOneb&nTiXf-FJ(lKEJ~kyH z%aCFbn2)#Tz%|VZESEVU)PnsdMmB9=`!N@F(isJbsjhJIzo_)0SEjF3vw`}{s~Yur zX;?y8)#L7lzXlX<{N(TdoPo5{g}~ASUT&C%QE63-Z%nf3ZpVVu*5SX zYewR!-u@Y;Eyz2Ve3BF$LQVsIRMyH?N{P6hZCxOKwwG~;dJA~tX6fmESxWXY!>3|i zJi0P|CM%Y(>q8=DBFdK$ko(_=#7mi1FDogRP2y>T?k&X`9R@_xZia9V7*XZ{+2fLC z8ep`Ic7MrBCi(8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O<)>#bhE(kRK=*DB z3D+>ny;lTC z3{?dBsqJCS&vMqpg<5voxBerk+9+$s(5JWQL3iagCpM9^(SU-ccc&n^N?GT;MF%z5 zT!SmOD<>VQ3v+xp8#Es6C^_dyM*ZRED-UhwZj37KrzN!`vg6Cmuj)aw2205vw(4AH z_FwdhBoS%~#|}!o7FlWE^JR;%CV0S2>)rTj@!3)2di_=ab$9=r+9flX_AHwvzqm zsx9cjkgSy*b(cqCjR2_)v3Ldh_i1Y8?wpKD$|tb6Kh~Aa1$65e0>lP9TWV5vWDof8 z=mXc?|0r&&XP_AHGOiH1i0tsVh{WC(Jx%UmLE>qZ9Y=mX{)+RE7I;3k=XH8u{&%21 er`M-W$n0XSDj8<0_?-SYfM~1h+%8tNdhtKk7F^{3 diff --git a/examples/dev-server-no-compress/readme.md b/examples/dev-server-no-compress/readme.md deleted file mode 100644 index 6d966b38bf3b..000000000000 --- a/examples/dev-server-no-compress/readme.md +++ /dev/null @@ -1,16 +0,0 @@ -#### 本example用来验证UMI_DEV_SERVER_COMPRESS环境变量能否正确的打开和关闭dev server的压缩功能: -``` -1. 只有在UMI_DEV_SERVER_COMPRESS=none时,dev server才不会开启压缩功能。 -2. 其他任何值。包括不设置这个环境变量,都会开启压缩功能。 -``` -#### 这个feature的意义 -``` -保证在开发环境时,使用了proxy配置项配置了sse形式的接口时,能够如预期的流式输出,而不是所有event都被阻塞到最后一起输出。 -``` -#### 本example的验证方式 -1. 安装依赖并build pnpm install && pnpm run build -2. 启动sse_server.js文件:这是用来测试效果的sse服务 -3. 运行npm run withcompress, 并访问页面。按预期,sse的事件是一次性输出的,效果如下: -![](.\images\withcompress.png) -4. 运行npm run nocompress, 并访问页面。按预期,sse的事件是流式输出的,效果如下: -![](.\images\nocompress.png) diff --git a/examples/dev-server-no-compress/sse_server.js b/examples/dev-server-no-compress/sse_server.js deleted file mode 100644 index 687fe6d9740e..000000000000 --- a/examples/dev-server-no-compress/sse_server.js +++ /dev/null @@ -1,34 +0,0 @@ -const express = require('@umijs/bundler-utils/compiled/express'); -const 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(); - }); -}); - -const PORT = 3000; -app.listen(PORT, () => { - console.log(`SSE server running at http://localhost:${PORT}`); -}); diff --git a/examples/with-no-compress-for-sse/.umirc.ts b/examples/with-no-compress-for-sse/.umirc.ts new file mode 100644 index 000000000000..ff8b4c56321a --- /dev/null +++ b/examples/with-no-compress-for-sse/.umirc.ts @@ -0,0 +1 @@ +export default {}; diff --git a/examples/dev-server-no-compress/package.json b/examples/with-no-compress-for-sse/package.json similarity index 54% rename from examples/dev-server-no-compress/package.json rename to examples/with-no-compress-for-sse/package.json index 6768d5bf15f7..949318ff8d8f 100644 --- a/examples/dev-server-no-compress/package.json +++ b/examples/with-no-compress-for-sse/package.json @@ -1,11 +1,10 @@ { - "name": "@example/dev-server-no-compress", + "name": "@example/with-no-compress-for-sse", "private": true, "scripts": { "build": "umi build", "dev": "umi dev", - "nocompress": "cross-env UMI_DEV_SERVER_COMPRESS=none npm run dev", - "withcompress": "cross-env npm run dev" + "dev:nocompress": "cross-env UMI_DEV_SERVER_COMPRESS=none npm run dev" }, "dependencies": { "umi": "workspace:*" diff --git a/examples/dev-server-no-compress/pages/index.tsx b/examples/with-no-compress-for-sse/pages/index.tsx similarity index 96% rename from examples/dev-server-no-compress/pages/index.tsx rename to examples/with-no-compress-for-sse/pages/index.tsx index 0aa2819370fc..2368f88f4c76 100644 --- a/examples/dev-server-no-compress/pages/index.tsx +++ b/examples/with-no-compress-for-sse/pages/index.tsx @@ -12,6 +12,7 @@ class Event { export default function HomePage() { const [events, setEvents] = useState([]); + useEffect(() => { console.log('开始请求'); const eventSource = new EventSource('/events/number'); @@ -26,8 +27,9 @@ export default function HomePage() { eventSource.close(); }; }, []); + return ( -
+ <> @@ -44,6 +46,6 @@ export default function HomePage() { ))}
-
+ ); } diff --git a/examples/with-no-compress-for-sse/plugin.ts b/examples/with-no-compress-for-sse/plugin.ts new file mode 100644 index 000000000000..e3d4901cdbbb --- /dev/null +++ b/examples/with-no-compress-for-sse/plugin.ts @@ -0,0 +1,8 @@ +import { IApi } from 'umi'; +import { sseMiddleware } from './sse-middleware'; + +export default (api: IApi) => { + api.onBeforeMiddleware(({ app }) => { + sseMiddleware(app); + }); +}; diff --git a/examples/with-no-compress-for-sse/readme.md b/examples/with-no-compress-for-sse/readme.md new file mode 100644 index 000000000000..15e5513baac3 --- /dev/null +++ b/examples/with-no-compress-for-sse/readme.md @@ -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 在本地开发时正常运作。 diff --git a/examples/with-no-compress-for-sse/sse-middleware.ts b/examples/with-no-compress-for-sse/sse-middleware.ts new file mode 100644 index 000000000000..bddbfcb10b11 --- /dev/null +++ b/examples/with-no-compress-for-sse/sse-middleware.ts @@ -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(); + }); + }); +}; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f6181225ce7d..5984c1a00775 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1383,6 +1383,16 @@ importers: specifier: ^18.0.10 version: 18.0.10 + examples/with-no-compress-for-sse: + dependencies: + umi: + specifier: workspace:* + version: link:../../packages/umi + devDependencies: + cross-env: + specifier: ^7.0.3 + version: 7.0.3 + examples/with-react-17: dependencies: react: @@ -19707,9 +19717,6 @@ packages: /ajv-formats@2.1.1: resolution: {integrity: sha512-Wx0Kx52hxE7C18hkMEggYlEifqWZtYaRgouJor+WMdPnQyEK13vgEWyVNup7SoeeoLMsr4kf5h6dOW11I15MUA==} - peerDependenciesMeta: - ajv: - optional: true dependencies: ajv: 8.11.0 @@ -23381,7 +23388,6 @@ packages: loose-envify: 1.4.0 object-assign: 4.1.1 dev: false - bundledDependencies: false /create-require@1.1.1: resolution: {integrity: sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==} @@ -45941,9 +45947,6 @@ packages: resolution: {integrity: sha512-AhwtHCKMtR71JgeYDaswmZXhPcW9iuI9Sp2LvZPo9upDZ7231ZJ7eA9RaURbhpXGVlrjX4cFNlB4ieTetEb7hQ==} engines: {node: '>=12.13.0'} hasBin: true - peerDependenciesMeta: - postcss: - optional: true dependencies: arg: 5.0.2 chokidar: 3.5.3 From a025760f955f60b991ab5bae6d9f9265b998a97d Mon Sep 17 00:00:00 2001 From: fz6m <59400654+fz6m@users.noreply.github.com> Date: Sun, 3 Mar 2024 00:18:28 +0900 Subject: [PATCH 3/5] docs: `UMI_DEV_SERVER_COMPRESS` --- docs/docs/docs/guides/env-variables.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/docs/docs/guides/env-variables.md b/docs/docs/docs/guides/env-variables.md index 821142a2d486..162f18f97e67 100644 --- a/docs/docs/docs/guides/env-variables.md +++ b/docs/docs/docs/guides/env-variables.md @@ -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 日志。 From 5acd90277ec607de8a3fc8a2733892a86ca7a8b6 Mon Sep 17 00:00:00 2001 From: fz6m <59400654+fz6m@users.noreply.github.com> Date: Sun, 3 Mar 2024 00:20:45 +0900 Subject: [PATCH 4/5] example: update --- examples/with-no-compress-for-sse/pages/index.tsx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/with-no-compress-for-sse/pages/index.tsx b/examples/with-no-compress-for-sse/pages/index.tsx index 2368f88f4c76..2ef38e04f2ef 100644 --- a/examples/with-no-compress-for-sse/pages/index.tsx +++ b/examples/with-no-compress-for-sse/pages/index.tsx @@ -29,7 +29,8 @@ export default function HomePage() { }, []); return ( - <> +
+

{`演示:当默认存在 compress 时,数据无法流式获取。`}

@@ -46,6 +47,6 @@ export default function HomePage() { ))}
- +
); } From 955ebebbed6387cbf7213de69145ec5d1e1f11b6 Mon Sep 17 00:00:00 2001 From: fz6m <59400654+fz6m@users.noreply.github.com> Date: Sun, 3 Mar 2024 00:22:14 +0900 Subject: [PATCH 5/5] docs: comment --- packages/bundler-webpack/src/server/server.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/bundler-webpack/src/server/server.ts b/packages/bundler-webpack/src/server/server.ts index af312b21e45c..b6296e867bae 100644 --- a/packages/bundler-webpack/src/server/server.ts +++ b/packages/bundler-webpack/src/server/server.ts @@ -44,7 +44,7 @@ export async function createServer(opts: IOpts): Promise { }), ); - // enable compression if UMI_DEV_SERVER_COMPRESS is not none + // See https://github.com/umijs/umi/issues/12144 if (process.env.UMI_DEV_SERVER_COMPRESS !== 'none') { app.use(require('@umijs/bundler-webpack/compiled/compression')()); }