perf: 优化客户端缓存

This commit is contained in:
kuaifan 2025-06-29 21:58:43 +08:00
parent 40f5ba5004
commit 124b63f325
2 changed files with 40 additions and 3 deletions

25
electron/electron.js vendored
View File

@ -96,10 +96,18 @@ async function startWebServer() {
return Promise.resolve();
}
// 每次启动前清理缓存
utils.clearServerCache();
return new Promise((resolve, reject) => {
// 创建Express应用
const app = express();
// 健康检查
app.head('/health', (req, res) => {
res.status(200).send('OK');
});
// 使用express.static中间件提供静态文件服务
// Express内置了全面的MIME类型支持无需手动配置
app.use(express.static(serverPublicDir, {
@ -113,9 +121,12 @@ async function startWebServer() {
dotfiles: 'ignore',
// 自定义头部
setHeaders: (res, path, stat) => {
// 对HTML文件禁用缓存方便开发调试
if (path.endsWith('.html')) {
res.set('Cache-Control', 'no-cache');
const ext = path.split('.').pop().toLowerCase();
// HTML、JS、CSS文件禁用缓存方便开发调试
if (['html', 'js', 'css'].includes(ext)){
res.set('Cache-Control', 'no-cache, no-store, must-revalidate');
res.set('Pragma', 'no-cache');
res.set('Expires', '0');
}
}
}));
@ -1483,6 +1494,14 @@ ipcMain.handle('getStore', (event, args) => {
return store.get(args)
});
/**
* 清理服务器缓存
*/
ipcMain.on('clearServerCache', (event) => {
utils.clearServerCache();
event.returnValue = "ok";
});
//================================================================
// Update
//================================================================

18
electron/utils.js vendored
View File

@ -682,6 +682,24 @@ const utils = {
} else {
return "#FFFFFF";
}
},
/**
* 清理服务器缓存
*/
clearServerCache() {
try {
// 清理require缓存中的express相关模块
Object.keys(require.cache).forEach(key => {
if (key.includes('express') || key.includes('static')) {
delete require.cache[key];
}
});
console.log('Server cache cleared');
} catch (e) {
console.error('Failed to clear server cache:', e);
}
}
}