Skip to content

Instantly share code, notes, and snippets.

@eternalharvest
Created March 15, 2021 21:12
Show Gist options
  • Save eternalharvest/e6b26f8e20d3ebf6048bd50ef8c3eb63 to your computer and use it in GitHub Desktop.
Save eternalharvest/e6b26f8e20d3ebf6048bd50ef8c3eb63 to your computer and use it in GitHub Desktop.
Electron BUG BrowserWindow#loadURL() throw an error
const { app, protocol, BrowserWindow } = require('electron');
const path = require('path');
const url = require('url');
app.whenReady()
.then(() => {
protocol.registerFileProtocol('asset', (request, callback) => {
console.log(request.url);
callback({path: path.join(__dirname, url.parse(request.url).path)});
});
const mainWindow = new BrowserWindow({
webPreferences: {
contextIsolation: true
}
});
// first load (ok)
mainWindow.loadURL('asset:///test.html#section1')
.then(() => console.log('ok'))
.catch(console.error);
setTimeout(() => {
// same url with same location hash (ok)
mainWindow.loadURL('asset:///test.html#section1')
.then(() => console.log('ok'))
.catch(console.error);
}, 1000);
setTimeout(() => {
// same url with different location hash (error)
mainWindow.loadURL('asset:///test.html#section2')
.then(() => console.log('ok'))
.catch(console.error);
}, 2000);
setTimeout(() => {
// same url with same location hash (ok)
mainWindow.loadURL('asset:///test.html#section2')
.then(() => console.log('ok'))
.catch(console.error);
}, 3000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment