Example of server-render with Blaze

Hello,

I have an application where I was rendering some pages with meteorhacks-ssr, but now I would like to simplify it by using this new package: https://github.com/meteor/meteor/blob/master/packages/server-render

I only found examples using React, or my application is using Blaze only. Does someone already have an example of server-render & Blaze working together? I need to render whole pages server-side, not just a single ID on the page. Thanks!

2 Likes

You just need to get Blaze to return the HTML. You might want to check out how this package works:

2 Likes

It may also be worth reading @benjamn’s reply on this subject:

2 Likes

Hello, thanks for the answer but I am still confused about how to use it inside even a simple app. I have for example this defined as a route (using iron-router):

Router.route('/:url', {

    name: 'post',
    data: function() {
        return Posts.findOne({ url: this.params.url });
    }

});

Then I am serving this in a very simple Blaze template:

<div class='row'>
    <div class='col-md-2'></div>
    <div class='col-md-8'>
        <div class='post-title single-post-title'>{{title}}</div>
    </div>
</div>
<div class='row'>
    <div class='col-md-2'></div>
    <div class='col-md-8'>
        <div class='post-content'>{{{content}}}</div>
    </div>
</div>

My question, where do I put the code to actually make sure that the template is rendered server side? This is the point where I am stuck. In the router? And then just send some HTML to the client? Or it’s something different? Thanks!

1 Like

I think the problem there is, Iron Router does not support server-render. However, if you place the following function on the server, you can see that server-render provides the URL in the sink function, and that can be used to determine what to display.

import { onPageLoad } from "meteor/server-render";

onPageLoad(function (sink) {
  console.log(sink)
});

Ok, from the documentation I really didn’t understand that there were some links to the router. So what router should I use to use server-render + Blaze?

I don’t think any routers support this functionality yet.

Thanks for posting this @msavin . Are there any benefits of using it vs. meteorhacks:ssr for server-side rendering of email templates? On first read, they seem pretty similar.