我正在尝试实现meteor-act服务器端渲染。 我已经删除了templating
包并添加了static-html
包,就像在 meteor 中使用react
for ui 时一样。
现在的问题是,当我使用任何服务器端渲染实现运行该应用程序时,它可以正常工作。这意味着如果我查看页面源代码,我会看到我的裸露标记和我的呈现目标div。 但是当我实现服务器渲染代码时,它会给出此错误
'''
Error: Cannot find package "templating". Try "meteor add templating".
at makeInstallerOptions.fallback (packages/modules-runtime.js:597:13)
at Module.require (packages/modules-runtime.js:230:14)
at require (packages/modules-runtime.js:244:21)
at AccountsUIWrapper.js (imports/ui/AccountsUIWrapper.js:1:250)
at fileEvaluate (packages/modules-runtime.js:322:7)
at Module.require (packages/modules-runtime.js:224:14)
at require (packages/modules-runtime.js:244:21)
at App.js (imports/ui/App.js:1:386)
at fileEvaluate (packages/modules-runtime.js:322:7)
at Module.require (packages/modules-runtime.js:224:14)
at require (packages/modules-runtime.js:244:21)
at routes.js (imports/startup/routes.js:1:87)
at fileEvaluate (packages/modules-runtime.js:322:7)
at Module.require (packages/modules-runtime.js:224:14)
at require (packages/modules-runtime.js:244:21)
at main.js (server/main.js:1:558)
at fileEvaluate (packages/modules-runtime.js:322:7)
at Module.require (packages/modules-runtime.js:224:14)
at require (packages/modules-runtime.js:244:21)
at /Users/kenshinman/Desktop/projects/simple-todos/.meteor/local/build/programs/server/app/app.js:463:15
at /Users/kenshinman/Desktop/projects/simple-todos/.meteor/local/build/programs/server/boot.js:411:36
at Array.forEach (<anonymous>)
=> 退出代码:1 => 您的应用程序正在崩溃。等待文件更改。
'''
这是我的server/main.js code
import { Tasks } from "../imports/api/tasks";
import React from "react";
import { Meteor } from "meteor/meteor";
import { onPageLoad } from "meteor/server-render";
import { StaticRouter, Switch } from "react-router-dom";
import { renderToString } from "react-dom/server";
import Routes from "../imports/startup/routes";
const siteName = "Todos";
const defaultImage = "https://loremflickr.com/320/240/baby";
const defaultMetaTags = `
<title>${siteName}</title>
<meta property="og:title" content="${siteName}" />
<meta property="og:description" content="All your todos synced wherever you happen to be" />
<meta property="og:image" content="${defaultImage}" />
`;
function createMetaTag(property, content) {
return `<meta property="${property}" content="${content}">`;
}
onPageLoad(sink => {
const { pathname } = sink.request.url;
const meteorHost = Meteor.absoluteUrl();
const App = props => {
console.log(props);
return (
<StaticRouter location={props.location} context={{}}>
<Switch>
<Routes />
</Switch>
</StaticRouter>
);
};
const content = renderToString(<App location={sink.request.url} />);
sink.renderIntoElementById("render-target", content);
const taskId = "asdlkjhfsda";
const task = taskId ? Tasks.findOne({ _id: "listId" }) : null;
// task count
if (task) {
const title = "task.text";
const description = `This list contains 8 incomplete tasks`;
const fullUrl = meteorHost + pathname.replace(/^/+/g, "");
sink.appendToHead(createMetaTag("og:title", title));
sink.appendToHead(createMetaTag("og:description", description));
sink.appendToHead(createMetaTag("og:url", fullUrl));
sink.appendToHead(createMetaTag("og:image", defaultImage));
sink.appendToHead(createMetaTag("og:site_name", siteName));
} else {
sink.appendToHead(defaultMetaTags);
sink.appendToHead(createMetaTag("og:url", meteorHost));
}
});
但是当我评论这行时import Routes from "../imports/startup/routes";
和路线的所有外观,一切再次正常。
这是路由文件代码
import React from "react";
import App from "../ui/App";
import { Switch, Route } from "react-router-dom";
const Routes = () => (
<div>
<Route exact path="/" component={App} />
<Route exact path="/test" render={() => <h2>This is the test page</h2>} />
</div>
);
export default Routes;
我怎样才能完成这项工作并停止收到此错误?
谢谢。
已解决。忘记了我从帐户 UI 配置模板中的meteor/templating
导入Template
。它现在正在工作。