mirror of
https://github.com/kuaifan/dootask.git
synced 2025-12-13 20:12:48 +00:00
perf: 优化客户端缓存
This commit is contained in:
parent
40f5ba5004
commit
124b63f325
25
electron/electron.js
vendored
25
electron/electron.js
vendored
@ -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
18
electron/utils.js
vendored
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user