react-router: "Warning: Automatically setting basename using is deprecated" errors
Version
2.0.0
Test Case
https://github.com/tomduncalf/react-router-base-issue (issue is not reproducable in JSBin, attempted at http://jsbin.com/poqige/edit?html,js,output, so I suspect the issue is related to ES6/Babel/webpack module bundling/loading somehow)
Steps to reproduce
- Add a
<base href="http://whatever">tag to the head of a page containingreact-routerโs HTML using the defaultbrowserHistory - Load the page -
history.jswill output 2 errors,Warning: Automatically setting basename using <base href> is deprecated and will be removed in the next major release. The semantics of <base href> are subtly different from basename. Please pass the basename explicitly in the options to createHistory - Change the react-router history to use
historywherehistoryisconst history = useRouterHistory(createHistory)({ basename: 'http://whatever' }), which should fix the problem (as basename is now explicitly passed) - Reload the page
Expected Behavior
Console errors do not appear
Actual Behavior
2 console errors still appear
Additional notes
I have done a bit of digging into this, and I believe what is going on is something like:
-
history2.x warns if the page has abasetag and nobasenameoption is passed in. However, we are passing abasenameoption, so this should not be an issue. -
However, https://github.com/reactjs/react-router/blob/master/modules/index.js#L31
exports https://github.com/reactjs/react-router/blob/master/modules/hashHistory.js. Thisexport(Babel) compiles down to:import _hashHistory from './hashHistory'; export { _hashHistory as hashHistory }; -
https://github.com/reactjs/react-router/blob/master/modules/hashHistory.js#L3 contains
export default createRouterHistory(createHashHistory) -
https://github.com/reactjs/react-router/blob/master/modules/createRouterHistory.js#L10 creates a new
historywithout any options:history = useRouterHistory(createHistory)()so I guess is where the error comes from
(the same is also true of https://github.com/reactjs/react-router/blob/master/modules/browserHistory.js)
So because this export is actually an import then an export, the import triggers a call to createRouterHistory for the default export of hashHistory, which in turn creates a new history without the options we need to pass to it to avoid an error.
A few options I can think of are:
- Ignore the errors, they only show in dev (not ideal)
- Fix the issue on the
historymodule end somehow, e.g. by passing a flag to silence these errors - Fix the issue in
react-router, e.g. makehashHistoryexport a function that callscreateRouterHistoryrather than calling it directly, or to somehow pass the options through
Would be great to get your input on this, first time I have looked in the react-router codebase otherwise I might have dug deeper and tried to fix ๐
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 30 (11 by maintainers)
@ochowie, yes, for now this is the solution (and make sure you only directly import the modules you need from react-router). Would be nice if browserHistory accepted options I agree!
Please follow the guidelines on https://github.com/reactjs/react-router/blob/master/docs/guides/MinimizingBundleSize.md#import-from-react-routerlib.
Iโm thinking about lazily initializing the history objects by using a membrane, but thatโs some ways away.