-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
React router path "/" doesn't work correctly on Cordova Android? #2161
Comments
@ubill Have you tried crosswalk? |
I would highly recommend using the most recent stable version (v1.0.0-rc1) we fixed bunch of bugs. Also as you are trying to use React Router with Cordova and also with Redux, have a look at this react-cordova-boilerplate. |
Thank you all for your suggestions. I have confirmed the solution and would like to share it here. If you are using desktop browser e.g. Chrome, you can use BrowserHistory. const history = new BrowserHistory(); However if you are using Cordova, you have to use HashHistory: const history = new HashHistory(); The URLs you get will no doubt be different for each implementation, but only the latter works on Cordova. @anri-asaturov Many thanks for the suggestion! I have not started using CrossWalk yet (will do so) but if it works I suspect it's due to CrossWalk supporting 'pushState' history, same as modern desktop browsers. @knowbody Now I confirmed it's not a problem with the version I'm using but with the router's History implementation used. Hope it helps other users and thanks for your suggestion! Cheers all! |
great, thanks @ubill |
@ubill is there a reason why HashHistory works on Cordova? I've been using |
@rclai I don't know of any reason. I was just sharing the finding that for my specific case HashHistory works, but the other scheme used by default by router (BrowserHistory) didn't work for me. - I'm not sure if it works for other people and I am the only one facing a problem. But if it doesn't work, only possibility I could think of:
|
hashHistory works because it doesn't change the base URL from which assets are loaded, and also perhaps react-router bugs with file:// |
@rclai So you have used |
Yeah it worked for me on an android phone |
I am getting
|
browser history assumes you have a server. Cordova apps run w/o a server, so you need hash history. |
I wonder why it works for @rclai then, Ryan? Also, why does it need a server? It does not fetch any content from a server, does it? Isn't it just rewriting the browsers location object but not actually handling any navigation? I mean, that's the idea of SPA, isn't it? |
You can also simply set in head, but that doesn't work with file urls. |
I was able to get non-hash routing to work on Cordova (iOS and Android) by doing this ugly hack:
|
@mkristo would you kindly post a snippet showing your solution, including the require/import statements? Thanks! |
@mbrevda @mkristo might be a little late, but I'm trying this now like so but having no luck on Cordova with: import { Router, browserHistory, useRouterHistory, Redirect } from 'react-router'
import { createHistory } from 'history';
let historyEngine = browserHistory;
if (isCordova()) {
historyEngine = useRouterHistory(createHistory)({
basename: document.location.href.replace('file://', '')
});
} I end up with the follwing in an iOS Simulator I've only started using react-router recently so not much insight without reading into the code, but it appears that this solution has an error in the replace logic Perhaps something to do with versions of history and react-router etc. I'd imaging the path should be file:///APP_CONTAINER/TYPICAL_IOS_URL_STUFF/index.html/login Using [email protected] and [email protected] |
@evanshortiss: The code has evolved a little since I wrote the snippet above.
I have a vague memory that the code change was to handle deep linking. Using older versions, react-router 2.3.0 and history 2.0.1. |
@mkristo, thanks! Will need to try this out - went down the hash history route which is working for now but would rather do what you've done to keep Cordova consistent 👍 |
@evanshortiss where did you get |
Works great for me; thanks! |
Hello, I am using react-router, and generate a bundle.js file with webpack,
my dependencies are:
then I use the bundle.js in a script tag in my cordova app:
my bundle.js is created with this file: (index.js)
But when I run it on my phone (cordova run android)
The path = "/" always seems to resolve to "/android_asset/www/index.html"
and my app won't work on phone because of this.
The router behaves correctly when I am developing on my browser, though: (in fact the below is my full code that works on the browser, I stripped it down to narrow down the root cause on my phone)
What do I do in this case to get my app to behave correctly on-device? Am I using the react-router API correctly or is there something else I don't know about?
The text was updated successfully, but these errors were encountered: