简体   繁体   English

服务器端渲染+反应路由器重定向

[英]Server side rendering + react router redirect

I have a higher order component (applied by wrapAuthComponent below) which wraps any components requiring authentication. 我有一个更高阶的组件(由下面的wrapAuthComponent应用),它包装任何需要身份验证的组件。 This component checks if the user is logged in, and if not, redirects: 此组件检查用户是否已登录,如果没有,则重定向:

let next = this.props.location.pathname;
this.context.router.push({
    pathname: '/login',
    query: {...this.props.location.query, next}
});

Separately, I have a nav bar component which allows the user to login: 另外,我有一个导航栏组件,允许用户登录:

<IndexLinkContainer to={`/login? next=${this.props.pathname}`}>
    <NavItem className='nav-bar-button'> Login </NavItem>
</IndexLinkContainer>

(the pathname prop above is passed by the App container) (上面的pathname prop由App容器传递)

So, to recap, my routes are drawn like this: 所以,回顾一下,我的路线是这样绘制的:

<Route component={App} /* app contains the above navbar */ path="/">
    <Route component={LoginPage} path="/login" />
    <Route component={wrapAuthComponent(Foo)} path="/foo" />
</Route>

Now, onto the bug. 现在,进入bug。 When I go to /foo and am not logged in, I get the following error: 当我去/foo并且我没有登录时,我收到以下错误:

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 compensate 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)  href="/login?next=/login" data-reactid=
 (server)  href="/login?next=/foo" data-reactid="

What am I doing wrong here? 我在这做错了什么?

Depending on your version of Node.js and React, this GitHub issue may relate to your problem. 根据您的Node.js和React版本, 此GitHub问题可能与您的问题有关。

Taken from these React docs: 取自这些React文档:

Some versions of Node.js have an Object.assign implementation that does not preserve key order. 某些版本的Node.js具有Object.assign实现,该实现不保留键顺序。 This can cause errors when validating the markup, creating a warning that says "React attempted to reuse markup in a container but the checksum was invalid". 这可能会在验证标记时导致错误,并创建一条警告,显示“React尝试在容器中重用标记,但校验和无效”。 If you run into this issue, you can override Object.assign to use a polyfill that preserves key order. 如果遇到此问题,可以覆盖Object.assign以使用保留键顺序的polyfill。

Installing the object-assign package and setting the global Object.assign as follows may fix your problem: 安装object-assign包并按如下方式设置全局Object.assign可能会解决您的问题:

Object.assign = require('object-assign');

If not, I think I'd need to see some more of your code before being able to determine the problem. 如果没有,我想在确定问题之前我需要查看更多代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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