react-router: react-router-dom v5 does not work with electron in production
I’m having an issue using react-router-dom with electron. In development, the router correctly functions and routes users between the different pages. In production, the router no longer functions correctly. In addition to the repo showing the issue, I have some gifs that can quickly outline the problem. Working in development, not working in production.
I am not using redux, and I am using the HashRouter, but I have also tried the MemoryRouter with no luck…
edit I just tested with version 4 of react router dom and it works correctly in production and development. So it seems that this issue is related to version 5.
Version
"electron": "^5.0.0",
"electron-builder": "^20.39.0",
"electron-webpack": "^2.6.2",
"react-router-dom": "^5.0.0"
Test Case
https://github.com/kyle-mccarthy/react-router-dom-electron-issue
Steps to reproduce
The best way to reproduce this error is through the provided github repo.
Expected Behavior
The router should continue to work under the production environment.
Actual Behavior
The router does not correctly work in production.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 34
- Comments: 30 (2 by maintainers)
Commits related to this issue
- Downgraded react-router-dom to v^4.3.1 The latest version of react-router-dom v5.0.0 was having trouble with Production builds of Electron app. For more information: https://github.com/ReactTraining... — committed to codesbiome/electron-react-webpack by deleted user 5 years ago
- chore: Update version for release (pre) (#6726) Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> — committed to remix-run/react-router by github-actions[bot] a year ago
Hi Hackers,
The problem with electron and react-router-dom is that electron when working in production mode (not really) is that you with electron are creating an application, (although it is known that it is a scan with js code running in backgroud), electron does not handle history and works with the synchronized URL, BrowseHistory does not work with electron, therefore you must use HashRouter that if you synchronize your URL with a UI that is (windows.location.hash).
This would be your final code for electron and react-router-dom to work perfectly
This is the solution … it is not a problem of versions
Happy Hacking
Ok, got this sorted:
The issue was that in some places I was importing (IDE did this for me) Link like:
changing it to:
solves the issue for me.
Just use HashRouter, browser router has too many configurations and will end up breaking your code. Proper way of doing it
Here’s a quick solution that seems to be working for me. Replaced
createBrowserHistorywithcreateHashHistoryand everything started working again in production electron app.The reason might be described in this StackOverflow issue.
I’ve been folllowing this issue as I’ve had a similar problem. I’ve tested in my project and have been able to upgrade to v5.0.0 and the router works in production.
The issue may be that electron-webpack may be breaking
react-router-domin production. You can whitelistreact-router-domto solve this.Add a electron-webpack.json file containing the following:
I wasn’t able to get it to work with your demo project, but that might be just a difference in build configuration. I hope this was at least some help to you.
For what it’s worth, here’s my scripts and build config from
package.jsonSo I actually stumbled across that which is what led me down this path. In development the (non minified) cjs file works fine, but in production there is an error.
Something equally interesting is that explicitly using “react-router-dom/cjs/react-router-dom.min.js” works in development AND production.
However, while using the normal react-router-dom import I don’t think that the production/minfied version of react-router-dom is ever actually used.
I went and changed back my import to just react-router-dom and then edited the index.js file of react-router-dom to the following
When I run the app in dev I get the expected
react-router-dom DEVbut I also getreact-router-dom DEVin production.One thing to note is that the electron app doesn’t actually have NODE_ENV set to production while it runs as a bundled app, but it does have the NODE_ENV as production while it is compiling.
It almost makes me think that both versions of react-router-dom are being included.
Helped this answer.
It’s not advisable to use BrowserRouter in electron. Just use HashRouter.
replacing
<BrowserRouter>to<HashRouter>should work. 👍BrowserRouter never worked in electron, everyone mentioned this like 10 times they are using hashrouter, the real solution is what NileDaley said about “whiteListedModules”, if you read documentation https://webpack.electron.build/configuration it says:
so I just added all FE dependencies into
whiteListedModules, not justreact-router-domand everything works as expected in production.It works for me!
I added this at my entry file:
I used Babel to automatically replace the
process.env.NODE_ENVto'production'.So after compiling, it will become:
The
react-routr-domworks normally.My thoughts 🤔
It is possible that incorrect files have been imported or other conditional statements entered in the production due to
process.env.NODE_ENVmissing.Most packers, such as Webpack and Parcel, simply replace
process.env.NODE_ENVwith'production', but they doesn’t apply the value to theprocess.env.Bumping this. My project wasn’t working on production when I had
react-router-dom@v5.0.1installed but works when I downgraded tov4.3.1.Replacing
<Router>with<HashRouter>worked for me. 🎉@granaber i repeat, nobody used BrowserRouter in Electron ever, everybody are already using HashRouter, and It didn’t work for me with React Router V5 on production nor it worked for the creator of this thread, he had to downgrade to React Router v4… when you whitelist externals, it works with React Router v5…
Top demais @zaynekomichi me salvou aqui em 2023
@WilianZilv, Did you solved the issue? HashRouter is not working on my end.
False White-listing Externals, if hashrouter works
I’m also encountering the issue kyle-mccarthy mentioned.
I’ve confirmed the same behavior, that with the latest react router I can run the electron packaged app with NODE_ENV=production and everything works fine. But running it without that will cause all routes to fail.
Their suggested workaround to import from
react-router-dom/cjs/react-router-domworked for me, and I’m able to use that to build working production apps again. (Thanks!)My relevant dependencies: