From 7f001a4758207bf41d8fc3597f5277b6b8072d0e Mon Sep 17 00:00:00 2001 From: Enrique Benitez <3136873+bntzio@users.noreply.github.com> Date: Thu, 28 Oct 2021 14:10:53 -0500 Subject: [PATCH] Replaced moment with luxon in amp helper (#13683) refs: #13648 - We are replacing moment with luxon as it is now recommended. --- .../apps/amp/lib/helpers/amp_content.js | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/core/frontend/apps/amp/lib/helpers/amp_content.js b/core/frontend/apps/amp/lib/helpers/amp_content.js index 7aabd91266..68fd196397 100644 --- a/core/frontend/apps/amp/lib/helpers/amp_content.js +++ b/core/frontend/apps/amp/lib/helpers/amp_content.js @@ -8,7 +8,7 @@ // there if available. The cacheId is a combination of `updated_at` and the `slug`. const Promise = require('bluebird'); -const moment = require('moment'); +const {DateTime, Interval} = require('luxon'); const errors = require('@tryghost/errors'); const logging = require('@tryghost/logging'); @@ -119,14 +119,26 @@ function getAmperizeHTML(html, post) { } let Amperize = require('amperize'); - let startedAtMoment = moment(); amperize = amperize || new Amperize(); - if (!amperizeCache[post.id] || moment(new Date(amperizeCache[post.id].updated_at)).diff(new Date(post.updated_at)) < 0) { + const startedAtMoment = DateTime.now(); + + let cacheDateTime; + let postDateTime; + + if (amperizeCache[post.id]) { + const {updated_at: ampCacheUpdatedAt} = amperizeCache[post.id]; + const {updated_at: postUpdatedAt} = post; + + cacheDateTime = DateTime.fromJSDate(new Date(ampCacheUpdatedAt)); + postDateTime = DateTime.fromJSDate(new Date(postUpdatedAt)); + } + + if (!amperizeCache[post.id] || cacheDateTime.diff(postDateTime).valueOf() < 0) { return new Promise((resolve) => { amperize.parse(html, (err, res) => { - logging.info('amp.parse', post.url, moment().diff(startedAtMoment, 'ms') + 'ms'); + logging.info('amp.parse', post.url, Interval.fromDateTimes(startedAtMoment, DateTime.now()).length('milliseconds') + 'ms'); if (err) { if (err.src) {