A better way to do SSR with blaze in meteor than meteorhacks:SSR?

The answer for me is to remove the old package meteorhacks:ssr and do what it did using the new package organizations. It requires meteor add spacebars-compiler (Blaze is already present in the server runtime). It also requires eval but I don’t see anyway around it.

import { SpacebarsCompiler } from "meteor/spacebars-compiler";
import { Blaze } from "meteor/blaze";

export const SSR = {
    render: (templateName, data) => {
        const renderFunc = (data)? Blaze.toHTMLWithData : Blaze.toHTML;
        const template = Blaze.Template[templateName];
        if (!template) {
            throw new Error(`Template ${templateName} not found`);
        }
        else {
            return renderFunc(template, data);
        }
    },
    compileTemplate: (name, content) => {
        const renderfunc = eval(`(function(view) { return ${SpacebarsCompiler.compile(content)}(); })`);
        const template = new Blaze.Template(name, function() { return renderfunc(this); });
        Blaze.Template[name] = template;
        return template;
    }
};
8 Likes