简体   繁体   中英

React router base url not working

I've got this piece of code as a router for my React 15.0.1 universal app. React router version is 2.3.0.

import React from 'react';
import { createHistory } from 'history';
import { Router, Route, IndexRoute, Redirect, useRouterHistory } from 'react-router';

import MainLayout from './../views/layout/mainLayout.jsx';
import Home from './../views/home.jsx';
import About from './../views/about.jsx';

import Error404 from './../views/404.jsx';

const browserHistory = useRouterHistory(createHistory)({
    basename: '/example-url'
});

module.exports = (
  <Router history={browserHistory}>
    <Route path='/' component={MainLayout}>
      <IndexRoute component={Home} />
      <Route path='about/' component={About} />
      <Route path='*' component={Error404} />
    </Route>
  </Router>

The problem is, that I'm getting the following error:

Invariant Violation: Browser history needs a DOM

It seems that this solution doesn't work for my server side routing, as I'm trying to set a basename for my routes.

If I forget about the basename, then everything is fine but my application doesn't behave like a spa (I believe that routes on client side are not found).

Here is some of my server side entry point (app.js):

import routes from './client/routers/routes.jsx';

const engine = ReactEngine.server.create({
  routes: routes,
  routesFilePath: path.join(__dirname, 'client/routers/routes.jsx'),
});

app.engine('.jsx', engine);
app.set('views', __dirname + '/client/views');
app.set('view engine', 'jsx');
app.set('view', ReactEngine.expressView);

app.get('*', function(req, res) {
  res.render(req.url, { metadata: res.data.metadata,baseUrl: res.data.baseUrl, url: res.data.url });  
});

Could anybody help please? I have read through the docs and supposedly this is the right way to do it and many people seem to support the solution but I'm stuck here.

Thank you very much

what's your history version? if your react-router version is v2/v3, history version should be v3, v4 is for react-router v4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM