From 8f89997debed46c15f86adb3774a3656ea7f36d1 Mon Sep 17 00:00:00 2001 From: Austin Burdine Date: Thu, 10 Dec 2015 08:46:58 -0600 Subject: [PATCH] minify ghost.url.api in production closes #6150 - clean up ghost.url.api script - switch to inlining config and making the ghost-url.js file an external request - add minification in production --- .gitignore | 1 + Gruntfile.js | 13 +++++++++- core/server/helpers/ghost_head.js | 25 +++++++++++++------ core/shared/ghost-url.js | 24 ++++++++---------- core/test/unit/ghost_url_spec.js | 12 ++++----- .../unit/server_helpers/ghost_head_spec.js | 17 +++++++------ package.json | 1 + 7 files changed, 57 insertions(+), 36 deletions(-) diff --git a/.gitignore b/.gitignore index c62cda1885..ec109c56a1 100644 --- a/.gitignore +++ b/.gitignore @@ -66,6 +66,7 @@ config.js # Built asset files /core/built /core/server/views/default.hbs +/core/shared/ghost-url.min.js # Coverage reports coverage.html diff --git a/Gruntfile.js b/Gruntfile.js index c43b499b3e..2d9002bbe3 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -418,6 +418,17 @@ var _ = require('lodash'), params: '--init' } } + }, + + uglify: { + prod: { + options: { + sourceMap: false + }, + files: { + 'core/shared/ghost-url.min.js': 'core/shared/ghost-url.js' + } + } } }; @@ -909,7 +920,7 @@ var _ = require('lodash'), // // It is otherwise the same as running `grunt`, but is only used when running Ghost in the `production` env. grunt.registerTask('prod', 'Build JS & templates for production', - ['shell:ember:prod', 'master-warn']); + ['shell:ember:prod', 'uglify:prod', 'master-warn']); // ### Live reload // `grunt dev` - build assets on the fly whilst developing diff --git a/core/server/helpers/ghost_head.js b/core/server/helpers/ghost_head.js index 69de00cd39..3213c53072 100644 --- a/core/server/helpers/ghost_head.js +++ b/core/server/helpers/ghost_head.js @@ -10,13 +10,12 @@ var hbs = require('express-hbs'), moment = require('moment'), _ = require('lodash'), Promise = require('bluebird'), - fs = require('fs'), - path = require('path'), config = require('../config'), filters = require('../filters'), api = require('../api'), + assetHelper = require('./asset'), urlHelper = require('./url'), meta_description = require('./meta_description'), meta_title = require('./meta_title'), @@ -278,10 +277,8 @@ function finaliseSchema(schema, head) { return head; } -function getAjaxHelper() { - var ghostUrlScript = fs.readFileSync(path.join(config.paths.corePath, 'shared', 'ghost-url.js'), 'utf8'), - template = hbs.compile(ghostUrlScript, path.join(config.paths.subdir || '/', 'shared', 'ghost-url.js')), - apiPath = require('../routes').apiBaseUri, +function getAjaxHelper(clientId, clientSecret) { + var apiPath = require('../routes').apiBaseUri, url, useOrigin; if (config.forceAdminSSL) { @@ -292,7 +289,19 @@ function getAjaxHelper() { useOrigin = true; } - return ''; + return '' + + ''; } ghost_head = function (options) { @@ -357,7 +366,7 @@ ghost_head = function (options) { if (metaData.clientId && metaData.clientSecret) { head.push(writeMetaTag('ghost:client_id', metaData.clientId)); head.push(writeMetaTag('ghost:client_secret', metaData.clientSecret)); - head.push(getAjaxHelper()); + head.push(getAjaxHelper(metaData.clientId, metaData.clientSecret)); } } diff --git a/core/shared/ghost-url.js b/core/shared/ghost-url.js index 2681fe4c2d..e04f6e1225 100644 --- a/core/shared/ghost-url.js +++ b/core/shared/ghost-url.js @@ -2,7 +2,7 @@ 'use strict'; function generateQueryString(object) { - var url = '?', + var queries = [], i; if (!object) { @@ -11,25 +11,22 @@ for (i in object) { if (object.hasOwnProperty(i) && (!!object[i] || object[i] === false)) { - url += i + '=' + encodeURIComponent(object[i]) + '&'; + queries.push(i + '=' + encodeURIComponent(object[i])); } } - return url.substring(0, url.length - 1); + if (queries.length) { + return '?' + queries.join('&'); + } + return ''; } var url = { - config: { - url: '{{api_url}}', - useOrigin: '{{useOrigin}}', - origin: '', - clientId: '', - clientSecret: '' - }, + config: {}, api: function () { var args = Array.prototype.slice.call(arguments), - url = ((this.config.useOrigin === 'true')) ? this.config.origin + this.config.url : this.config.url, + url = (this.config.useOrigin) ? this.config.origin + this.config.url : this.config.url, queryOptions; if (args.length && typeof args[args.length - 1] === 'object') { @@ -52,12 +49,11 @@ }; if (typeof window !== 'undefined') { - url.config.origin = window.location.origin; - url.config.clientId = document.querySelector('meta[property=\'ghost:client_id\']').content; - url.config.clientSecret = document.querySelector('meta[property=\'ghost:client_secret\']').content; window.ghost = window.ghost || {}; + url.config = window.ghost.config || {}; window.ghost.url = url; } + if (typeof module !== 'undefined') { module.exports = url; } diff --git a/core/test/unit/ghost_url_spec.js b/core/test/unit/ghost_url_spec.js index 7836bfa918..e644a01400 100644 --- a/core/test/unit/ghost_url_spec.js +++ b/core/test/unit/ghost_url_spec.js @@ -10,7 +10,7 @@ describe('Ghost Ajax Helper', function () { it('renders basic url correctly when no arguments are presented & useOrigin is set to false', function () { url.config = { url: 'http://testblog.com/', - useOrigin: 'false', + useOrigin: false, clientId: '', clientSecret: '' }; @@ -21,7 +21,7 @@ describe('Ghost Ajax Helper', function () { it('renders basic url correctly when no arguments are presented & useOrigin is set to true', function () { url.config = { url: '/url/', - useOrigin: 'true', + useOrigin: true, origin: 'http://originblog.com', clientId: '', clientSecret: '' @@ -33,7 +33,7 @@ describe('Ghost Ajax Helper', function () { it('strips arguments of forward and trailing slashes correctly', function () { url.config = { url: 'http://testblog.com/', - useOrigin: 'false', + useOrigin: false, clientId: '', clientSecret: '' }; @@ -44,7 +44,7 @@ describe('Ghost Ajax Helper', function () { it('appends client_id & client_secret to query string automatically', function () { url.config = { url: 'http://testblog.com/', - useOrigin: 'false', + useOrigin: false, clientId: 'ghost-frontend', clientSecret: 'notasecret' }; @@ -55,7 +55,7 @@ describe('Ghost Ajax Helper', function () { it('generates query parameters correctly', function () { url.config = { url: 'http://testblog.com/', - useOrigin: 'false', + useOrigin: false, clientId: 'ghost-frontend', clientSecret: 'notasecret' }; @@ -73,7 +73,7 @@ describe('Ghost Ajax Helper', function () { it('generates complex query correctly', function () { url.config = { url: '/blog/ghost/api/v0.1/', - useOrigin: 'true', + useOrigin: true, origin: 'https://testblog.com', clientId: 'ghost-frontend', clientSecret: 'notasecret' diff --git a/core/test/unit/server_helpers/ghost_head_spec.js b/core/test/unit/server_helpers/ghost_head_spec.js index 5115bbe97e..cd51dc98a3 100644 --- a/core/test/unit/server_helpers/ghost_head_spec.js +++ b/core/test/unit/server_helpers/ghost_head_spec.js @@ -789,15 +789,18 @@ describe('{{ghost_head}} helper', function () { }); it('renders script tags with basic configuration', function (done) { + utils.overrideConfig({ + url: 'http://example.com/' + }); + helpers.ghost_head.call( {safeVersion: '0.3', context: ['paged', 'index'], post: false}, {data: {root: {context: []}}} ).then(function (rendered) { should.exist(rendered); expectGhostClientMeta(rendered); - rendered.string.should.match(/