Reputation: 51668
I have a normal Electron app, with a BrowserWindow that loads a file:
win = new BrowserWindow(...);
win.loadURL(url.format({
pathname: path.resolve(__dirname, 'main.html'),
protocol: "file:",
slashes: true
}));
I'm using electron-build to package the app. Most of the time this works fine. But occasionally when I build it, both for Mac and Windows, the BrowserWindow doesn't load the file, and shows an error in the console:
Not allowed to load local resource: file:///path/to/MyApp.app/Contents/Resources/app.asar/dist/main.html
In the developer tools' Network panel, it says (blocked:other)
under Status.
But unlike many similar questions here, THE FILE EXISTS (in the asar archive). I can do this immediately after opening the window:
console.log(fs.readFileSync(path.resolve(__dirname, 'main.html'), { encoding: 'utf-8' }));
and it will print the file's contents.
I can see the file in the asar, at the expected path:
$ asar list /path/to/MyApp.app/Contents/Resources/app.asar | grep main.html
/dist/main.html
So why won't it read the file? How can I debug this?
For what it's worth, my code is in myproject/dist
, and in myproject/package.json
, I have:
"build": {
"directories": {
"output": "out"
},
"files": [
"dist"
],
...
}
I'm using the latest versions of electron and electron-builder as of now, but I've had the issue on earlier versions.
I tried using a file path instead of a URL:
win.loadFile(path.resolve(__dirname, 'main.html'));
but got the same result.
I also tried using "asar": false
. I get the same error, just with "app" instead of "app.asar" in the path. And the path points to a valid, readable file.
Upvotes: 0
Views: 1426
Reputation: 51668
OK, figured it out. I'd forgotten I was intercepting the file:
protocol with protocol.interceptFileProtocol
. My app name has a space in it, so when I called the callback
function, and didn't replace the %20
with an actual space, it couldn't find the file. Hope this helps someone.
Upvotes: 0