electron-vite/electron-vite-react

contextBridge communication via preload is not working

umerjaved178 opened this issue · 3 comments

Unable to get window.electronAPI on the page

const { contextBridge, ipcRenderer } = require("electron");
contextBridge.exposeInMainWorld("electronAPI", {
setTitle: (title) => ipcRenderer.send("send-message", title),
});

node integration is on

@caoxiemeihao the core functionality of electronjs is broken, can you please look into this issue

  win = new BrowserWindow({
    title: 'Main window',
    icon: join(process.env.VITE_PUBLIC, 'favicon.ico'),
    webPreferences: {
      preload,
      // Warning: Enable nodeIntegration and disable contextIsolation is not secure in production
      // Consider using contextBridge.exposeInMainWorld
      // Read more on https://www.electronjs.org/docs/latest/tutorial/context-isolation
-     nodeIntegration: true,
-     contextIsolation: false,
    },
  })

If this doesn't solve your problem, you can use the project created by create electron-vite, that is different.

@umerjaved178 Seems that there is currently no typescript support or declared types for the exposed API, probably requires some form of polyfilling.

However I found a workaround to the issue:

await (window as any).electronAPI.setTitle(title)