我试图在基本的Meteor应用程序中使用IronRouter和Meteor-SSR进行服务器端渲染。当我在浏览器中加载/home
时,出现以下错误:
Error: Can't render undefined
at checkRenderContent (packages/blaze.js:702:11)
at contentAsView (packages/blaze.js:724:3)
at Blaze.toHTML (packages/blaze.js:851:40)
at Object.SSR.render (packages/meteorhacks_ssr.js:208:10)
at [object Object].Router.route.where (main.js:9:20)
at boundNext (packages/iron_middleware-stack.js:408:31)
at runWithEnvironment (packages/meteor.js:1176:24)
at packages/meteor.js:1189:14
at [object Object].urlencodedParser (/Users/roger/.meteor/packages/iron_router/.1.1.1.1q7cd8x++os+web.browser+web.cordova/npm/node_modules1/body-parser/lib/types/urlencoded.js:84:40)
at packages/iron_router.js:886:36
以下是该应用的代码:
// main.js
if (Meteor.isServer) {
Router.route('/home', function() {
let html = SSR.render('home');
this.response.end(html);
}, {where: 'server'});
}
if (Meteor.isClient) {
Router.route('/home', function() {
this.render("home")
});
}
这是main.html
:
<head>
<title>SSR Test</title>
</head>
<body>
</body>
<template name="home">
Home
</template>
答案 0 :(得分:1)
这是因为您的home
模板未在服务器上定义。要使用SSR
包,您需要首先使用SSR.compileTemplate
在服务器中编译模板,然后才能使用SSR.render
进行渲染。这是一个简单的例子:
if (Meteor.isServer) {
const template = 'Hello {{username}}, <br> Now time is: {{time}}';
SSR.compileTemplate('hello', template);
Router.route('/home', function() {
const html = SSR.render('hello', {
username: 'foo',
time: new Date(),
});
this.response.end(html);
}, {where: 'server'});
}
还可以从服务器上的静态文件中编译模板,这可以找到here