-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
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
Add <Routes baseHref> #353
Comments
OK. I found I'd recommend to rename this property to |
@korpirkor I'm going to leave this open tho because I think |
@mjackson yeah, but we were all talking about different things here #111 In our app we find it decent to just add the base url to our root route path, everything else is relative from there and not a big deal. <Routes>
<Route path="/some/sub/dir">
<!-- since its relative here, I only have to specify the sub directory once
which is not really different than a baseUrl option, is it? -->
<Route path="foo" />
</Route>
</Routes> |
Yeah, it's not too much different. I still think |
|
Can't make it work @mjackson I'm developing using webpack so my root url on development machine is like this
I'm i'm doing just
it works with subdirectory but I don't see path changing in the address bar when navigate thru the states. |
I'm running router with
so it's html5 mode |
Is this now possible, or still considered unnecessary? Am using Unfortunately for me, my root path is different on different servers, and again different on our clients prod domains. I could set the root path dynamically, having assessed the domain structure, but seems fairly brittle. Also, maybe I'm missing something, but Redirects seem to need the full route path for
SomeOtherClass.js
|
I need it as well for the same reasons @jamiehill explained (the root path is different on different servers). It'd be great if we don't have to pass the |
Maybe the cleanest way is to write a helper to create URLs to prepend the Another solution is to monkey patch some |
OK I found a very interesting example. You can add the import { createHistory, useBasename } from 'history'
const history = useBasename(createHistory)({
basename: '/my-custom-root'
}) |
Thanks @vrde I have an app runs under let base = "/app_name/"
<Router history={browserHistory}>
<Route path={base} component={App}></Route>
</Router>
<Link path={base + "some_path"}>some_path</Link> With const browserHistory = useBasename(createHistory)({
basename: "/app_name"
});
<Router history={browserHistory}>
<Route path="/" component={App}></Route>
</Router>
<Link path="/some_path">some_path</Link> It's more convenience and clear. |
Sorry what version are we talking here @anjianshi ? |
const React = require("react");
const ReactDOM = require("react-dom");
import { Router, IndexRoute } from "react-router";
import { createHistory, useBasename } from "history";
const App = require("./App.jsx");
const Home = require("./Home.jsx");
const browserHistory = useBasename(createHistory)({
basename: "/app_name"
});
ReactDOM.render(
<Router history={browserHistory}>
<Route path="/" component={App}>
<IndexRoute component={Home} />
</Route>
</Router>,
document.getElementById("wrap")
); |
Yes, that's what |
I'm sorry but sometimes an issue is closed before all questions have been satisfied. And that said, both @mjackson and @ryanflorence have added comments since it was closed. |
@taion yes it was a necropost from my side, but I found this issue googling the keywords react-router baseurl. I'm quite sure many developers will land here using similar keywords, that's why I added some info about the solution. |
@vrde I owe you a beer, dude. I was really stuck on this problem before I found your post. Thank you! |
useBasename is awesome for client-side. Any ideas how to have links include baseName for server-side rendering? |
I managed to get this working in const browserHistory = useRouterHistory(useBasename(createHistory))({
basename: "/baseurl"
}); Unfortunately, it only worked with the provided I solved it by wrapping the API by my own function that simply prepends the base url and I also added the base url to the root To be honest, I kind of miss the times before |
It appears that you are doing multiple things wrong. Consider reaching out on Stack Overflow or Reactiflux for support. |
Ah :D Well it's very possible. I had the same problem this thread addresses and since it's been active until pretty recently I considered the information here pretty up-to-date. |
A lot of things change, especially internally. We don't want to encourage the use of the issue tracker for this sort of thing, because the issues stay the same while the code marches onward. It's not generally safe to follow stale old issue tracker examples, and it's confusing to us as well when people point to very old examples and expect them to work. |
I understand that. Sorry. Do you think you could point me to a good source of information regarding this problem? This is the only source I was able to find. |
As I just said, try Stack Overflow or Reactiflux. |
For fellow React noobs who might not consider this "obvious" alternative right away: this isn't an issue if you use the old-fashioned, hash-based URLs (via createHashHistory in 1.0.x or the newer hashHistory). |
I had alot of trouble getting yair-pantaflix's solution working with [email protected]:
Finally worked out that this solution will not work with the latest version of history, I needed to downgrade to [email protected] |
To what extend
is version sensitive? |
Same here. [email protected] and [email protected] If I try this it gives me the same error: "Uncaught TypeError: createHistory is not a function" This is a very urgent problem! |
Hi Johannes, this particular problem I solved:
but another issue had arisen. Having pretty URL is nice but each reload requires to begin with localhost:8080 which means manual labor instead of hot reload. |
How does this solve the problem? When I'm just importing browserHistory, how can I define a basename? |
Sorry, without defining your own brand new basename but you got rid of random abracadabra. |
Using This implementation is based upon the file withExampleBasename.js in React Router Examples:
Please "@mention" me if you have any feedback on this implementation. |
Hello @beausmith Do you use the same router for your server side and client side rendering? This is the scenario in which we have problems. When NodeJS is trying to use your "universal" router to do the server-side rendering, it is unable to use browserHistory, throwing an Invariant Violation... |
@Richacinas It might vary a little bit depending on what version of react-router you're using, but if you have the // inside your `match` callback
const basename = YOUR_BASENAME;
renderProps.history = useBasename(() => renderProps.history)({ basename });
renderProps.router = { ...renderProps.router, ...renderProps.history };
<RouterContext { ...renderProps } /> |
@Richacinas I haven't ventured into server-side rendering yet. |
To get basename working for [email protected]:
import React from 'react'
import { render } from 'react-dom'
import { Router, Route, IndexRoute, useRouterHistory } from 'react-router'
import { createHistory } from 'history'
const history = useRouterHistory(createHistory)({
basename: '/subdirectory-where-the-app-is-hosted-goes-here'
})
render((
<Router history={history}>
<Route path='/' component={Layout}>
<IndexRoute component={HomeView} />
<Route path='other-views' component={OtherViews} />
</Route>
</Router>
), document.getElementById('main')) You'll also need to set up .htaccess if you're running Apache or nginx.conf if you're running nginx:
|
@unyo Since history is a dependency of react-router you don't need to install the package manually. It is already there and you can simply import it. |
@unyo, any advice to make that solution works in Firebase? It works great locally but not deployed in Firebase hosting 😢 |
How does this work in react-router 4? Been digging through source code and haven't found useRouterHistory yet. |
Looks like this guy had it:
|
@pshrmn Thank you |
Looks like nobody stops me from using something like this: Content should be a wrapper function that return another function with anything that should be rendered |
BTW: basename doesn't work if contains protocol and host, so if anyone would like to use construct(props) {
super(props)
this.basepath = (process.env.PUBLIC_URL === '.') ?
undefined :
process.env.PUBLIC_URL.substr(`${window.location.protocol}//${window.location.host}`.length)
}
render() {
return (
<Router basename={ this.basepath }>
//...
</Router>
)
} |
React router should be automatically detecting the context root instead of hardcoding it in code. Any one can deploy that build with any context they like we cannot restrict them. In current scenario we need to update basename everytime we change context root and moreover setting context root is not part of developement it is part of deployement. |
If I"m using However,
Anything unsafe about- Does not return anything. Any ideas on that? |
@yarnball that code doesn't work as it returns only first segment ( IMHO optimal way is to extract pathname from |
I think, the api is changing a lot in every version.
And it works for me. import { createBrowserHistory } from 'history';
import { Router } from 'react-router-dom';
export const history = createBrowserHistory({ basename });
export const AppRouter = () => (
<Router history={history}>
...
...
</Router>
) The API gets more simplified. |
Now I'm using code similar to @ritwickdey's example. |
Hey!
When website is in subdirectory (like
localhost/projects/MyProject
), react router triggersNotFoundRoute
instead ofDefaultRoute
for/
.It would be cool if there is base href option for react-router.
Thanks, Cezary
The text was updated successfully, but these errors were encountered: