From cdf6a10490e540069336c2c17dcb20f88cd08750 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Thu, 7 Sep 2017 11:59:02 +0100 Subject: [PATCH] ghost_head improvements (#8983) no issue - Added debug statements to ghost_head - useful for determining how much render time is spent in ghost head - Make promises more readable - Used join instead of props for less code --- core/server/helpers/ghost_head.js | 156 +++++++++++++++--------------- 1 file changed, 79 insertions(+), 77 deletions(-) diff --git a/core/server/helpers/ghost_head.js b/core/server/helpers/ghost_head.js index bcfdd00d0ed..8c7d7f5af48 100644 --- a/core/server/helpers/ghost_head.js +++ b/core/server/helpers/ghost_head.js @@ -9,6 +9,7 @@ var proxy = require('./proxy'), _ = require('lodash'), Promise = require('bluebird'), + debug = require('ghost-ignition').debug('ghost_head'), getMetaData = proxy.metaData.get, getAssetUrl = proxy.metaData.getAssetUrl, @@ -24,18 +25,20 @@ var proxy = require('./proxy'), function getClient() { if (labs.isSet('publicAPI') === true) { - return api.clients.read({slug: 'ghost-frontend'}).then(function (client) { - client = client.clients[0]; - - if (client.status === 'enabled') { - return { - id: client.slug, - secret: client.secret - }; - } + return api.clients + .read({slug: 'ghost-frontend'}) + .then(function handleClient(client) { + client = client.clients[0]; + + if (client.status === 'enabled') { + return { + id: client.slug, + secret: client.secret + }; + } - return {}; - }); + return {}; + }); } return Promise.resolve({}); } @@ -80,99 +83,98 @@ function getAjaxHelper(clientId, clientSecret) { } module.exports = function ghost_head(options) { + debug('begin'); // if server error page do nothing if (this.statusCode >= 500) { return; } - var metaData, - client, - head = [], + var head = [], globalCodeinjection = settingsCache.get('ghost_head'), postCodeInjection = options.data.root && options.data.root.post ? options.data.root.post.codeinjection_head : null, context = this.context ? this.context : null, useStructuredData = !config.isPrivacyDisabled('useStructuredData'), safeVersion = this.safeVersion, referrerPolicy = config.get('referrerPolicy') ? config.get('referrerPolicy') : 'no-referrer-when-downgrade', - fetch = { - metaData: getMetaData(this, options.data.root), - client: getClient() - }, favicon = blogIconUtils.getIconUrl(), iconType = blogIconUtils.getIconType(favicon); - return Promise.props(fetch).then(function (response) { - client = response.client; - metaData = response.metaData; + debug('preparation complete, begin fetch'); + return Promise + .join(getMetaData(this, options.data.root), getClient(), function handleData(metaData, client) { + debug('end fetch'); - if (context) { - // head is our main array that holds our meta data - if (metaData.metaDescription && metaData.metaDescription.length > 0) { - head.push(''); - } + if (context) { + // head is our main array that holds our meta data + if (metaData.metaDescription && metaData.metaDescription.length > 0) { + head.push(''); + } - head.push(''); - head.push(''); - head.push(''); + head.push(''); + head.push(''); + head.push(''); - // show amp link in post when 1. we are not on the amp page and 2. amp is enabled - if (_.includes(context, 'post') && !_.includes(context, 'amp') && settingsCache.get('amp')) { - head.push(''); - } + // show amp link in post when 1. we are not on the amp page and 2. amp is enabled + if (_.includes(context, 'post') && !_.includes(context, 'amp') && settingsCache.get('amp')) { + head.push(''); + } - if (metaData.previousUrl) { - head.push(''); - } + if (metaData.previousUrl) { + head.push(''); + } - if (metaData.nextUrl) { - head.push(''); - } + if (metaData.nextUrl) { + head.push(''); + } - if (!_.includes(context, 'paged') && useStructuredData) { - head.push(''); - head.push.apply(head, finaliseStructuredData(metaData)); - head.push(''); + if (!_.includes(context, 'paged') && useStructuredData) { + head.push(''); + head.push.apply(head, finaliseStructuredData(metaData)); + head.push(''); - if (metaData.schema) { - head.push('\n'); + if (metaData.schema) { + head.push('\n'); + } } - } - if (client && client.id && client.secret && !_.includes(context, 'amp')) { - head.push(getAjaxHelper(client.id, client.secret)); + if (client && client.id && client.secret && !_.includes(context, 'amp')) { + head.push(getAjaxHelper(client.id, client.secret)); + } } - } - head.push(''); + head.push(''); - head.push(''); + head.push(''); - // no code injection for amp context!!! - if (!_.includes(context, 'amp')) { - if (!_.isEmpty(globalCodeinjection)) { - head.push(globalCodeinjection); - } + // no code injection for amp context!!! + if (!_.includes(context, 'amp')) { + if (!_.isEmpty(globalCodeinjection)) { + head.push(globalCodeinjection); + } - if (!_.isEmpty(postCodeInjection)) { - head.push(postCodeInjection); + if (!_.isEmpty(postCodeInjection)) { + head.push(postCodeInjection); + } } - } - return filters.doFilter('ghost_head', head); - }).then(function (head) { - return new SafeString(head.join('\n ').trim()); - }).catch(function handleError(err) { - logging.error(err); - - // Return what we have so far (currently nothing) - return new SafeString(head.join('\n ').trim()); - }); + return filters.doFilter('ghost_head', head); + }) + .then(function afterFilters(head) { + debug('end'); + return new SafeString(head.join('\n ').trim()); + }) + .catch(function handleError(err) { + logging.error(err); + + // Return what we have so far (currently nothing) + return new SafeString(head.join('\n ').trim()); + }); };