Meteor SSR complete example

Hello! couldn’t manage to get working SSR with the available documentation, any examples appreciate it.

Currently I’m not able to make SSR render on the server.

client\main.js
import { Meteor } from ‘meteor/meteor’;
import { Template } from ‘meteor/templating’;
import { ReactiveVar } from ‘meteor/reactive-var’;
import {SSR} from ‘meteor/meteorhacks:ssr’;
import ‘./main.html’;
if (Meteor.isServer) {
SSR.compileTemplate(‘body’, Assets.getText(‘main.html’));
}else{
console.log(‘client’,Template);
console.log(‘client’,Template.body);
}

Template.body.helpers({
    getConfirmationURL: function() {
        return this.url;
    }
});
//SSR.compileTemplate('body', Assets.getText('main.html'));

server/main.js

import { Meteor } from 'meteor/meteor';
import {SSR , Template } from 'meteor/meteorhacks:ssr';

Meteor.startup(() => {
  // code to run on server at startup
});
Meteor.methods({
});
var html = SSR.render("body", {username: "arunoda"});

Thank you in advance.

1 Like

To be able to run method both from client and server:

sendEmail.call({to, from, subject, doc}, function (error) {
  if (error) {
    console.error('sendEmail error:', error.message);
    return false;
  }
});

I have this code structure:

/server/main.js:

import '/imports/startup/server';
import '/imports/startup/both';

/client/main.js:

import '/imports/startup/client';
import '/imports/startup/both';

/imports/startup/both/index.js:

import '/imports/api/email_methods';

/imports/api/email_methods.js:

import { ValidatedMethod } from 'meteor/mdg:validated-method';
import { SimpleSchema } from 'meteor/aldeed:simple-schema';
import { Email } from 'meteor/email';
import { SSR, Template } from 'meteor/meteorhacks:ssr';
...

export const sendEmail = new ValidatedMethod({
  name: 'sendEmail',
  validate: new SimpleSchema({
    to: {type: String, regEx: SimpleSchema.RegEx.Email},
    from: {type: String, regEx: SimpleSchema.RegEx.Email},
    subject: {type: String},
    doc: {type: Object, blackbox: true}
  }).validator(),
  run({to, from, subject, doc}) {
    if (Meteor.isServer) {
      SSR.compileTemplate('htmlEmail', Assets.getText('templates/email.html'));

      Template.htmlEmail.helpers({
        numeral(num) {
          if (num) { return numeral(num).format(); }
        }
      });

      Email.send({to, from, subject, html: SSR.render('htmlEmail', doc)});
    }
  }
});

Thank you zzs! will give it a try!