From 3a5c233122c333a0b7a3834092574d9b90df9d32 Mon Sep 17 00:00:00 2001 From: Simon Backx Date: Tue, 26 Sep 2023 16:25:03 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fixed=20staff=20emails=20eventua?= =?UTF-8?q?lly=20having=20invalid=20styles=20(#18363)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fixes https://github.com/TryGhost/Ghost/issues/17937 - We used a global Handlebars instance, which means it was reused across Ghost - Partials are different between parts of Ghost, that means the partials were overwritten every time a normal Mailgun email was send - All staff emails send after a normal newsletter would have invalid styles because the partials for styles were overwritten --- .../services/comments/CommentsServiceEmails.js | 2 +- ghost/email-service/lib/EmailRenderer.js | 2 +- ghost/staff-service/lib/StaffServiceEmails.js | 13 ++++++++----- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/ghost/core/core/server/services/comments/CommentsServiceEmails.js b/ghost/core/core/server/services/comments/CommentsServiceEmails.js index c5fbe9e784..252ec07473 100644 --- a/ghost/core/core/server/services/comments/CommentsServiceEmails.js +++ b/ghost/core/core/server/services/comments/CommentsServiceEmails.js @@ -15,7 +15,7 @@ class CommentsServiceEmails { this.urlService = urlService; this.urlUtils = urlUtils; - this.Handlebars = require('handlebars'); + this.Handlebars = require('handlebars').create(); } async notifyPostAuthors(comment) { diff --git a/ghost/email-service/lib/EmailRenderer.js b/ghost/email-service/lib/EmailRenderer.js index ad037c4b36..5c22ef0972 100644 --- a/ghost/email-service/lib/EmailRenderer.js +++ b/ghost/email-service/lib/EmailRenderer.js @@ -668,7 +668,7 @@ class EmailRenderer { } async renderTemplate(data) { - this.#handlebars = require('handlebars'); + this.#handlebars = require('handlebars').create(); // Helpers this.#handlebars.registerHelper('if', function (conditional, options) { diff --git a/ghost/staff-service/lib/StaffServiceEmails.js b/ghost/staff-service/lib/StaffServiceEmails.js index 75f1657d0b..516c84d31c 100644 --- a/ghost/staff-service/lib/StaffServiceEmails.js +++ b/ghost/staff-service/lib/StaffServiceEmails.js @@ -13,8 +13,9 @@ class StaffServiceEmails { this.urlUtils = urlUtils; this.labs = labs; - this.Handlebars = require('handlebars'); + this.Handlebars = require('handlebars').create(); this.registerPartials(); + this.registerHelpers(); } async notifyFreeMemberSignup({ @@ -478,10 +479,7 @@ class StaffServiceEmails { }); } - async renderHTML(templateName, data) { - const htmlTemplateSource = await fs.readFile(path.join(__dirname, './email-templates/', `${templateName}.hbs`), 'utf8'); - const htmlTemplate = this.Handlebars.compile(Buffer.from(htmlTemplateSource).toString()); - + registerHelpers() { this.Handlebars.registerHelper('eq', function (arg, value, options) { if (arg === value) { return options.fn(this); @@ -496,6 +494,11 @@ class StaffServiceEmails { } return array.slice(0,limit); }); + } + + async renderHTML(templateName, data) { + const htmlTemplateSource = await fs.readFile(path.join(__dirname, './email-templates/', `${templateName}.hbs`), 'utf8'); + const htmlTemplate = this.Handlebars.compile(Buffer.from(htmlTemplateSource).toString()); let sharedData = {}; if (data.recipient) {