Skip to content

Commit

Permalink
🐛 Blog icon improvements (#8298)
Browse files Browse the repository at this point in the history
refs #7688

- renders the correct `/favicon.ico` or `/favcicon.png` in `{{ghost_head}}`
- removes an regex issue in `serve-favicon`
  • Loading branch information
aileen authored and ErisDS committed Apr 10, 2017
1 parent a413d70 commit 4ba5cc8
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 36 deletions.
2 changes: 1 addition & 1 deletion core/server/data/meta/asset_url.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var config = require('../../config'),
*/
function getFaviconUrl() {
if (settingsCache.get('icon')) {
return utils.url.urlJoin(utils.url.getSubdir(), utils.url.urlFor('image', {image: settingsCache.get('icon')}));
return settingsCache.get('icon').match(/\.ico$/i) ? utils.url.urlJoin(utils.url.getSubdir(), '/favicon.ico') : utils.url.urlJoin(utils.url.getSubdir(), '/favicon.png');
}

return utils.url.urlJoin(utils.url.getSubdir(), '/favicon.ico');
Expand Down
7 changes: 4 additions & 3 deletions core/server/helpers/ghost_head.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ module.exports = function ghost_head(options) {
},
blogIcon = settingsCache.get('icon'),
// CASE: blog icon is not set in config, we serve the default
iconType = !blogIcon ? 'x-icon' : blogIcon.match(/\/favicon\.ico$/i) ? 'x-icon' : 'png',
favicon = !blogIcon ? '/favicon.ico' : url.urlFor('image', {image: blogIcon});
iconType = !blogIcon ? 'x-icon' : blogIcon.match(/\.ico$/i) ? 'x-icon' : 'png',
favicon = !blogIcon ? url.urlFor({relativeUrl: '/favicon.ico'}) :
blogIcon.match(/\.ico$/i) ? url.urlFor({relativeUrl: '/favicon.ico'}) : url.urlFor({relativeUrl: '/favicon.png'});

return Promise.props(fetch).then(function (response) {
client = response.client;
Expand All @@ -111,7 +112,7 @@ module.exports = function ghost_head(options) {
head.push('<meta name="description" content="' + escapeExpression(metaData.metaDescription) + '" />');
}

head.push('<link rel="shortcut icon" href="' + favicon + '" type="' + iconType + '" />');
head.push('<link rel="shortcut icon" href="' + favicon + '" type="image/' + iconType + '" />');
head.push('<link rel="canonical" href="' +
escapeExpression(metaData.canonicalUrl) + '" />');
head.push('<meta name="referrer" content="' + referrerPolicy + '" />');
Expand Down
2 changes: 1 addition & 1 deletion core/server/middleware/serve-favicon.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function serveFavicon() {
storage.getStorage()
.read({path: filePath})
.then(function readFile(buf) {
iconType = settingsCache.get('icon').match(/\/favicon\.ico$/i) ? 'x-icon' : 'png';
iconType = settingsCache.get('icon').match(/\.ico$/i) ? 'x-icon' : 'png';
content = buildContentResponse(iconType, buf);

res.writeHead(200, content.headers);
Expand Down
4 changes: 2 additions & 2 deletions core/test/unit/metadata/asset_url_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('getAssetUrl', function () {
testUrl.should.equal('/favicon.ico');
});

it.skip('should correct favicon path for custom png', function () {
it('should correct favicon path for custom png', function () {
sandbox.stub(settingsCache, 'get').withArgs('icon').returns('/content/images/2017/04/my-icon.png');
var testUrl = getAssetUrl('favicon.ico');
testUrl.should.equal('/favicon.png');
Expand Down Expand Up @@ -113,7 +113,7 @@ describe('getAssetUrl', function () {
testUrl.should.equal('/blog/favicon.ico');
});

it.skip('should return correct favicon path for custom png', function () {
it('should return correct favicon path for custom png', function () {
sandbox.stub(settingsCache, 'get').withArgs('icon').returns('/content/images/2017/04/my-icon.png');
var testUrl = getAssetUrl('favicon.ico');
testUrl.should.equal('/blog/favicon.png');
Expand Down
20 changes: 20 additions & 0 deletions core/test/unit/middleware/serve-favicon_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,26 @@ describe('Serve Favicon', function () {
middleware(req, res, next);
});

it('custom uploaded myicon.ico', function (done) {
var middleware = serveFavicon();
req.path = '/favicon.ico';

storage.getStorage().storagePath = path.join(__dirname, '../../../test/utils/fixtures/images/');
localSettingsCache.icon = 'myicon.ico';

res = {
writeHead: function (statusCode) {
statusCode.should.eql(200);
},
end: function (body) {
body.length.should.eql(15086);
done();
}
};

middleware(req, res, next);
});

it('default favicon.ico', function (done) {
var middleware = serveFavicon();
req.path = '/favicon.ico';
Expand Down
8 changes: 4 additions & 4 deletions core/test/unit/server_helpers/asset_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ describe('{{asset}} helper', function () {
// with png
rendered = helpers.asset('favicon.png');
should.exist(rendered);
String(rendered).should.equal('/content/images/favicon.png');
String(rendered).should.equal('/favicon.png');

localSettingsCache.icon = '/content/images/favicon.ico';

// with ico
rendered = helpers.asset('favicon.ico');
should.exist(rendered);
String(rendered).should.equal('/content/images/favicon.ico');
String(rendered).should.equal('/favicon.ico');
});

it('handles public assets correctly', function () {
Expand Down Expand Up @@ -96,14 +96,14 @@ describe('{{asset}} helper', function () {
// with png
rendered = helpers.asset('favicon.png');
should.exist(rendered);
String(rendered).should.equal('/blog/content/images/favicon.png');
String(rendered).should.equal('/blog/favicon.png');

localSettingsCache.icon = '/content/images/favicon.ico';

// with ico
rendered = helpers.asset('favicon.ico');
should.exist(rendered);
String(rendered).should.equal('/blog/content/images/favicon.ico');
String(rendered).should.equal('/blog/favicon.ico');
});

it('handles public assets correctly', function () {
Expand Down
Loading

0 comments on commit 4ba5cc8

Please sign in to comment.