Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use createElectronRouter with electron-vite #28

Closed
dantman opened this issue Nov 23, 2024 · 3 comments · Fixed by #30
Closed

Cannot use createElectronRouter with electron-vite #28

dantman opened this issue Nov 23, 2024 · 3 comments · Fixed by #30

Comments

@dantman
Copy link

dantman commented Nov 23, 2024

I just tried adding electron-router-dom to a project using electron-vite but ran into a roadblock when trying to refactor the code that calls loadURL/loadFile to use createElectronRouter.

The code in question is written like so, which seems to be effectively what electron-vite's HMR guide suggests.

if (!app.isPackaged && process.env['ELECTRON_RENDERER_URL']) {
    myAppWindow.loadURL(
        `${process.env['ELECTRON_RENDERER_URL']}/src/renderer/main.html`,
    )
} else {
    myAppWindow.loadFile(path.join(__dirname, '../renderer/src/renderer/main.html'))
}

I examined createElectronRouter to see if there's some way I could make it happy, but it appears this cannot be refactored to ELECTRON_RENDERER_URL without some something extra.

  1. const devServerUrl = `http://localhost:${props.port ?? port}`; is hardcoded, so there's no easy way to pass in ELECTRON_RENDERER_URL. This would be simple if devServerUrl were an option.
  2. const URLRoute = createURLRoute(devServerUrl, windowId, { also hardcodes the assumption that loadURL points to the root path. Which won't work if that is not the case. I know electron-router-dom is supposed to be taking care of routing but in this case the multi-page pattern is used because the other html document is a special case like a status window that doesn't fit into being part of the router.
@sebbean
Copy link

sebbean commented Nov 27, 2024

i've been battling this all dayyyy

@sebbean
Copy link

sebbean commented Dec 3, 2024

i guess you dont need to mainWindow.loadURL anymore, registerRoute handles that

@daltonmenezes daltonmenezes linked a pull request Jan 22, 2025 that will close this issue
@daltonmenezes
Copy link
Owner

daltonmenezes commented Jan 22, 2025

@dantman @sebbean Could you please try this preview and see if the issue is resolved?

npm i https://pkg.pr.new/electron-router-dom@30

This preview adds the devServerUrl property, it has priority over both hardcoded localhost url and port property

it must work by providing it in the createElectronRouter function

export const { Router, registerRoute, settings } = createElectronRouter({
  // port: 4927,
  devServerUrl: 'http://localhost:4927',

  types: {
    ids: ['main'],
  },
})

or in the registerRoute function

  registerRoute({
    id: 'main',
    browserWindow: mainWindow,
    devServerUrl: 'http://localhost:4927',
    htmlFile: path.join(__dirname, '../renderer/index.html'),
  })

if it is necessary to pass an env var, this must be done in the registerRoute method (main process)

@github-actions github-actions bot locked and limited conversation to collaborators Feb 9, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants