Isaac
Isaac

Reputation: 46

Electron webview preload only file:// protocol is supported when using create-react-app base

Electron V3.0.5

Hello, I have a project using Electron and create-react-app and I can't get my project to find the preload script.

Here's the webview tag:

<webview preload={`./Injects/preload.js`} />

When I use the tag as shown, the console tells me that I must use file: protocol. So I tried going with:

<webview preload={`file://${__dirname}/Injects/preload.js`} />

but of course that brings me to the path of the .asar packed file which doesn't have the Injects/preload.js in it.

I also tried putting my inject script in my public folder and using

<webview preload={`${process.env.PUBLIC_URL}/Injects/preload.js`} />

and

<webview preload={`file://${process.env.PUBLIC_URL}/Injects/preload.js`} />

however I just get the same errors, and process.env.PUBLIC_URL returned "" anyway.

Any help would be much appreciated, I haven't been able to find an answer to this problem anywhere.

Upvotes: 2

Views: 2638

Answers (2)

shuoGG
shuoGG

Reputation: 19

let path = `file:${require('path').resolve(__static, './preload.js')}`

I place the preload.js in the static folder in my electron project, and it worked for me

Upvotes: 1

Isaac
Isaac

Reputation: 46

To solve the problem I created a __static variable based off of __dirname which changed location depending on production or development builds.

Upvotes: 0

Related Questions