javascript - Use basename in react-router on server side -
i'm serving site /clientpanel
directory on server. url http://xxx.yy/clientpanel
. client-side code:
const history = userouterhistory(createhistory)({ basename: "/clientpanel" }); render( <router history={history} routes={routes}/>, document.getelementbyid("root") );
on client-side works fine. urls relative /clientpanel
, have problem on how work on server. server-side code:
const history = userouterhistory(creatememoryhistory)({ basename: "/clientpanel" }); match({ routes, location: req.url, history}, (error, redirectlocation, renderprops) => { if (renderprops) { const html = rendertostring( <routercontext {...renderprops}/> ); res.send(renderfullpage(html)) } });
on first page load need omit /clientpanel
in url work, on client-side after clicking first <link>
/clientpanel
added begining. how make works consistent on both client , server side?
i got work following code:
const basename = '/foo' const location = req.url.replace(basename, '') // creatememoryhistory history package const history = userouterhistory(() => creatememoryhistory(location))({ basename }) match({ history, routes, location }, (error, redirectlocation, renderprops) => {
edit
even better:
const basename = '/foo' const location = req.url.replace(basename, '') // creatememoryhistory react-router package const history = creatememoryhistory({ entries: [location], basename }) match({ history, routes, location }, (error, redirectlocation, renderprops) => {
Comments
Post a Comment