mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
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
This commit is contained in:
parent
c64c56f1dc
commit
cdf6a10490
1 changed files with 81 additions and 79 deletions
|
@ -9,6 +9,7 @@
|
||||||
var proxy = require('./proxy'),
|
var proxy = require('./proxy'),
|
||||||
_ = require('lodash'),
|
_ = require('lodash'),
|
||||||
Promise = require('bluebird'),
|
Promise = require('bluebird'),
|
||||||
|
debug = require('ghost-ignition').debug('ghost_head'),
|
||||||
|
|
||||||
getMetaData = proxy.metaData.get,
|
getMetaData = proxy.metaData.get,
|
||||||
getAssetUrl = proxy.metaData.getAssetUrl,
|
getAssetUrl = proxy.metaData.getAssetUrl,
|
||||||
|
@ -24,18 +25,20 @@ var proxy = require('./proxy'),
|
||||||
|
|
||||||
function getClient() {
|
function getClient() {
|
||||||
if (labs.isSet('publicAPI') === true) {
|
if (labs.isSet('publicAPI') === true) {
|
||||||
return api.clients.read({slug: 'ghost-frontend'}).then(function (client) {
|
return api.clients
|
||||||
client = client.clients[0];
|
.read({slug: 'ghost-frontend'})
|
||||||
|
.then(function handleClient(client) {
|
||||||
|
client = client.clients[0];
|
||||||
|
|
||||||
if (client.status === 'enabled') {
|
if (client.status === 'enabled') {
|
||||||
return {
|
return {
|
||||||
id: client.slug,
|
id: client.slug,
|
||||||
secret: client.secret
|
secret: client.secret
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return Promise.resolve({});
|
return Promise.resolve({});
|
||||||
}
|
}
|
||||||
|
@ -80,99 +83,98 @@ function getAjaxHelper(clientId, clientSecret) {
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = function ghost_head(options) {
|
module.exports = function ghost_head(options) {
|
||||||
|
debug('begin');
|
||||||
// if server error page do nothing
|
// if server error page do nothing
|
||||||
if (this.statusCode >= 500) {
|
if (this.statusCode >= 500) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var metaData,
|
var head = [],
|
||||||
client,
|
|
||||||
head = [],
|
|
||||||
globalCodeinjection = settingsCache.get('ghost_head'),
|
globalCodeinjection = settingsCache.get('ghost_head'),
|
||||||
postCodeInjection = options.data.root && options.data.root.post ? options.data.root.post.codeinjection_head : null,
|
postCodeInjection = options.data.root && options.data.root.post ? options.data.root.post.codeinjection_head : null,
|
||||||
context = this.context ? this.context : null,
|
context = this.context ? this.context : null,
|
||||||
useStructuredData = !config.isPrivacyDisabled('useStructuredData'),
|
useStructuredData = !config.isPrivacyDisabled('useStructuredData'),
|
||||||
safeVersion = this.safeVersion,
|
safeVersion = this.safeVersion,
|
||||||
referrerPolicy = config.get('referrerPolicy') ? config.get('referrerPolicy') : 'no-referrer-when-downgrade',
|
referrerPolicy = config.get('referrerPolicy') ? config.get('referrerPolicy') : 'no-referrer-when-downgrade',
|
||||||
fetch = {
|
|
||||||
metaData: getMetaData(this, options.data.root),
|
|
||||||
client: getClient()
|
|
||||||
},
|
|
||||||
favicon = blogIconUtils.getIconUrl(),
|
favicon = blogIconUtils.getIconUrl(),
|
||||||
iconType = blogIconUtils.getIconType(favicon);
|
iconType = blogIconUtils.getIconType(favicon);
|
||||||
|
|
||||||
return Promise.props(fetch).then(function (response) {
|
debug('preparation complete, begin fetch');
|
||||||
client = response.client;
|
return Promise
|
||||||
metaData = response.metaData;
|
.join(getMetaData(this, options.data.root), getClient(), function handleData(metaData, client) {
|
||||||
|
debug('end fetch');
|
||||||
|
|
||||||
if (context) {
|
if (context) {
|
||||||
// head is our main array that holds our meta data
|
// head is our main array that holds our meta data
|
||||||
if (metaData.metaDescription && metaData.metaDescription.length > 0) {
|
if (metaData.metaDescription && metaData.metaDescription.length > 0) {
|
||||||
head.push('<meta name="description" content="' + escapeExpression(metaData.metaDescription) + '" />');
|
head.push('<meta name="description" content="' + escapeExpression(metaData.metaDescription) + '" />');
|
||||||
}
|
}
|
||||||
|
|
||||||
head.push('<link rel="shortcut icon" href="' + favicon + '" type="image/' + iconType + '" />');
|
head.push('<link rel="shortcut icon" href="' + favicon + '" type="image/' + iconType + '" />');
|
||||||
head.push('<link rel="canonical" href="' +
|
head.push('<link rel="canonical" href="' +
|
||||||
escapeExpression(metaData.canonicalUrl) + '" />');
|
escapeExpression(metaData.canonicalUrl) + '" />');
|
||||||
head.push('<meta name="referrer" content="' + referrerPolicy + '" />');
|
head.push('<meta name="referrer" content="' + referrerPolicy + '" />');
|
||||||
|
|
||||||
// show amp link in post when 1. we are not on the amp page and 2. amp is enabled
|
// 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')) {
|
if (_.includes(context, 'post') && !_.includes(context, 'amp') && settingsCache.get('amp')) {
|
||||||
head.push('<link rel="amphtml" href="' +
|
head.push('<link rel="amphtml" href="' +
|
||||||
escapeExpression(metaData.ampUrl) + '" />');
|
escapeExpression(metaData.ampUrl) + '" />');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (metaData.previousUrl) {
|
if (metaData.previousUrl) {
|
||||||
head.push('<link rel="prev" href="' +
|
head.push('<link rel="prev" href="' +
|
||||||
escapeExpression(metaData.previousUrl) + '" />');
|
escapeExpression(metaData.previousUrl) + '" />');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (metaData.nextUrl) {
|
if (metaData.nextUrl) {
|
||||||
head.push('<link rel="next" href="' +
|
head.push('<link rel="next" href="' +
|
||||||
escapeExpression(metaData.nextUrl) + '" />');
|
escapeExpression(metaData.nextUrl) + '" />');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!_.includes(context, 'paged') && useStructuredData) {
|
if (!_.includes(context, 'paged') && useStructuredData) {
|
||||||
head.push('');
|
head.push('');
|
||||||
head.push.apply(head, finaliseStructuredData(metaData));
|
head.push.apply(head, finaliseStructuredData(metaData));
|
||||||
head.push('');
|
head.push('');
|
||||||
|
|
||||||
if (metaData.schema) {
|
if (metaData.schema) {
|
||||||
head.push('<script type="application/ld+json">\n' +
|
head.push('<script type="application/ld+json">\n' +
|
||||||
JSON.stringify(metaData.schema, null, ' ') +
|
JSON.stringify(metaData.schema, null, ' ') +
|
||||||
'\n </script>\n');
|
'\n </script>\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('<meta name="generator" content="Ghost ' +
|
||||||
head.push(getAjaxHelper(client.id, client.secret));
|
escapeExpression(safeVersion) + '" />');
|
||||||
|
|
||||||
|
head.push('<link rel="alternate" type="application/rss+xml" title="' +
|
||||||
|
escapeExpression(metaData.blog.title) + '" href="' +
|
||||||
|
escapeExpression(metaData.rssUrl) + '" />');
|
||||||
|
|
||||||
|
// no code injection for amp context!!!
|
||||||
|
if (!_.includes(context, 'amp')) {
|
||||||
|
if (!_.isEmpty(globalCodeinjection)) {
|
||||||
|
head.push(globalCodeinjection);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!_.isEmpty(postCodeInjection)) {
|
||||||
|
head.push(postCodeInjection);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
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);
|
||||||
|
|
||||||
head.push('<meta name="generator" content="Ghost ' +
|
// Return what we have so far (currently nothing)
|
||||||
escapeExpression(safeVersion) + '" />');
|
return new SafeString(head.join('\n ').trim());
|
||||||
|
});
|
||||||
head.push('<link rel="alternate" type="application/rss+xml" title="' +
|
|
||||||
escapeExpression(metaData.blog.title) + '" href="' +
|
|
||||||
escapeExpression(metaData.rssUrl) + '" />');
|
|
||||||
|
|
||||||
// no code injection for amp context!!!
|
|
||||||
if (!_.includes(context, 'amp')) {
|
|
||||||
if (!_.isEmpty(globalCodeinjection)) {
|
|
||||||
head.push(globalCodeinjection);
|
|
||||||
}
|
|
||||||
|
|
||||||
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());
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue