Skip to content

Commit

Permalink
[example] Improve preload bridge
Browse files Browse the repository at this point in the history
- Enables only in devmode
- Generic port
  • Loading branch information
HaNdTriX committed Aug 11, 2022
1 parent 0d408fa commit 4c49763
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions example/main/preload.js
Expand Up @@ -8,17 +8,23 @@ contextBridge.exposeInMainWorld("electron", {
},
});

// Patch the global WebSocket constructor to use the correct DevServer url
// 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
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:3000/_next/webpack-hmr", protocols)
} else {
return new Target(url, protocols)
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)
}
}
}
})
});`);
})
});`);
}

0 comments on commit 4c49763

Please sign in to comment.