使用 electron-packager 打包到 .exe 后如何禁用 electron 应用程序的 http 缓存
How to disable http cache for electron app after packaging to .exe using electron-packager
我有一个电子应用程序,它从 spring 启动服务器加载一些 css。
当我 运行 app from npm from sources 时,我可以 运行 as
ng build && electron . --disable-http-cache
它在没有缓存的情况下也能工作。
如果我使用 electron-packager 将我的应用程序构建到 app.exe,我该如何禁用缓存。
使用 --disable-http-cache 启动 .exe 文件不起作用
更新
唯一可行的方法是在应用程序加载页面之前从主进程中清除缓存。但是还有其他方法可以禁用缓存吗?
另一种可能性是在 app
对象的主进程中使用 Electron 的 commandLine.appendSwitch ()
,就在执行任何操作之前:
const { app } = require ("electron");
app.commandLine.appendSwitch ("disable-http-cache");
// any other main process code
这会将 --disable-http-cache
附加到 Chromium 的命令行,就像将其附加到 electron
命令一样。在代码中使用此开关时,您不再需要 运行 您的应用附加此开关,因为它会自动添加。
您还可以在请求 css 的路由 loadURL()
内添加 no-cache
header( 甚至是根路由)
webContents.loadURL(url, {"extraHeaders" : "pragma: no-cache\n"})
一个完整的迷你example gist
我有一个电子应用程序,它从 spring 启动服务器加载一些 css。 当我 运行 app from npm from sources 时,我可以 运行 as
ng build && electron . --disable-http-cache
它在没有缓存的情况下也能工作。 如果我使用 electron-packager 将我的应用程序构建到 app.exe,我该如何禁用缓存。 使用 --disable-http-cache 启动 .exe 文件不起作用
更新 唯一可行的方法是在应用程序加载页面之前从主进程中清除缓存。但是还有其他方法可以禁用缓存吗?
另一种可能性是在 app
对象的主进程中使用 Electron 的 commandLine.appendSwitch ()
,就在执行任何操作之前:
const { app } = require ("electron");
app.commandLine.appendSwitch ("disable-http-cache");
// any other main process code
这会将 --disable-http-cache
附加到 Chromium 的命令行,就像将其附加到 electron
命令一样。在代码中使用此开关时,您不再需要 运行 您的应用附加此开关,因为它会自动添加。
您还可以在请求 css 的路由 loadURL()
内添加 no-cache
header( 甚至是根路由)
webContents.loadURL(url, {"extraHeaders" : "pragma: no-cache\n"})
一个完整的迷你example gist