Ghost/core/server/web/site/middleware/static-theme.js
Hannah Wolfe 34d2cc1b0b Moved active theme to new theme engine service
refs: bf0823c9a2

- continuing the work of splitting up the theme service into logical components
- this is where it starts to get fiddly as the getActive function in themeService index is required across the frontend/backend mostly due to its use in the getApiVersion method
- for now left one usage of the getActive method in place in ghost-locals middleware ready for the next phase of the refactor, which will move some of the themeService index into a shared location
2021-04-23 15:28:50 +01:00

44 lines
1.2 KiB
JavaScript

const path = require('path');
const config = require('../../../../shared/config');
const constants = require('@tryghost/constants');
const themeEngine = require('../../../../frontend/services/theme-engine');
const express = require('../../../../shared/express');
function isBlackListedFileType(file) {
const blackListedFileTypes = ['.hbs', '.md', '.json'];
const ext = path.extname(file);
return blackListedFileTypes.includes(ext);
}
function isWhiteListedFile(file) {
const whiteListedFiles = ['manifest.json'];
const base = path.basename(file);
return whiteListedFiles.includes(base);
}
function forwardToExpressStatic(req, res, next) {
if (!themeEngine.getActive()) {
return next();
}
const configMaxAge = config.get('caching:theme:maxAge');
express.static(themeEngine.getActive().path,
{maxAge: (configMaxAge || configMaxAge === 0) ? configMaxAge : constants.ONE_YEAR_MS}
)(req, res, next);
}
function staticTheme() {
return function blackListStatic(req, res, next) {
if (!isWhiteListedFile(req.path) && isBlackListedFileType(req.path)) {
return next();
}
return forwardToExpressStatic(req, res, next);
};
}
module.exports = staticTheme;