Electron react app routing returns blank page in production

Viewed 411

Hey so i finally finished my react electron app and it works well in development however, it returns blank pages when i click on a button on the homescreen to direct me to a different page.

Attempted solutions

  • I do use HashRouter instead of normal Router
  • Tried adding a # at the end of file://${path.join(__dirname, "../build/index.html#")}

Problem Basically the Main Menu at route "/" shows up and so does the backgroundProcess window but when i click on a button to direct me to /slow or /fast etc it returns a blank page

My windows in main.js file:

mainWindow.loadURL(isDev ? "http://localhost:3000" : `file://${path.join(__dirname, "../build/index.html#")}`);
backgroundProcess.loadURL(isDev ? "http://localhost:3000" : `file://${path.join(__dirname, "../build/woker.html")}`)
configWindow.loadURL(isDev ? `http://localhost:3000#/config${mode.toString()}` : `file://${path.join(__dirname, `../build/index.html#config${mode}`)}`);
AbiWindow.loadURL(isDev ? "http://localhost:3000#/abi" : `file://${path.join(__dirname, "../build/index.html#abi")}`);

App.js routing:

  <HashRouter>
      <Route exact path='/' component={Mainmenu}/>
      <Route exact path='/fast' component={Fast}/>
      <Route exact path='/config0' component={ConfigWindow}/>
      <Route exact path='/abi' component={AbiWindow}/>
      <Route exact path='/slow' component={Slow}/>
      <Route exact path='/config1' component={SlowConfig}/>
      <Route exact path='/sell' component={Sell}/>
      <Route exact path='/config2' component={SellConfig}/>
  </HashRouter>

MainMenu Button routing:

function MainMenu() {
    return ( 
        <div className='no-gutters'>
            <div className='col no-gutters'>
                <div className='fast d-flex justify-content-center align-items-center'>
                    <Link className='fastbtn' to="/fast">FAST</Link>
                </div>
            </div>
            <div className='col no-gutters'>
                <div className='slow d-flex justify-content-center align-items-center'>
                    <Link className='slowbtn' to="/slow">SLOW</Link>
                </div>
            </div>
            <div className='col no-gutters'>
                <div className='sell d-flex justify-content-center align-items-center'>
                    <Link className='sellbtn' to="/sell">SELL</Link>
                </div>
            </div>
        </div>
     );
}


Any help is greatly appreciated Thanks in advance :)

1 Answers

If you are using the lastest electron, you may change <HashRouter> to <BrowserRouter basename={'/'}>. Both router should work fine in Electron, at least in my test. It is the basename that results a blank screen.

This should let the render process know where the right url is.

Also the homepage in the package.json should still be ./.

This is what I did today and the corresponding release. See the change of src/App.tsx

https://github.com/wongchito/RailMapGenerator/commit/1a2a5dbf3c5dca710f168837649e2b77e347c6b0

https://github.com/wongchito/RailMapGenerator/releases/tag/3.12.2

Related