mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-03 16:38:22 +03:00
Merge pull request #6202 from acburdine/ghost-url-updates
`ghost.url.api` cleanup/minification
This commit is contained in:
commit
2b145b3c61
1
.gitignore
vendored
1
.gitignore
vendored
@ -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
Gruntfile.js
13
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
|
||||
|
@ -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 '<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) {
|
||||
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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'
|
||||
|
@ -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();
|
||||
});
|
||||
@ -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();
|
||||
});
|
||||
@ -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();
|
||||
});
|
||||
@ -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();
|
||||
});
|
||||
@ -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();
|
||||
});
|
||||
|
@ -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",
|
||||
|
Loading…
Reference in New Issue
Block a user