0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00

Url Service: trigger url event with relative/absolute urls

refs https://github.com/TryGhost/Team/issues/65

- this is just the first optimisation regarding relative/absolute urls
- the full strike will happen when i start with the url utility re-write
- for now: there will be only one subscriber of url events -> the sitemaps service
- the sitemaps service outputs absolute urls
  - we don't want to receive an url event and ask the url service again to get an absolute version of the url
This commit is contained in:
kirrg001 2018-04-18 11:30:40 +02:00
parent 2a4d759b78
commit 097e1d1fac
3 changed files with 15 additions and 5 deletions

View file

@ -138,8 +138,6 @@ class UrlGenerator {
/**
* We currently generate relative urls.
*
* @TODO: reconsider? e.g. sitemaps would receive a relative url, but we show absolute urls
*/
_generateUrl(resource) {
let url = this.routingType.getPermalinks().getValue();

View file

@ -2,6 +2,7 @@
const _ = require('lodash');
const debug = require('ghost-ignition').debug('services:url:urls');
const localUtils = require('./utils');
const common = require('../../lib/common');
/**
@ -9,6 +10,8 @@ const common = require('../../lib/common');
* Each resource has exactly one url.
*
* Connector for url generator and resources.
*
* Stores relative urls by default.
*/
class Urls {
constructor() {
@ -38,11 +41,15 @@ class Urls {
};
common.events.emit('url.added', {
url: url,
url: {
relative: url,
absolute: localUtils.createUrl(url, true)
},
resource: resource
});
}
// @TODO: add an option to receive an absolute url
getByResourceId(id) {
return this.urls[id];
}

View file

@ -47,8 +47,8 @@ describe('Unit: services/url/Urls', function () {
});
eventsToRemember = {};
sandbox.stub(common.events, 'emit').callsFake(function (eventName, callback) {
eventsToRemember[eventName] = callback;
sandbox.stub(common.events, 'emit').callsFake(function (eventName, data) {
eventsToRemember[eventName] = data;
});
});
@ -69,6 +69,11 @@ describe('Unit: services/url/Urls', function () {
});
should.exist(eventsToRemember['url.added']);
eventsToRemember['url.added'].url.absolute.should.eql('http://127.0.0.1:2369/test/');
eventsToRemember['url.added'].url.relative.should.eql('/test/');
should.exist(eventsToRemember['url.added'].resource);
should.exist(eventsToRemember['url.added'].resource.data);
urls.getByResourceId('object-id-x').resource.data.slug.should.eql('a');
// add duplicate