Skip to content

Commit

Permalink
fix: windows - Fixed issue with electron Windows program opening blank (
Browse files Browse the repository at this point in the history
  • Loading branch information
HashCookie authored Jun 2, 2024
1 parent bbccf4f commit 39757fd
Showing 1 changed file with 12 additions and 36 deletions.
48 changes: 12 additions & 36 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,7 @@ const path = require('path');

protocol.registerSchemesAsPrivileged([{ scheme: 'app', privileges: { secure: true, standard: true } }]);

let appServe;
let isDev;

const loadModules = async () => {
isDev = (await import('electron-is-dev')).default;
if (!isDev) {
const serve = (await import('electron-serve')).default;
appServe = serve({
directory: path.join(__dirname, 'out'),
});
}
};

const createWindow = async () => {
const createWindow = () => {
const win = new BrowserWindow({
width: 1200,
height: 800,
Expand All @@ -39,33 +26,22 @@ const createWindow = async () => {
},
});

if (!isDev && appServe) {
win.loadFile(path.join(__dirname, 'out/index.html')).catch((err) => {
console.error('Failed to load local file:', err);
win.loadURL('http://editor.casbin.org');
});

win.webContents.on('did-fail-load', async () => {
try {
await appServe(win);
win.loadURL('app://-');
await win.loadFile(path.join(__dirname, 'out/index.html'));
} catch (err) {
console.error('Failed to serve app:', err);
win.loadFile(path.join(__dirname, 'out/index.html'));
}
} else {
win.loadFile(path.join(__dirname, 'out/index.html')).catch((err) => {
console.error('Failed to load local file:', err);
console.error('Retry loading local file failed:', err);
win.loadURL('http://editor.casbin.org');
});

win.webContents.on('did-fail-load', async () => {
try {
await win.loadFile(path.join(__dirname, 'out/index.html'));
} catch (err) {
console.error('Retry loading local file failed:', err);
win.loadURL('http://editor.casbin.org');
}
});
}
}
});
};

app.whenReady().then(async () => {
await loadModules();
app.whenReady().then(() => {
createWindow();
});

Expand Down

0 comments on commit 39757fd

Please sign in to comment.