简体   繁体   中英

In Meteor how to render the templates on server side?

I followed this tutorial and I built this with it: http://x111.meteor.com/ But as you can see the loading is very slow, because meteor loads the data from client side.

I get this error when I want to use the global Template inside Meteor.isServer :

ReferenceError: Template is not defined
at app/products.js:56:3 ...

How do I serve the templates from server side so I don't have to wait for the client?

Try this . This package adds support for server side Handlebars in Meteor. It's primarily intended as a stop gap for server side email html until Meteor releases server side rendering support.

> mrt add handlebars-server

The templates are served up with the server even though the code is in the client html. The reason they take long is the step for the meteor collections to download to the client on the first load.

Update: I know this isn't what you asked for exactly, but the root cause of the lag isn't the Templating system.

The core issue would be the latency between your browser & the server . You need to place the server closer to you to remove this lag / make it shorter as would be with any web server.

If you can't get a server closer you could display a loading... message so users are aware the data will be available shortly.

{{#unless CartItems.count}}
    <tr>
        <td colspan="4">Loading...</td>
    </tr>
{{else}}
    {{#each CartItems}} 
        <tr>
           <td>{{Name}}</td>
           <td>${{Price}}</td>
           <td>{{Quantity}}</td>
           <td>${{Total}}</td>
        </tr>
     {{/each}}
{{/unless}}

Currently there is no built-in way to do this, but adding serverside template rendering is a planned feature (though I don't see it on the Roadmap yet):

This version of spiderable is specifically for search engines. A future version of Meteor will also send HTML to web browsers on inital page load. The Meteor templating system was designed specifically to support this use case.

( http://meteor.com/faq/can-meteor-serve-static-html )

While people will say this violates the "only send data over the wire" philosophy, for sites that need good SEO it's pretty necessary, and also a very natural thing to do when you have the same template language and framework available to both the server and client.

如果您在Node.JS框架中寻找这种类型的功能,我建议使用Derby.JS http://derbyjs.com/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM