IPC with Vite / svelte-kit
See original GitHub issueHello! Thanks for such a great template! 🚀
I cannot seem to get interprocess communication working. Now that remote has been depreciated, in a project I have using Vue/webpack everything is converted to IPC. All good.
At first I thought it may be because of using an older version of this template, but persists after scaffolding a new project.
Possibly related issue vite-electron-builder #189
How can I get IPC to work with Svelte-Kit? Here is an error I am getting:
[svelte] 2:21:17 PM [vite] Error when evaluating SSR module /Users/redacted/Developer/redacted-dev/redacted/src/routes/__layout.svelte:
[svelte] TypeError: Cannot create proxy with a non-object as target or handler
[svelte] at nodeRequire (/Users/redacted/Developer/redacted-dev/redacted/node_modules/vite/dist/node/chunks/dep-e9a16784.js:68214:12)
[svelte] at ssrImport (/Users/redacted/Developer/redacted-dev/redacted/node_modules/vite/dist/node/chunks/dep-e9a16784.js:68164:20)
[svelte] at eval (/Users/redacted/Developer/redacted-dev/redacted/src/routes/__layout.svelte:9:31)
[svelte] at instantiateModule (/Users/redacted/Developer/redacted-dev/redacted/node_modules/vite/dist/node/chunks/dep-e9a16784.js:68197:166)
[svelte] Cannot create proxy with a non-object as target or handler
[svelte] TypeError: Cannot create proxy with a non-object as target or handler
I have not yet changed the webPreferences object from the template default.
webPreferences: {
enableRemoteModule: true,
contextIsolation: true,
nodeIntegration: true,
spellcheck: false,
devTools: dev,
}
WebPrefs in my other project for comparison:
const webPrefs = {
nodeIntegration: false,
enableRemoteModule: false,
contextIsolation: false
}
Is the issue with the main process or renderer?
if I pass
// electron.cjs
mainWindow.webContents.send('my-channel', 'message')
…
in renderer (say in __layout.svelte
):
import { ipcRenderer } from 'electron'
ipcRenderer.on('my-channel', (event, message) => {
console.log(message) // Prints 'message'
})
Then I get the error mentioned above. Thanks for any insight and if this is beyond the scope of the template project then please accept my apologies.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (1 by maintainers)
The response from main process can be wrapped in a browser check. See: https://kit.svelte.dev/faq
Main process:
in Svelte component:
https://stackoverflow.com/a/59888788/4727183
@FractalHQ this seems to solve the problem. Not an issue with the template, but rather just proper implementation. Closing.