我有一個高階組件,用於檢查用戶是否已通過身份驗證,如果沒有,則會重定向到其他網址。React路由器不在服務器上重定向
它是一個同構的應用程序,它適用於客戶端,但如果我關閉JS,服務器不會重定向。
if (!this.props.authenticated) {
this.context.router.push('/');
}
我可以在服務器上打這一說法,並this.context.router
逐漸復甦,但沒有任何反應。
全部組件:
export default (
<Route path='/' component={App}>
<IndexRoute component={Welcome} />
<Route path='/feature' component={requireAuth(Feature)} />
<Route path='/signin' component={Signin} />
<Route path='/signout' component={Signout} />
<Route path='/signup' component={Signup} />
</Route>
);
這是在服務器渲染代碼:
import { RouterContext, match } from 'react-router';
import { Provider } from 'react-redux';
import { renderToString } from 'react-dom/server';
import React from 'react';
import reactCookie from 'react-cookie';
import configureStore from '../../shared/store';
import routes from '../../shared/routes';
import assets from '../../public/assets.json';
import { AUTH_USER } from '../../shared/actions/types';
export default function (req, res) {
match({ routes, location: req.url }, (error, redirectLocation, renderProps) => {
if (error) return res.status(500).send(error.message);
if (redirectLocation) return res.redirect(302, redirectLocation.pathname + redirectLocation.search);
if (!renderProps) return res.status(404).send('Page not found');
const store = configureStore();
// use cookies to retrieve token
const unplug = reactCookie.plugToRequest(req, res); // eslint-disable-line no-unused-vars
const token = reactCookie.load('token');
// if we have a token, consider the user to be signed in
if (token) {
// we need to update application state
store.dispatch({ type: AUTH_USER });
}
const content = renderToString(
<Provider store={store}>
<RouterContext {...renderProps} />
</Provider>
);
const initialState = JSON.stringify(store.getState());
return res.render('index', { content, assets, initialState });
});
}
你已經把JS變成了「關閉」。你如何期待它的工作? – activatedgeek
它是一個同構的應用程序使用node.js – Paul