-
Notifications
You must be signed in to change notification settings - Fork 6
/
preload.js
30 lines (28 loc) · 1.09 KB
/
preload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const { ipcRenderer, contextBridge, webFrame } = require("electron");
contextBridge.exposeInMainWorld("electron", {
message: {
send: (payload) => ipcRenderer.send("message", payload),
on: (handler) => ipcRenderer.on("message", handler),
off: (handler) => ipcRenderer.off("message", handler),
},
});
// Next.js Websocket DevServer is not listining on our custom scheme.
// This is why we need to monkey patch the global WebSocket constructor
// to use the correct DevServer url
// More info: https://github.com/HaNdTriX/next-electron-server/issues/7
if (process.env.NEXT_ELECTON_SERVER_DEV === "true") {
webFrame.executeJavaScript(`Object.defineProperty(globalThis, 'WebSocket', {
value: new Proxy(WebSocket, {
construct: (Target, [url, protocols]) => {
if (url.endsWith('/_next/webpack-hmr')) {
// Fix the Next.js hmr client url
return new Target("ws://localhost:${
process.env.NEXT_ELECTON_SERVER_PORT || 3000
}/_next/webpack-hmr", protocols)
} else {
return new Target(url, protocols)
}
}
})
});`);
}