mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 06:29:20 +08:00
62 lines
1.8 KiB
TypeScript
62 lines
1.8 KiB
TypeScript
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: {
|
|
// 设置API URL
|
|
apiURL: '/api',
|
|
printInfoMap: {},
|
|
},
|
|
vite: {
|
|
server: {
|
|
proxy: {
|
|
'/api': {
|
|
changeOrigin: true,
|
|
// 后端服务地址
|
|
target: 'http://localhost:8080',
|
|
ws: true,
|
|
// SSE 长连接:禁止代理空闲超时断开
|
|
timeout: 0,
|
|
proxyTimeout: 0,
|
|
configure: configureSseFriendlyProxy,
|
|
},
|
|
// 静态资源代理 - 用于访问上传的图片和文件
|
|
'/uploads': {
|
|
changeOrigin: true,
|
|
target: 'http://localhost:8080',
|
|
ws: false,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
};
|
|
});
|