0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Merge pull request #6202 from acburdine/ghost-url-updates

`ghost.url.api` cleanup/minification
This commit is contained in:
Hannah Wolfe 2015-12-10 17:21:14 +00:00
commit 2b145b3c61
7 changed files with 57 additions and 36 deletions

1
.gitignore vendored
View file

@ -66,6 +66,7 @@ config.js
# Built asset files # Built asset files
/core/built /core/built
/core/server/views/default.hbs /core/server/views/default.hbs
/core/shared/ghost-url.min.js
# Coverage reports # Coverage reports
coverage.html coverage.html

View file

@ -418,6 +418,17 @@ var _ = require('lodash'),
params: '--init' 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. // 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', grunt.registerTask('prod', 'Build JS & templates for production',
['shell:ember:prod', 'master-warn']); ['shell:ember:prod', 'uglify:prod', 'master-warn']);
// ### Live reload // ### Live reload
// `grunt dev` - build assets on the fly whilst developing // `grunt dev` - build assets on the fly whilst developing

View file

@ -10,13 +10,12 @@ var hbs = require('express-hbs'),
moment = require('moment'), moment = require('moment'),
_ = require('lodash'), _ = require('lodash'),
Promise = require('bluebird'), Promise = require('bluebird'),
fs = require('fs'),
path = require('path'),
config = require('../config'), config = require('../config'),
filters = require('../filters'), filters = require('../filters'),
api = require('../api'), api = require('../api'),
assetHelper = require('./asset'),
urlHelper = require('./url'), urlHelper = require('./url'),
meta_description = require('./meta_description'), meta_description = require('./meta_description'),
meta_title = require('./meta_title'), meta_title = require('./meta_title'),
@ -278,10 +277,8 @@ function finaliseSchema(schema, head) {
return head; return head;
} }
function getAjaxHelper() { function getAjaxHelper(clientId, clientSecret) {
var ghostUrlScript = fs.readFileSync(path.join(config.paths.corePath, 'shared', 'ghost-url.js'), 'utf8'), var apiPath = require('../routes').apiBaseUri,
template = hbs.compile(ghostUrlScript, path.join(config.paths.subdir || '/', 'shared', 'ghost-url.js')),
apiPath = require('../routes').apiBaseUri,
url, useOrigin; url, useOrigin;
if (config.forceAdminSSL) { if (config.forceAdminSSL) {
@ -292,7 +289,19 @@ function getAjaxHelper() {
useOrigin = true; 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) { ghost_head = function (options) {
@ -357,7 +366,7 @@ ghost_head = function (options) {
if (metaData.clientId && metaData.clientSecret) { if (metaData.clientId && metaData.clientSecret) {
head.push(writeMetaTag('ghost:client_id', metaData.clientId)); head.push(writeMetaTag('ghost:client_id', metaData.clientId));
head.push(writeMetaTag('ghost:client_secret', metaData.clientSecret)); head.push(writeMetaTag('ghost:client_secret', metaData.clientSecret));
head.push(getAjaxHelper()); head.push(getAjaxHelper(metaData.clientId, metaData.clientSecret));
} }
} }

View file

@ -2,7 +2,7 @@
'use strict'; 'use strict';
function generateQueryString(object) { function generateQueryString(object) {
var url = '?', var queries = [],
i; i;
if (!object) { if (!object) {
@ -11,25 +11,22 @@
for (i in object) { for (i in object) {
if (object.hasOwnProperty(i) && (!!object[i] || object[i] === false)) { 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 = { var url = {
config: { config: {},
url: '{{api_url}}',
useOrigin: '{{useOrigin}}',
origin: '',
clientId: '',
clientSecret: ''
},
api: function () { api: function () {
var args = Array.prototype.slice.call(arguments), 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; queryOptions;
if (args.length && typeof args[args.length - 1] === 'object') { if (args.length && typeof args[args.length - 1] === 'object') {
@ -52,12 +49,11 @@
}; };
if (typeof window !== 'undefined') { 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 || {}; window.ghost = window.ghost || {};
url.config = window.ghost.config || {};
window.ghost.url = url; window.ghost.url = url;
} }
if (typeof module !== 'undefined') { if (typeof module !== 'undefined') {
module.exports = url; module.exports = url;
} }

View file

@ -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 () { it('renders basic url correctly when no arguments are presented & useOrigin is set to false', function () {
url.config = { url.config = {
url: 'http://testblog.com/', url: 'http://testblog.com/',
useOrigin: 'false', useOrigin: false,
clientId: '', clientId: '',
clientSecret: '' 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 () { it('renders basic url correctly when no arguments are presented & useOrigin is set to true', function () {
url.config = { url.config = {
url: '/url/', url: '/url/',
useOrigin: 'true', useOrigin: true,
origin: 'http://originblog.com', origin: 'http://originblog.com',
clientId: '', clientId: '',
clientSecret: '' clientSecret: ''
@ -33,7 +33,7 @@ describe('Ghost Ajax Helper', function () {
it('strips arguments of forward and trailing slashes correctly', function () { it('strips arguments of forward and trailing slashes correctly', function () {
url.config = { url.config = {
url: 'http://testblog.com/', url: 'http://testblog.com/',
useOrigin: 'false', useOrigin: false,
clientId: '', clientId: '',
clientSecret: '' clientSecret: ''
}; };
@ -44,7 +44,7 @@ describe('Ghost Ajax Helper', function () {
it('appends client_id & client_secret to query string automatically', function () { it('appends client_id & client_secret to query string automatically', function () {
url.config = { url.config = {
url: 'http://testblog.com/', url: 'http://testblog.com/',
useOrigin: 'false', useOrigin: false,
clientId: 'ghost-frontend', clientId: 'ghost-frontend',
clientSecret: 'notasecret' clientSecret: 'notasecret'
}; };
@ -55,7 +55,7 @@ describe('Ghost Ajax Helper', function () {
it('generates query parameters correctly', function () { it('generates query parameters correctly', function () {
url.config = { url.config = {
url: 'http://testblog.com/', url: 'http://testblog.com/',
useOrigin: 'false', useOrigin: false,
clientId: 'ghost-frontend', clientId: 'ghost-frontend',
clientSecret: 'notasecret' clientSecret: 'notasecret'
}; };
@ -73,7 +73,7 @@ describe('Ghost Ajax Helper', function () {
it('generates complex query correctly', function () { it('generates complex query correctly', function () {
url.config = { url.config = {
url: '/blog/ghost/api/v0.1/', url: '/blog/ghost/api/v0.1/',
useOrigin: 'true', useOrigin: true,
origin: 'https://testblog.com', origin: 'https://testblog.com',
clientId: 'ghost-frontend', clientId: 'ghost-frontend',
clientSecret: 'notasecret' clientSecret: 'notasecret'

View file

@ -789,15 +789,18 @@ describe('{{ghost_head}} helper', function () {
}); });
it('renders script tags with basic configuration', function (done) { it('renders script tags with basic configuration', function (done) {
utils.overrideConfig({
url: 'http://example.com/'
});
helpers.ghost_head.call( helpers.ghost_head.call(
{safeVersion: '0.3', context: ['paged', 'index'], post: false}, {safeVersion: '0.3', context: ['paged', 'index'], post: false},
{data: {root: {context: []}}} {data: {root: {context: []}}}
).then(function (rendered) { ).then(function (rendered) {
should.exist(rendered); should.exist(rendered);
expectGhostClientMeta(rendered); expectGhostClientMeta(rendered);
rendered.string.should.match(/<script type="text\/javascript">\(function \(\) \{/); rendered.string.should.match(/<script type="text\/javascript">/);
rendered.string.should.match(/'use strict';/); rendered.string.should.match(/<script type="text\/javascript" src="\/shared\/ghost-url\.js\?v=/);
rendered.string.should.match(/<\/script>/);
done(); done();
}); });
@ -815,7 +818,7 @@ describe('{{ghost_head}} helper', function () {
should.exist(rendered); should.exist(rendered);
expectGhostClientMeta(rendered); expectGhostClientMeta(rendered);
rendered.string.should.match(/url: '\/ghost\/api\/v0\.1\/'/); rendered.string.should.match(/url: '\/ghost\/api\/v0\.1\/'/);
rendered.string.should.match(/useOrigin: 'true'/); rendered.string.should.match(/useOrigin: true/);
done(); done();
}); });
@ -833,7 +836,7 @@ describe('{{ghost_head}} helper', function () {
should.exist(rendered); should.exist(rendered);
expectGhostClientMeta(rendered); expectGhostClientMeta(rendered);
rendered.string.should.match(/url: '\/blog\/ghost\/api\/v0\.1\/'/); rendered.string.should.match(/url: '\/blog\/ghost\/api\/v0\.1\/'/);
rendered.string.should.match(/useOrigin: 'true'/); rendered.string.should.match(/useOrigin: true/);
done(); done();
}); });
@ -852,7 +855,7 @@ describe('{{ghost_head}} helper', function () {
should.exist(rendered); should.exist(rendered);
expectGhostClientMeta(rendered); expectGhostClientMeta(rendered);
rendered.string.should.match(/url: 'https:\/\/testurl\.com\/ghost\/api\/v0\.1\/'/); 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(); done();
}); });
@ -872,7 +875,7 @@ describe('{{ghost_head}} helper', function () {
should.exist(rendered); should.exist(rendered);
expectGhostClientMeta(rendered); expectGhostClientMeta(rendered);
rendered.string.should.match(/url: 'https:\/\/sslurl\.com\/ghost\/api\/v0\.1\/'/); 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(); done();
}); });

View file

@ -82,6 +82,7 @@
"grunt-contrib-compress": "0.13.0", "grunt-contrib-compress": "0.13.0",
"grunt-contrib-copy": "0.8.0", "grunt-contrib-copy": "0.8.0",
"grunt-contrib-jshint": "0.11.2", "grunt-contrib-jshint": "0.11.2",
"grunt-contrib-uglify": "0.11.0",
"grunt-contrib-watch": "0.6.1", "grunt-contrib-watch": "0.6.1",
"grunt-docker": "0.0.10", "grunt-docker": "0.0.10",
"grunt-express-server": "0.5.1", "grunt-express-server": "0.5.1",