React Router / Hapi 服务器端渲染错误
React Router / Hapi Server Side Rendering Error
我已经为此苦苦挣扎了一段时间,之前也能正常工作,但莫名其妙地又坏了,很明显根本原因还没有解决。
React 路由器 v: 2.0.0-rc4
问题:加载页面时,服务器returns出现以下错误。
Warning: Failed propType: Required prop `router` was not specified in `RouterContext`.
Warning: Failed propType: Required prop `location` was not specified in `RouterContext`.
Warning: Failed propType: Required prop `routes` was not specified in `RouterContext`.
Warning: Failed propType: Required prop `params` was not specified in `RouterContext`.
Warning: Failed propType: Required prop `components` was not specified in `RouterContext`.
Warning: [react-router] `<RouterContext>` expects a `router` rather than a `history`
页面加载正常,客户端路由似乎工作正常。
来自 Server.js 的相关片段:
import routeConfig from './../common/routes/Routes.js';
const handleRender = function(req, res) {
const initialState = {
profile: {
name: 'Bob',
age: 10
},
messages: []
}
const createStoreWithMiddleware = applyMiddleware( thunkMiddleware)(createStore);
const store = createStoreWithMiddleware(reducer(initialState));
match({routes: routeConfig, location: req.url}, (error, redirectLocation, renderProps) => {
// res(req.url);
if(error) {
res('error' + error.message);
}
else {
res(renderProps);
const html = renderToString(
<Provider store={store}>
<RouterContext {...renderProps} />
</Provider>
);
//const initialState = store.getState();
//res(renderFullPage(html, initialState));
}
});
}
这是从 Routes.js
导出的内容
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import { Route } from 'react-router';
//Components
import App from './../components/App.jsx';
import Name from './../components/Name.jsx';
import Profile from './../components/Profile.jsx';
import Messages from './../components/Messages.jsx';
const routeConfig = [
{
path: '/',
component: App,
childRoutes: [
{path: 'test', component: Name},
{path: 'profile', component: Profile},
{path: 'messages', component: Messages}
]
}
];
export default routeConfig;
当我转储 renderprops 时,这就是我得到的
{
routes: [
{
path: "/",
childRoutes: [
{
path: "test"
},
{
path: "profile"
},
{
path: "messages"
}
]
},
{
path: "test"
}
],
params: { },
location: {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: null,
query: { },
pathname: "/test",
path: "/test",
href: "/test"
},
components: [
null,
null
],
history: {
__v2_compatible__: true
},
router: {
__v2_compatible__: true
}
}
所以它似乎永远不会匹配组件。也许我传递的 req.url 不正确?但是我找不到任何 react-router 文档来准确指示该参数应该是什么样子。
把这个留在这里以防其他人遇到这种愚蠢的事情。
在通过 Good 启用更强大的错误日志记录后,我意识到这个错误实际上是关于对 /favicon.ico 的请求,我的路由没有处理它,并且正在落入我的反应路由中.
我犯了一个非常愚蠢的错误,而且由于我没有经验需要 handle/log 在 Hapi 中出错。
如果你像我一样,按照 Github 上的 reactjs react-router-tutorial 来到这里(如果没有,请去看看),这些错误消息可能会显示,因为你没有描述任何替代方案在路线中寻找路线。如果它只出现在未定义的路线上(尝试在 localhost:8080/ 之后随机输入一些内容),请帮自己一个忙并阅读 this 到最后。
此外,在服务器的 app.get 调用中插入 console.log('req.url: ', req.url) 以查看导致错误的原因。
此外 II:在匹配函数之外声明 appHTML 以确保它在 res.send() 中存活。
我已经为此苦苦挣扎了一段时间,之前也能正常工作,但莫名其妙地又坏了,很明显根本原因还没有解决。
React 路由器 v: 2.0.0-rc4
问题:加载页面时,服务器returns出现以下错误。
Warning: Failed propType: Required prop `router` was not specified in `RouterContext`.
Warning: Failed propType: Required prop `location` was not specified in `RouterContext`.
Warning: Failed propType: Required prop `routes` was not specified in `RouterContext`.
Warning: Failed propType: Required prop `params` was not specified in `RouterContext`.
Warning: Failed propType: Required prop `components` was not specified in `RouterContext`.
Warning: [react-router] `<RouterContext>` expects a `router` rather than a `history`
页面加载正常,客户端路由似乎工作正常。
来自 Server.js 的相关片段:
import routeConfig from './../common/routes/Routes.js';
const handleRender = function(req, res) {
const initialState = {
profile: {
name: 'Bob',
age: 10
},
messages: []
}
const createStoreWithMiddleware = applyMiddleware( thunkMiddleware)(createStore);
const store = createStoreWithMiddleware(reducer(initialState));
match({routes: routeConfig, location: req.url}, (error, redirectLocation, renderProps) => {
// res(req.url);
if(error) {
res('error' + error.message);
}
else {
res(renderProps);
const html = renderToString(
<Provider store={store}>
<RouterContext {...renderProps} />
</Provider>
);
//const initialState = store.getState();
//res(renderFullPage(html, initialState));
}
});
}
这是从 Routes.js
导出的内容import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import { Route } from 'react-router';
//Components
import App from './../components/App.jsx';
import Name from './../components/Name.jsx';
import Profile from './../components/Profile.jsx';
import Messages from './../components/Messages.jsx';
const routeConfig = [
{
path: '/',
component: App,
childRoutes: [
{path: 'test', component: Name},
{path: 'profile', component: Profile},
{path: 'messages', component: Messages}
]
}
];
export default routeConfig;
当我转储 renderprops 时,这就是我得到的
{
routes: [
{
path: "/",
childRoutes: [
{
path: "test"
},
{
path: "profile"
},
{
path: "messages"
}
]
},
{
path: "test"
}
],
params: { },
location: {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: null,
query: { },
pathname: "/test",
path: "/test",
href: "/test"
},
components: [
null,
null
],
history: {
__v2_compatible__: true
},
router: {
__v2_compatible__: true
}
}
所以它似乎永远不会匹配组件。也许我传递的 req.url 不正确?但是我找不到任何 react-router 文档来准确指示该参数应该是什么样子。
把这个留在这里以防其他人遇到这种愚蠢的事情。
在通过 Good 启用更强大的错误日志记录后,我意识到这个错误实际上是关于对 /favicon.ico 的请求,我的路由没有处理它,并且正在落入我的反应路由中.
我犯了一个非常愚蠢的错误,而且由于我没有经验需要 handle/log 在 Hapi 中出错。
如果你像我一样,按照 Github 上的 reactjs react-router-tutorial 来到这里(如果没有,请去看看),这些错误消息可能会显示,因为你没有描述任何替代方案在路线中寻找路线。如果它只出现在未定义的路线上(尝试在 localhost:8080/ 之后随机输入一些内容),请帮自己一个忙并阅读 this 到最后。
此外,在服务器的 app.get 调用中插入 console.log('req.url: ', req.url) 以查看导致错误的原因。
此外 II:在匹配函数之外声明 appHTML 以确保它在 res.send() 中存活。