Skip to content

Commit

Permalink
minify ghost.url.api in production
Browse files Browse the repository at this point in the history
closes TryGhost#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
  • Loading branch information
acburdine committed Dec 10, 2015
1 parent 70a481b commit 8f89997
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 12 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
}
};

Expand Down Expand Up @@ -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
Expand Down
25 changes: 17 additions & 8 deletions core/server/helpers/ghost_head.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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) {
Expand All @@ -292,7 +289,19 @@ function getAjaxHelper() {
useOrigin = true;
}

return '<script type="text/javascript">' + template({api_url: url, useOrigin: useOrigin ? 'true' : 'false'}) + '</script>';
return '<script type="text/javascript">\n' +
'window.ghost = window.ghost || {};\n' +
'window.ghost.config = {\n' +
'\turl: \'' + url + '\',\n' +
'\tuseOrigin: ' + (useOrigin ? 'true' : 'false') + ',\n' +
'\torigin: window.location.origin,\n' +
'\tclientId: \'' + clientId + '\',\n' +
'\tclientSecret: \'' + clientSecret + '\'\n' +
'};' +
'</script>' +
'<script type="text/javascript" src="' +
assetHelper('shared/ghost-url.js', {hash: {minifyInProduction: true}}) +
'"></script>';
}

ghost_head = function (options) {
Expand Down Expand Up @@ -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));
}
}

Expand Down
24 changes: 10 additions & 14 deletions core/shared/ghost-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'use strict';

function generateQueryString(object) {
var url = '?',
var queries = [],
i;

if (!object) {
Expand All @@ -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') {
Expand All @@ -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;
}
Expand Down
12 changes: 6 additions & 6 deletions core/test/unit/ghost_url_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: ''
};
Expand All @@ -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: ''
Expand All @@ -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: ''
};
Expand All @@ -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'
};
Expand All @@ -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'
};
Expand All @@ -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'
Expand Down
17 changes: 10 additions & 7 deletions core/test/unit/server_helpers/ghost_head_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(/<script type="text\/javascript">\(function \(\) \{/);
rendered.string.should.match(/'use strict';/);
rendered.string.should.match(/<\/script>/);
rendered.string.should.match(/<script type="text\/javascript">/);
rendered.string.should.match(/<script type="text\/javascript" src="\/shared\/ghost-url\.js\?v=/);

done();
});
Expand All @@ -815,7 +818,7 @@ describe('{{ghost_head}} helper', function () {
should.exist(rendered);
expectGhostClientMeta(rendered);
rendered.string.should.match(/url: '\/ghost\/api\/v0\.1\/'/);
rendered.string.should.match(/useOrigin: 'true'/);
rendered.string.should.match(/useOrigin: true/);

done();
});
Expand All @@ -833,7 +836,7 @@ describe('{{ghost_head}} helper', function () {
should.exist(rendered);
expectGhostClientMeta(rendered);
rendered.string.should.match(/url: '\/blog\/ghost\/api\/v0\.1\/'/);
rendered.string.should.match(/useOrigin: 'true'/);
rendered.string.should.match(/useOrigin: true/);

done();
});
Expand All @@ -852,7 +855,7 @@ describe('{{ghost_head}} helper', function () {
should.exist(rendered);
expectGhostClientMeta(rendered);
rendered.string.should.match(/url: 'https:\/\/testurl\.com\/ghost\/api\/v0\.1\/'/);
rendered.string.should.match(/useOrigin: 'false'/);
rendered.string.should.match(/useOrigin: false/);

done();
});
Expand All @@ -872,7 +875,7 @@ describe('{{ghost_head}} helper', function () {
should.exist(rendered);
expectGhostClientMeta(rendered);
rendered.string.should.match(/url: 'https:\/\/sslurl\.com\/ghost\/api\/v0\.1\/'/);
rendered.string.should.match(/useOrigin: 'false'/);
rendered.string.should.match(/useOrigin: false/);

done();
});
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"grunt-contrib-compress": "0.13.0",
"grunt-contrib-copy": "0.8.0",
"grunt-contrib-jshint": "0.11.2",
"grunt-contrib-uglify": "0.11.0",
"grunt-contrib-watch": "0.6.1",
"grunt-docker": "0.0.10",
"grunt-express-server": "0.5.1",
Expand Down

0 comments on commit 8f89997

Please sign in to comment.