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

I use an old package called meteorhacks:ssr for generating html strings from templates for documents and email. It has worked well for years but if I update to the recent release, the ssr package breaks because SpacebarsCompiler is undefined. Apparently this used to be a variable in global scope that is now an export. I have two questions.

  1. Is there a more modern way to to do ssr in meteor using only blaze.
  2. When I try to rewrite the package and import the required variables, in a meteor server side js, it doesn’t work. i.e. import { SpacebarsCompiler } from “spacebars-compiler”. How can I import from these indirect dependencies or do I have to add the entire blaze universe as a direct dependency.

This app was generate 6 or 7 years ago

The dependencies from meteor list are below. I am assuming it gets blaze from iron or meteor indirectly somehow.


accounts-base                    1.8.0  A user account system
accounts-password                1.7.0  Password support for accounts
alanning:roles                   3.2.3  Authorization package for Meteor
check                            1.3.1  Check whether a value matches a pattern
dynamic-import                   0.6.0  Runtime support for Meteor 1.5 dynamic import(...) syntax
ecmascript                       0.15.0  Compiler plugin that supports ES2015+ in all .js files
email                            2.0.0  Send email messages
fourseven:scss                   4.12.0  Style with attitude. Sass and SCSS support for Meteor.js.
iron:router                      1.1.2  Routing specifically designed for Meteor
jquery                           3.0.0  Manipulate the DOM using CSS selectors
littledata:synced-cron           1.5.1  Allows you to define and run scheduled jobs across multiple servers.
meteorhacks:ssr                  2.2.0  Server Side Rendering for Meteor with Blaze
mrt:later                        1.6.1  Define schedules and calculate future or previous schedule occurrences.
percolate:find-from-publication  0.2.1  Enable finding all documents that have been published by a given publication
reactive-var                     1.0.11  Reactive variable
ros:publish-counts               0.5.1  Publish the count of a cursor, in real time.  Supports large collections.
shell-server                     0.5.0  Server-side component of the `meteor shell` command.
standard-app-packages            1.0.9  Moved to meteor-platform
standard-minifier-css            1.7.2  Standard css minifier used with Meteor apps by default.
standard-minifier-js             2.6.0  Standard javascript minifiers used with Meteor apps by default.

2 Likes

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;
    }
};
7 Likes