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

  1. Add a <base href="http://whatever"> tag to the head of a page containing react-routerโ€™s HTML using the default browserHistory
  2. Load the page - history.js will 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
  3. Change the react-router history to use history where history is const history = useRouterHistory(createHistory)({ basename: 'http://whatever' }), which should fix the problem (as basename is now explicitly passed)
  4. 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:

(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:

  1. Ignore the errors, they only show in dev (not ideal)
  2. Fix the issue on the history module end somehow, e.g. by passing a flag to silence these errors
  3. Fix the issue in react-router, e.g. make hashHistory export a function that calls createRouterHistory rather 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)

Most upvoted comments

@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!

On 27 Apr 2016, at 20:13, ochowie notifications@github.com wrote:

So with this new solution, is the approach to use const history = useRouterHistory(createHistory)({ basename: โ€˜http://whateverโ€™ }) as tomduncalf mentioned? Looking at the changed code it seems like it will still issue the warning if only the tag is present. Will there be a change to enable browserHistory to accept options as well?

โ€” You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub

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.