学习《3-7 服务端渲染优化》的时候,引入了 react-async-bootstrapper
, 运行后报错了。
// dev-static.js
// ...
module.exports = function(server) {
server.use(
"/public",
proxy({
target: STATIC_HOST
})
);
server.use("*", (req, res) => {
getTemplate()
.then(response => {
let template = response.data;
let url = req.originalUrl;
let context = {};
let stores = createStoreMap ? createStoreMap() : {};
let renderResult = "";
if (serverBundle) {
let app = serverBundle(url, context, stores);
bootstrapper(app).then(() => {
renderResult = ReactDOMServer.renderToString(app);
console.log("打印count");
console.log(JSON.stringify(stores.appState.count));
if (context.url) {
res.status(302).setHeader("Location", context.url);
res.end();
} else {
res.send(template.replace("<!-- app -->", renderResult));
}
});
} else {
res.send(template);
}
})
.catch(err => {
console.error(err);
});
});
};
Warning: Rendering <Context.Consumer.Provider> is not supported and will be removed in a future major release. Did you mean to render <Context.Provider> instead?
Warning: Rendering <Context.Consumer.Consumer> is not supported and will be removed in a future major release. Did you mean to render <Context.Consumer> instead?
(node:4720) UnhandledPromiseRejectionWarning: Error: Invariant failed: You should not use <Link> outside a <Router>
at invariant (webpack-internal:///./node_modules/tiny-invariant/dist/tiny-invariant.esm.js:12:11)
at Object.eval [as children] (webpack-internal:///./node_modules/react-router-dom/esm/react-router-dom.js:187:88)
at recursive (G:\front_end\react-ser\node_modules\react-tree-walker\dist\react-tree-walker.js:129:41)
at G:\front_end\react-ser\node_modules\react-tree-walker\dist\react-tree-walker.js:153:26
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:182:7)
(node:4720) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:4720) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.