From f1b71f7fd7c83ea63173dcff7c7e065130215ac0 Mon Sep 17 00:00:00 2001 From: Torsten Zander Date: Mon, 7 Feb 2022 16:07:18 +0100 Subject: [PATCH] :bug: Fixed AssetHelper not working with svg (#13978) loses TryGhost#13971 This fixes an issue with links containing # anchor. It makes sure the # part is at the end of the url like url?v=hash#anhor Co-authored-by: Hannah Wolfe --- core/frontend/meta/asset-url.js | 11 +++++++++++ test/unit/frontend/meta/asset-url.test.js | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/core/frontend/meta/asset-url.js b/core/frontend/meta/asset-url.js index a36dfd8b8c..ab56bca80c 100644 --- a/core/frontend/meta/asset-url.js +++ b/core/frontend/meta/asset-url.js @@ -41,9 +41,20 @@ function getAssetUrl(path, hasMinFile) { config.set('assetHash', (crypto.createHash('md5').update(Date.now().toString()).digest('hex')).substring(0, 10)); } + // if url has # make sure the hash is at th right place + let anchor; + if (path.match('#')) { + const index = output.indexOf('#'); + anchor = output.substring(index); + output = output.slice(0, index); + } + // Finally add the asset hash to the output URL output += '?v=' + config.get('assetHash'); + if (anchor) { + output += anchor; + } return output; } diff --git a/test/unit/frontend/meta/asset-url.test.js b/test/unit/frontend/meta/asset-url.test.js index d4c2144c16..4b0407e69f 100644 --- a/test/unit/frontend/meta/asset-url.test.js +++ b/test/unit/frontend/meta/asset-url.test.js @@ -34,6 +34,11 @@ describe('getAssetUrl', function () { testUrl.should.equal('/public/myfile.js?v=' + config.get('assetHash')); }); + it('should return hash before #', function () { + const testUrl = getAssetUrl('myfile.svg#arrow-up'); + testUrl.should.equal(`/assets/myfile.svg?v=${config.get('assetHash')}#arrow-up`); + }); + describe('favicon', function () { it('should not add asset to url if favicon.ico', function () { const testUrl = getAssetUrl('favicon.ico');