Skip to content

Commit

Permalink
Url Service: trigger url event with relative/absolute urls
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kirrg001 committed Apr 18, 2018
1 parent 2a4d759 commit 097e1d1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
2 changes: 0 additions & 2 deletions core/server/services/url/UrlGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
9 changes: 8 additions & 1 deletion core/server/services/url/Urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

const _ = require('lodash');
const debug = require('ghost-ignition').debug('services:url:urls');
const localUtils = require('./utils');
const common = require('../../lib/common');

/**
* Keeps track of all urls.
* Each resource has exactly one url.
*
* Connector for url generator and resources.
*
* Stores relative urls by default.
*/
class Urls {
constructor() {
Expand Down Expand Up @@ -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];
}
Expand Down
9 changes: 7 additions & 2 deletions core/test/unit/services/url/Urls_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
});

Expand All @@ -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
Expand Down

0 comments on commit 097e1d1

Please sign in to comment.