Ghost/core/frontend/services/theme-engine/middleware/ensure-active-theme.js
Hannah Wolfe 9f11140ec4
Split theme engine middleware into separate files
- one big file full of stuff is never good for clarity
- separating it out helps us see what requires what
- it also highlights the awful naming and opaque behaviour we have in themes - much to do, but this helps us start
2021-12-01 12:04:36 +00:00

35 lines
1.3 KiB
JavaScript

const errors = require('@tryghost/errors');
const tpl = require('@tryghost/tpl');
const activeTheme = require('../active');
const settingsCache = require('../../../../shared/settings-cache');
const messages = {
missingTheme: 'The currently active theme "{theme}" is missing.'
};
// ### Ensure Active Theme
// Ensure there's a properly set & mounted active theme before attempting to serve a site request
// If there is no active theme, throw an error
// Else, ensure the active theme is mounted
function ensureActiveTheme(req, res, next) {
// CASE: this means that the theme hasn't been loaded yet i.e. there is no active theme
if (!activeTheme.get()) {
// This is the one place we ACTUALLY throw an error for a missing theme as it's a request we cannot serve
return next(new errors.InternalServerError({
// We use the settingsCache here, because the setting will be set,
// even if the theme itself is not usable because it is invalid or missing.
message: tpl(messages.missingTheme, {theme: settingsCache.get('active_theme')})
}));
}
// If the active theme has not yet been mounted, mount it into express
if (!activeTheme.get().mounted) {
activeTheme.get().mount(req.app);
}
next();
}
module.exports = ensureActiveTheme;