VITE_DEV_SERVER_URL returning random port
Since I upgraded my dependencies to the latest (see table), electron no longer starts on port 3000.
┌────────────────────────────┬─────────┬────────┐
│ Package │ Current │ Latest │
├────────────────────────────┼─────────┼────────┤
│ @aws-amplify/ui-vue │ 3.1.23 │ 3.1.27 │
├────────────────────────────┼─────────┼────────┤
│ @types/uuid (dev) │ 9.0.2 │ 9.0.3 │
├────────────────────────────┼─────────┼────────┤
│ electron-builder (dev) │ 24.6.3 │ 24.6.4 │
├────────────────────────────┼─────────┼────────┤
│ @vueuse/rxjs │ 10.3.0 │ 10.4.1 │
├────────────────────────────┼─────────┼────────┤
│ nuxt (dev) │ 3.6.5 │ 3.7.1 │
├────────────────────────────┼─────────┼────────┤
│ electron (dev) │ 25.4.0 │ 26.2.0 │
├────────────────────────────┼─────────┼────────┤
│ vite-plugin-electron (dev) │ 0.12.0 │ 0.14.1 │
└────────────────────────────┴─────────┴────────┘
Vite seems to start on 3000, however when I dump the VITE_DEV_SERVER_URL variable it says http://localhost:51316 🤔
Any ideas?
Workaround in main.ts
if (process.env.VITE_DEV_SERVER_URL) {
mainWindow.loadURL("http://localhost:3000"); // dev
} else {
mainWindow.loadFile("dist/index.html"); // production
}
This bug makes cookies and localstorage be lost at every electron restart. Since the port randomly changes the server origin changes, thus Electron (aka chromium) won't load the persisted data.
Who can provide a 100% reproduction repo?
This bug makes cookies and localstorage be lost at every electron restart. Since the port randomly changes the server origin changes, thus Electron (aka chromium) won't load the persisted data.
Indeed this was the same issue for me. The current workaround for me is the following:
if (process.env.VITE_DEV_SERVER_URL) {
mainWindow.loadURL("http://localhost:3000"); // dev
} else {
mainWindow.loadFile("dist/index.html"); // production
}
@caoxiemeihao You can try the quick start demo and print VITE_DEV_SERVER_URL after running it. It will indeed be generated randomly every time and needs to be specified manually like above. My current temporary solution is to get 0.0.0.0 from process.env.NUXT_DEV and replace it. into localhost and put it in loadURL。