diff --git a/core/frontend/helpers/asset.js b/core/frontend/helpers/asset.js index be6a0bfdd2..4f59d27974 100644 --- a/core/frontend/helpers/asset.js +++ b/core/frontend/helpers/asset.js @@ -2,7 +2,7 @@ // Usage: `{{asset "css/screen.css"}}` // // Returns the path to the specified asset. -const {metaData} = require('../services/proxy'); +const {metaData, urlUtils} = require('../services/proxy'); const {SafeString} = require('../services/rendering'); const errors = require('@tryghost/errors'); @@ -22,6 +22,14 @@ module.exports = function asset(path, options) { message: tpl(messages.pathIsRequired) }); } + if (typeof urlUtils.getSiteUrl() !== 'undefined' + && typeof urlUtils.getAdminUrl() !== 'undefined' + && urlUtils.getSiteUrl() !== urlUtils.getAdminUrl()) { + const target = new URL(getAssetUrl(path, hasMinFile), urlUtils.getSiteUrl()); + return new SafeString( + target.href + ); + } return new SafeString( getAssetUrl(path, hasMinFile) diff --git a/test/unit/frontend/helpers/asset.test.js b/test/unit/frontend/helpers/asset.test.js index c2b2c0d952..4bef9e3e8a 100644 --- a/test/unit/frontend/helpers/asset.test.js +++ b/test/unit/frontend/helpers/asset.test.js @@ -75,4 +75,27 @@ describe('{{asset}} helper', function () { String(rendered).should.equal('/assets/js/asset.min.js?v=abc'); }); }); + + describe('different admin and site urls', function () { + before(function () { + configUtils.set({url: 'http://127.0.0.1'}); + configUtils.set({'admin:url': 'http://localhost'}); + }); + + after(function () { + configUtils.restore(); + }); + + it('handles favicon correctly', function () { + rendered = asset('favicon.ico'); + should.exist(rendered); + String(rendered).should.equal('http://127.0.0.1/favicon.ico'); + }); + + it('handles ghost.css for default templates correctly', function () { + rendered = asset('public/ghost.css'); + should.exist(rendered); + String(rendered).should.equal('http://127.0.0.1/public/ghost.css?v=abc'); + }); + }); });