admin管理员组文章数量:1403221
I'm using react-router, React 0.14 and nginx to render a universal JS app. Because I'm in the middle of transitioning an existing app, I need to put the new 'react' code behind a url prefix, say /foo
However, I'd ideally like nginx config to handle the proxy_pass
to the react server running on a local port (say 8080).
nginx conf
location /foo/ {
proxy_pass http://localhost:8080/;
proxy_set_header Host $host;
}
React-router routes.jsx
import HomePage from 'ponents/pages/HomePage';
import AboutPage from 'ponents/pages/AboutPage';
import NotFoundPage from 'ponents/pages/NotFoundPage';
export default (
<Route path="/">
<IndexRoute ponent={HomePage} />
<Route path="about" ponent={AboutPage} />
<Route path="*" status={404} ponent={NotFoundPage} />
</Route>
);
Nothing special on the server. The server-side rendering seems to work fine, but when it gets to the client, since the path doesn't match up it shows the following Warning in the console:
Warning: React attempted to reuse markup in a container but the checksum was invalid. This generally means that you are using server rendering and the markup generated on the server was not what the client was expecting. React injected new markup to pensate which works but you have lost many of the benefits of server rendering. Instead, figure out why the markup being generated is different on the client or server:
(client) <div class="NotFoundPage" data-r
(server) <div class="HomePage" data-react
Which I guess makes sense since the URL is actually http://localhost:80801/foo
instead of http://localhost:8081/
which react-router is expecting on the client.
Any ideas of ways around this other than putting the /foo
prefix in the top-level Route? The reason I don't want to do that is I don't want to have /foo
prefixes everywhere (in the <Link />
ponents for example).
MTIA!
I'm using react-router, React 0.14 and nginx to render a universal JS app. Because I'm in the middle of transitioning an existing app, I need to put the new 'react' code behind a url prefix, say /foo
However, I'd ideally like nginx config to handle the proxy_pass
to the react server running on a local port (say 8080).
nginx conf
location /foo/ {
proxy_pass http://localhost:8080/;
proxy_set_header Host $host;
}
React-router routes.jsx
import HomePage from 'ponents/pages/HomePage';
import AboutPage from 'ponents/pages/AboutPage';
import NotFoundPage from 'ponents/pages/NotFoundPage';
export default (
<Route path="/">
<IndexRoute ponent={HomePage} />
<Route path="about" ponent={AboutPage} />
<Route path="*" status={404} ponent={NotFoundPage} />
</Route>
);
Nothing special on the server. The server-side rendering seems to work fine, but when it gets to the client, since the path doesn't match up it shows the following Warning in the console:
Warning: React attempted to reuse markup in a container but the checksum was invalid. This generally means that you are using server rendering and the markup generated on the server was not what the client was expecting. React injected new markup to pensate which works but you have lost many of the benefits of server rendering. Instead, figure out why the markup being generated is different on the client or server:
(client) <div class="NotFoundPage" data-r
(server) <div class="HomePage" data-react
Which I guess makes sense since the URL is actually http://localhost:80801/foo
instead of http://localhost:8081/
which react-router is expecting on the client.
Any ideas of ways around this other than putting the /foo
prefix in the top-level Route? The reason I don't want to do that is I don't want to have /foo
prefixes everywhere (in the <Link />
ponents for example).
MTIA!
Share Improve this question asked Dec 8, 2015 at 19:13 MuersMuers 3,2403 gold badges28 silver badges32 bronze badges1 Answer
Reset to default 7You can configure a baseURL
when setting up the history object.
import { createHistory, useBasename } from 'history'
const history = useBasename(createHistory)({
basename: '/foo'
})
本文标签: javascriptServer rendering React app behind a URL prefix with reactrouterStack Overflow
版权声明:本文标题:javascript - Server rendering React app behind a URL prefix with react-router - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744382291a2603564.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论