-
Notifications
You must be signed in to change notification settings - Fork 15.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: Unsanboxed preload running after page with content is loaded (ESM) #40777
Comments
@Arkellys Without a full repro there's not much to look at here, but per our docs does your preload script have the |
I thought the example was simple enough that it didn't need a repo, but I can make one if you think it's necessary? And yes the preload has |
Can confirm that this is also happening to our app, though intermittently, so clearly a race-condition. |
Hello @Arkellys. Thanks for reporting this and helping to make Electron better! Would it be possible for you to make a standalone testcase with only the code necessary to reproduce the issue? For example, Electron Fiddle is a great tool for making small test cases and makes it easy to publish your test case to a gist that Electron maintainers can use. Stand-alone test cases make fixing issues go more smoothly: it ensure everyone's looking at the same issue, it removes all unnecessary variables from the equation, and it can also provide the basis for automated regression tests. Now adding the
blocked/need-repro
|
I'm encountering this as well after upgrading to Electron 28 and switching to ESM. Similar to the OP, my preload script (ending in .mjs as required) uses It does look to me like the preload script is not blocking the page load like it should, and there's a race condition regarding whether the preload script runs quickly enough before the page loads. |
I had the time to search a bit more about this issue, and I managed to pinpoint some elements that trigger it:
Test cases: 🟢 No bug - 🔴 Always bug - 🟡 Inconsistent
So yeah, very inconsistent bug... Sadly it's a blocker for switching an app to ESM. |
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment! |
Bump. |
Issue is still present on |
Bump, issue still present on |
Bump, issue still present on |
i am using electron v32.x and electron vite with esm. I worked around it my initialize my app on "dev" mode (i am just checking if Since i have some component imports that are directly using some apis definded in the preload.mts and need to load them with async imports. But at least this allows me to stay with wonderful esm. I guess people using other spa/ui frameworks can work around the same way. |
Bump, still an issue in |
Adding a |
Bump, issue still present on |
One solution is to wait for the API provided by the preload script to load completely before calling it, regardless of the execution order, to avoid errors.
function checkPreloadReady() {
if (window.api) {
console.log("Preload script completed. Executing renderer script...");
// Execute the rendering script code here 在这里执行渲染脚本的代码
} else {
console.log("Waiting for preload script to complete...");
setTimeout(checkPreloadReady, 1000); // Adjust polling interval as needed 轮询间隔时间可以根据需要调整
}
}
checkPreloadReady(); |
Preflight Checklist
Electron Version
28.0.0
What operating system are you using?
Windows
Operating System Version
Windows 10 Family
What arch are you using?
x64
Last Known Working Electron version
No response
Expected Behavior
Electron 28 added support for ESM, the new doc states that:
So for an HTML with content, I expect the preload to be run before the page loads:
Actual Behavior
For the following HTML:
Loaded as:
With in
package.json
:The JS file linked to my HTML shows the page is loaded before the preload.
For context,
listen
is a function exposed on the preload withcontextBridge
.Testcase Gist URL
No response
Additional Information
I can't see the
Content-Length
header in the response headers:The file is loaded from
http://localhost:3000/worker.html
The text was updated successfully, but these errors were encountered: