mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 07:09:20 +08:00
fix: 修复开发前端代理 sse 断联问题
This commit is contained in:
@@ -1,5 +1,33 @@
|
||||
import { defineConfig } from '@vben/vite-config';
|
||||
|
||||
/** 开发环境 SSE 经 Vite 代理时禁止缓冲,否则只有首包 message+ping 后续心跳到不了浏览器 */
|
||||
function configureSseFriendlyProxy(proxyServer: {
|
||||
on: (
|
||||
event: 'proxyReq' | 'proxyRes',
|
||||
listener: (...args: any[]) => void,
|
||||
) => void;
|
||||
}) {
|
||||
proxyServer.on('proxyReq', (proxyReq, req) => {
|
||||
if (req.url?.includes('/notify/sse/')) {
|
||||
proxyReq.setHeader('Accept', 'text/event-stream');
|
||||
proxyReq.setHeader('Cache-Control', 'no-cache');
|
||||
}
|
||||
});
|
||||
proxyServer.on('proxyRes', (proxyRes, req, res) => {
|
||||
const contentType = proxyRes.headers['content-type'];
|
||||
if (
|
||||
req.url?.includes('/notify/sse/') ||
|
||||
(typeof contentType === 'string' &&
|
||||
contentType.includes('text/event-stream'))
|
||||
) {
|
||||
delete proxyRes.headers['content-encoding'];
|
||||
res.setHeader('Cache-Control', 'no-cache, no-transform');
|
||||
res.setHeader('Connection', 'keep-alive');
|
||||
res.setHeader('X-Accel-Buffering', 'no');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export default defineConfig(async () => {
|
||||
return {
|
||||
application: {
|
||||
@@ -18,6 +46,7 @@ export default defineConfig(async () => {
|
||||
// SSE 长连接:禁止代理空闲超时断开
|
||||
timeout: 0,
|
||||
proxyTimeout: 0,
|
||||
configure: configureSseFriendlyProxy,
|
||||
},
|
||||
// 静态资源代理 - 用于访问上传的图片和文件
|
||||
'/uploads': {
|
||||
|
||||
Reference in New Issue
Block a user