Files
kurihada fc5a644726 fix(web): 修复 WebSocket 切换 session 时的错误
- 添加 isClosingRef 标记主动关闭,避免触发错误回调
- 在 effect 开始时重置 isClosingRef 支持 StrictMode
- 增强 Vite 代理错误处理,静默 EPIPE/ECONNRESET 错误
2025-12-12 15:29:46 +08:00

102 lines
2.8 KiB
TypeScript

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { VitePWA } from 'vite-plugin-pwa';
import path from 'path';
export default defineConfig({
plugins: [
react(),
VitePWA({
registerType: 'autoUpdate',
includeAssets: ['icon.svg'],
manifest: {
name: 'AI Terminal Assistant',
short_name: 'AI Assistant',
description: 'Your intelligent coding companion',
theme_color: '#111827',
background_color: '#111827',
display: 'standalone',
orientation: 'portrait-primary',
scope: '/',
start_url: '/',
icons: [
{
src: '/icon.svg',
sizes: 'any',
type: 'image/svg+xml',
purpose: 'any maskable',
},
],
},
workbox: {
globPatterns: ['**/*.{js,css,html,ico,png,svg,woff2}'],
runtimeCaching: [
{
urlPattern: /\/api\//i,
handler: 'NetworkFirst',
options: {
cacheName: 'api-cache',
expiration: {
maxEntries: 50,
maxAgeSeconds: 60 * 60, // 1 hour
},
},
},
],
},
devOptions: {
enabled: false, // 开发模式下禁用 PWA
},
}),
],
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
},
server: {
port: 5173,
proxy: {
'/api': {
target: 'http://localhost:3000',
changeOrigin: true,
ws: true,
// 忽略 WebSocket 代理错误(切换 session 时正常发生)
configure: (proxy) => {
// 静默忽略所有代理错误
proxy.on('error', (err, _req, res) => {
const msg = err.message || '';
// EPIPE/ECONNRESET 是正常的连接关闭错误
if (msg.includes('EPIPE') || msg.includes('ECONNRESET') || msg.includes('ECONNREFUSED')) {
if (res && 'writeHead' in res && !res.headersSent) {
res.writeHead(502);
res.end();
}
return;
}
console.error('[proxy error]', msg);
});
// WebSocket 请求阶段错误
proxy.on('proxyReqWs', (_proxyReq, _req, socket) => {
socket.on('error', () => {});
});
// WebSocket 响应阶段错误
proxy.on('open', (proxySocket) => {
proxySocket.on('error', () => {});
});
// WebSocket 关闭
proxy.on('close', (_res, socket) => {
if (socket && !socket.destroyed) {
socket.destroy();
}
});
},
},
'/health': {
target: 'http://localhost:3000',
changeOrigin: true,
},
},
},
});