0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

Added middleware for serving minified card assets

- Wired up the forntend to include and serve the minified css and js card assets if they exist
- This is a very naive implementation - ideally we wouldn't have to inject this in multiple places
- This allows us to add files to src/cards and have them included in themes
- The system is currently disabled due to an override in the theme config setting assets to false
This commit is contained in:
Hannah Wolfe 2021-11-04 12:00:33 +00:00
parent 64e8fdf4bb
commit e4c1e0d938
No known key found for this signature in database
GPG key ID: 9F8C7532D0A6BA55
3 changed files with 21 additions and 1 deletions

View file

@ -5,12 +5,16 @@
const {metaData, settingsCache, config, blogIcon, urlUtils, labs} = require('../services/proxy');
const {escapeExpression, SafeString} = require('../services/rendering');
// BAD REQUIRE
// @TODO fix this require
const cardAssetService = require('../services/card-assets');
const logging = require('@tryghost/logging');
const _ = require('lodash');
const debug = require('@tryghost/debug')('ghost_head');
const templateStyles = require('./tpl/styles');
const getMetaData = metaData.get;
const {get: getMetaData, getAssetUrl} = metaData;
function writeMetaTag(property, content, type) {
type = type || property.substring(0, 7) === 'twitter' ? 'name' : 'property';
@ -193,6 +197,14 @@ module.exports = function ghost_head(options) { // eslint-disable-line camelcase
if (!_.includes(context, 'amp')) {
head.push(getMembersHelper(options.data));
// @TODO do this in a more "frameworky" way
if (cardAssetService.hasFile('js')) {
head.push(`<script async src="${getAssetUrl('public/cards.min.js')}"></script>`);
}
if (cardAssetService.hasFile('css')) {
head.push(`<link rel="stylesheet" type="text/css" href="${getAssetUrl('public/cards.min.css')}">`);
}
if (!_.isEmpty(globalCodeinjection)) {
head.push(globalCodeinjection);
}

View file

@ -74,6 +74,10 @@ class CardAssetService {
}
}
hasFile(type) {
return this.files.indexOf(`cards.min.${type}`) > -1;
}
/**
* A theme can declare which cards it supports, and we'll do the rest
*

View file

@ -109,6 +109,10 @@ module.exports = function setupSiteApp(options = {}) {
siteApp.use(mw.servePublicFile('public/ghost.css', 'text/css', constants.ONE_HOUR_S));
siteApp.use(mw.servePublicFile('public/ghost.min.css', 'text/css', constants.ONE_YEAR_S));
// Card assets
siteApp.use(mw.servePublicFile('public/cards.min.css', 'text/css', constants.ONE_YEAR_S));
siteApp.use(mw.servePublicFile('public/cards.min.js', 'text/js', constants.ONE_YEAR_S));
// Serve blog images using the storage adapter
siteApp.use(STATIC_IMAGE_URL_PREFIX, mw.handleImageSizes, storage.getStorage('images').serve());
// Serve blog media using the storage adapter