🐛 Fixed card asset init/reload behaviour

- Card asset reloading was incorrectly only happening if the API version changed 🙈
- In addition, having an init function was redundant, as theme activation happens on boot
- This meant that the card assets were being generated twice on boot
- Instead, we now only generate them on theme activation, which covers the boot case and simplifies all the logic
This commit is contained in:
Hannah Wolfe 2021-11-23 12:47:46 +00:00
parent 6e6a4822f2
commit 0ede559d5b
No known key found for this signature in database
GPG Key ID: AB586C3B5AE5C037
5 changed files with 11 additions and 40 deletions

View File

@ -151,9 +151,6 @@ async function initFrontend() {
const helperService = require('./frontend/services/helpers');
await helperService.init();
const cardAssetService = require('./frontend/services/card-assets');
await cardAssetService.init();
debug('End: initFrontend');
}

View File

@ -16,6 +16,7 @@ const config = require('./shared/config');
const logging = require('@tryghost/logging');
const tpl = require('@tryghost/tpl');
const themeEngine = require('./frontend/services/theme-engine');
const cardAssetService = require('./frontend/services/card-assets');
const routerManager = require('./frontend/services/routing').routerManager;
const settingsCache = require('./shared/settings-cache');
@ -65,6 +66,11 @@ class Bridge {
themeEngine.setActive(settings, loadedTheme, checkedTheme);
const currentGhostAPI = this.getActiveTheme().engine('ghost-api');
const cardAssetConfig = this.getCardAssetConfig();
debug('reload card assets config', cardAssetConfig);
cardAssetService.load(cardAssetConfig);
if (previousGhostAPI !== undefined && (previousGhostAPI !== currentGhostAPI)) {
events.emit('services.themes.api.changed');
this.reloadFrontend();
@ -95,11 +101,6 @@ class Bridge {
reloadFrontend() {
const apiVersion = this.getFrontendApiVersion();
const cardAssetConfig = this.getCardAssetConfig();
debug('reload card assets config', cardAssetConfig);
const cardAssetService = require('./frontend/services/card-assets');
cardAssetService.load(cardAssetConfig);
debug('reload frontend', apiVersion);
const siteApp = require('./frontend/web/site');

View File

@ -1,16 +1,4 @@
const debug = require('@tryghost/debug')('card-assets');
const themeEngine = require('../theme-engine');
const CardAssetService = require('./service');
let cardAssetService = new CardAssetService();
const initFn = async () => {
const cardAssetConfig = themeEngine.getActive().config('card_assets');
debug('initialising with config', cardAssetConfig);
await cardAssetService.load(cardAssetConfig);
};
module.exports = cardAssetService;
module.exports.init = initFn;

View File

@ -1,3 +1,4 @@
const debug = require('@tryghost/debug')('card-assets');
const Minifier = require('@tryghost/minifier');
const _ = require('lodash');
const path = require('path');
@ -98,10 +99,14 @@ class CardAssetService {
this.config = cardAssetConfig;
}
debug('loading with config', cardAssetConfig);
await this.clearFiles();
const globs = this.generateGlobs();
debug('globs', globs);
this.files = await this.minify(globs) || [];
}
}

View File

@ -5,30 +5,10 @@ const path = require('path');
const fs = require('fs').promises;
const os = require('os');
const cardAssetService = require('../../../../core/frontend/services/card-assets');
const CardAssetService = require('../../../../core/frontend/services/card-assets/service');
const themeEngine = require('../../../../core/frontend/services/theme-engine');
const themeDefaults = require('../../../../core/frontend/services/theme-engine/config/defaults.json');
describe('Card Asset Init', function () {
it('calls loader with config', function () {
sinon.stub(themeEngine, 'getActive').returns({
config: function (key) {
if (key === 'card_assets') {
return 'random-test-value';
}
}
});
let serviceStub = sinon.stub(cardAssetService, 'load');
cardAssetService.init();
serviceStub.calledOnce.should.eql(true);
serviceStub.calledWith('random-test-value').should.eql(true);
});
});
describe('Card Asset Service', function () {
let testDir,
srcDir,