react-router icon indicating copy to clipboard operation
react-router copied to clipboard

Unable to redirect to '/' with replace in onEnter hook

Open thinklinux opened this issue 10 years ago • 4 comments

I have these two methods:

  toLogin: function (nextState, replace) {
    if (!localStorage.get("token")) {
      return replace('/signin');
    }
  },
  toHome: function (nextState, replace) {
    if (localStorage.get("token")) {
      return replace('/');
    }
  }

return replace('/signin') is working like charm but return replace('/') gives me that error:

Uncaught SecurityError: Failed to execute 'replaceState' on 'History': A history state object with URL 'http:' cannot be created in a document with origin 'http://localhost:3000'.

Do I need to set up some base URL thing that I missed in the documentation or this is a bug?

thinklinux avatar Jan 22 '16 13:01 thinklinux

Thanks for your question!

We want to make sure that the GitHub issue tracker remains the best place to track bug reports and feature requests that affect the development of React Router.

Questions like yours deserve a purpose-built Q&A forum. Could you post this question to Stack Overflow with the tag #react-router? https://stackoverflow.com/questions/ask?tags=react-router

We also have an active and helpful React Router community on Reactiflux, which is a great place to get fast help with React Router and with the rest of the React ecosystem. You can join at https://discord.gg/0ZcbPKXt5bYaNQ46.

taion avatar Jan 22 '16 13:01 taion

I had a similar problem, and since I couldn't find an answer on StackOverflow, I'm going to post it here since this issue shows up when you search for this error.

Using relative URLs fixed this issue for me. Change replace('/') to replace('./').

imontiel avatar Mar 23 '16 17:03 imontiel

@thinklinux i am getting same problem with you. Did you find solution in somewhere by any chance?

nindavidw avatar Jun 16 '16 10:06 nindavidw

I solved it by removing from my html file.

It depends on history npm module, and the error is from node_modules/history/lib/createBrowserHistory.js line 120.

window.history.replaceState(historyState, null, path); As long as i have <base href='/' /> in index.html, the 3rd parameter would be // which occurs error message above. (Uncaught SecurityError: Failed to execute 'replaceState' on 'History':)

Once I remove <base href='/' /> from my index.html, i don't see this error.

nindavidw avatar Jun 16 '16 14:06 nindavidw