Skip to content

Instantly share code, notes, and snippets.

@darsain
Created June 28, 2020 11:07
Show Gist options
  • Save darsain/aa794ac2dc3662fee5d800da420a4813 to your computer and use it in GitHub Desktop.
Save darsain/aa794ac2dc3662fee5d800da420a4813 to your computer and use it in GitHub Desktop.
electron window reload hang bug reproduction
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "testProcess.js",
"scripts": {
"start": "electron ."
},
"author": "",
"license": "ISC",
"devDependencies": {
"electron": "^9.0.5"
}
}
const electron = require('electron');
let mainWindow;
function createWindow() {
mainWindow = new electron.BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true,
enableRemoteModule: false,
},
});
mainWindow.loadFile('testWindow.html');
mainWindow.once('ready-to-show', () => {
if (mainWindow == null) return;
mainWindow.show();
mainWindow.webContents.openDevTools();
});
mainWindow.on('closed', () => {
mainWindow = null;
});
}
// Window creation and closing
electron.app.on('ready', createWindow);
electron.app.on('window-all-closed', () => {
if (process.platform !== 'darwin') electron.app.quit();
});
electron.app.on('activate', () => {
if (mainWindow === null) createWindow();
});
// IPC requests
electron.ipcMain.handle('toggle-devtools', () => mainWindow?.webContents.toggleDevTools());
electron.ipcMain.handle('reload-window', () => mainWindow?.webContents.reloadIgnoringCache());
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Security-Policy" content="default-src * 'unsafe-inline'">
</head>
<body>
...loading...
<script src="testWindow.js"></script>
</body>
</html>
const {ipcRenderer} = require('electron');
const FS = require('fs');
var {promisify} = require('util');
const readdir = promisify(FS.readdir);
// Global shortcuts
window.addEventListener('keydown', (event) => {
switch (event.key) {
// Developer tools
case 'F12':
ipcRenderer.invoke('toggle-devtools');
break;
// Reload window
case 'F5':
ipcRenderer.invoke('reload-window');
break;
}
});
async function load() {
console.log('load(): start');
try {
await readdir('.'); // <= it hangs here on reload
document.body.innerHTML = 'success! press F5 to reload';
} catch (error) {
document.body.innerHTML = `<pre>${error.message || error.stack}</pre>`;
}
console.log('load(): end');
}
load().then(console.log, console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment