2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-12-23 02:28:55 +00:00

Fix #316: Add --clear-cache flag to cleanup session on start/exit ("incognito" mode) (PR #747)

- Add a new `clearCache` option and `--clear-cache` parameter
  to trigger session cleanups upon window launch and close

- Covers the feature request from issue #316 

- Use case example: Forcing authentification / login between sessions without limiting cache size
This commit is contained in:
Adrian DC 2019-02-08 16:03:29 +01:00 committed by Ronan Jouchet
parent 85aacaaa52
commit 5b6cc89f22
5 changed files with 36 additions and 6 deletions

View File

@ -64,6 +64,17 @@ function maybeInjectCss(browserWindow) {
}); });
} }
function clearCache(browserWindow, targetUrl = null) {
const { session } = browserWindow.webContents;
session.clearStorageData(() => {
session.clearCache(() => {
if (targetUrl) {
browserWindow.loadURL(targetUrl);
}
});
});
}
/** /**
* *
* @param {{}} inpOptions AppArgs from nativefier.json * @param {{}} inpOptions AppArgs from nativefier.json
@ -178,12 +189,7 @@ function createMainWindow(inpOptions, onAppQuit, setDockBadge) {
if (response !== 0) { if (response !== 0) {
return; return;
} }
const { session } = mainWindow.webContents; clearCache(mainWindow, options.targetUrl);
session.clearStorageData(() => {
session.clearCache(() => {
mainWindow.loadURL(options.targetUrl);
});
});
}, },
); );
}; };
@ -334,6 +340,10 @@ function createMainWindow(inpOptions, onAppQuit, setDockBadge) {
mainWindow.webContents.on('new-window', onNewWindow); mainWindow.webContents.on('new-window', onNewWindow);
mainWindow.webContents.on('will-navigate', onWillNavigate); mainWindow.webContents.on('will-navigate', onWillNavigate);
if (options.clearCache) {
clearCache(mainWindow);
}
mainWindow.loadURL(options.targetUrl); mainWindow.loadURL(options.targetUrl);
mainWindow.on('new-tab', () => createNewTab(options.targetUrl, true)); mainWindow.on('new-tab', () => createNewTab(options.targetUrl, true));
@ -350,6 +360,10 @@ function createMainWindow(inpOptions, onAppQuit, setDockBadge) {
); );
} }
maybeHideWindow(mainWindow, event, options.fastQuit, options.tray); maybeHideWindow(mainWindow, event, options.fastQuit, options.tray);
if (options.clearCache) {
clearCache(mainWindow);
}
}); });
return mainWindow; return mainWindow;

View File

@ -57,6 +57,7 @@
- [[crash-reporter]](#crash-reporter) - [[crash-reporter]](#crash-reporter)
- [[zoom]](#zoom) - [[zoom]](#zoom)
- [[single-instance]](#single-instance) - [[single-instance]](#single-instance)
- [[clear-cache]](#clear-cache)
- [[tray]](#tray) - [[tray]](#tray)
- [[basic-auth-username]](#basic-auth-username) - [[basic-auth-username]](#basic-auth-username)
- [[processEnvs]](#processenvs) - [[processEnvs]](#processenvs)
@ -523,6 +524,14 @@ Sets a default zoom factor to be used when the app is opened, defaults to `1.0`.
Prevents application from being run multiple times. If such an attempt occurs the already running instance is brought to front. Prevents application from being run multiple times. If such an attempt occurs the already running instance is brought to front.
#### [clear-cache]
```
--clear-cache
```
Prevents the application from preserving cache between launches.
#### [tray] #### [tray]
``` ```
@ -725,6 +734,7 @@ var options = {
honest: false, honest: false,
zoom: 1.0, zoom: 1.0,
singleInstance: false, singleInstance: false,
clearCache: false,
fileDownloadOptions: { fileDownloadOptions: {
saveAs: true // always show "Save As" dialog saveAs: true // always show "Save As" dialog
}, },

View File

@ -44,6 +44,7 @@ function selectAppArgs(options) {
internalUrls: options.internalUrls, internalUrls: options.internalUrls,
crashReporter: options.crashReporter, crashReporter: options.crashReporter,
singleInstance: options.singleInstance, singleInstance: options.singleInstance,
clearCache: options.clearCache,
appCopyright: options.appCopyright, appCopyright: options.appCopyright,
appVersion: options.appVersion, appVersion: options.appVersion,
buildVersion: options.buildVersion, buildVersion: options.buildVersion,

View File

@ -208,6 +208,10 @@ if (require.main === module) {
'--single-instance', '--single-instance',
'allow only a single instance of the application', 'allow only a single instance of the application',
) )
.option(
'--clear-cache',
'prevent the application from preserving cache between launches',
)
.option( .option(
'--processEnvs <json-string>', '--processEnvs <json-string>',
'a JSON string of key/value pairs to be set as environment variables before any browser windows are opened.', 'a JSON string of key/value pairs to be set as environment variables before any browser windows are opened.',

View File

@ -59,6 +59,7 @@ export default function(inpOptions) {
zoom: inpOptions.zoom || 1.0, zoom: inpOptions.zoom || 1.0,
internalUrls: inpOptions.internalUrls || null, internalUrls: inpOptions.internalUrls || null,
singleInstance: inpOptions.singleInstance || false, singleInstance: inpOptions.singleInstance || false,
clearCache: inpOptions.clearCache || false,
appVersion: inpOptions.appVersion, appVersion: inpOptions.appVersion,
buildVersion: inpOptions.buildVersion, buildVersion: inpOptions.buildVersion,
appCopyright: inpOptions.appCopyright, appCopyright: inpOptions.appCopyright,