1

I'm configuring react-boilerplate to start a project on it.

Our dev environment publishes "dev" versions of the app in sub paths, for example: example.org/test/project-name

Then, when we publish the project, it stays on example.org/

I'd like to use the browser history (HTML5 API) and not the hash history. But this means that I have to set the basename of React Router to match the current environment.

Right now, I'm trying to set up a static basename in this way:

const browserHistory = useRouterHistory(useBasename(createHistory))({
  basename: '/test/project-name',
});
const store = configureStore(initialState, browserHistory);

But when I run the application, the homepage is still at / instead of /test/project-name/.


So, the questions are:

  1. What am I doing wrong?
  2. How would you make the basename match the current environment?
Fez Vrasta
  • 14,110
  • 21
  • 98
  • 160

1 Answers1

-1

The answer is in the react-router documentation:

Be aware that useRouterHistory already pre-enhances your history factory with the useQueries and useBasename enhancers from history.

import { useRouterHistory } from 'react-router'
import { createHistory } from 'history'

const history = useRouterHistory(createHistory)({
  basename: '/base-path'
})
Claus
  • 1,684
  • 14
  • 20
  • Read again my question please. – Fez Vrasta Jul 19 '16 at 20:23
  • Sorry this isn't helpful. I tried to use the useBasename() initially in several different ways as well and got the same behavior you describe, the basename being ignored. This fix worked for me. I assume you tried it as well and it didn't work for you. – Claus Jul 25 '16 at 15:43
  • 1
    In the code I provided in the question I'm already using `basename`, I need a way to set it dynamically based on my question requirements. – Fez Vrasta Jul 25 '16 at 16:20
  • From what I read you've tried to hardcode the basename to '/test/project-name' and that already failed. Once that's works (hint above) you can replace the hardcoded name with a configuration property variable. If you want the basename be set magically from wherever it's running then you're out of luck. But you could scrap the whole basename thing and use to wildcard redirect any url to your single page application. It works to run your app in one tomcat context without additional configuration. – Claus Jul 25 '16 at 20:28