caoxiemeihao / nuxt-electron

Integrate Nuxt and Electron
MIT License
184 stars 21 forks source link

VITE_DEV_SERVER_URL is a random port #77

Closed abarke closed 4 months ago

abarke commented 4 months ago

Since upgrading electron, nuxt and nuxt-electron, electron does not respect the line anymore:

Previously this worked. But now electron ignores this and uses VITE_DEV_SERVER_URL

win.loadURL("http://localhost:3000");

VITE_DEV_SERVER_URL is always set to a random port

I seem to remember that some time ago it was always port 3000. I need to explicity set it to 3000 as I want to persist localstorage and user session.

How can I fix this so electron in dev/prod mode always used port 3000?

abarke commented 4 months ago

When starting nuxt it even says it's opening on 3000:

image

But VITE_DEV_SERVER_URL=http://localhost:51622 🤔

All very confusing 😵‍💫

abarke commented 4 months ago

This one is definitely strange. When I change the electron/main.ts it rebuilds but does not update in electron-dist 🤔 if I remove the existing main.js, electron fail to run with Unable to find electron app.

OK... now I found the issue. I stupidly missed a dependency upgrade regression/migration issue import error in the electron logs, so it failed to build the main.js file. Electron however would still open as the preload.ts was there and an old version of the main.js giving the illusion of caching.

Now I fixed the error and main.js is building again, normality is restored and I'm a happy chappy 🙂 what a ride 🚵

kingyue737 commented 1 month ago

In Nuxt we should not read from VITE_DEV_SERVER_URL which is set by this module. As you've seen, it's a random port.

https://github.com/caoxiemeihao/nuxt-electron/blob/d7cf10ddaf8e050564d46b0a906cc7a468573e60/src/index.ts#L110

I think the following is the right URL for window to load:

win.loadURL(JSON.parse(process.env.__NUXT_DEV__!).proxy.url)
hifron commented 2 weeks ago

According electron tutorial ESM modules support could vary according required build.

Recommended is to client builds of vite so nuxt should support client side rendering and build without ssr and maybe expose some ipc isolations and sandbox webpreferences.