Skip to content

Commit

Permalink
Merge pull request #22 from Financial-Times/hashed-asset-helper
Browse files Browse the repository at this point in the history
hack together a hashed asset helper
  • Loading branch information
matthew-andrews committed Jan 29, 2015
2 parents 0742427 + 8e19288 commit 1700647
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var topicUrl = require('./src/handlebars/topic-url');
var dateformat = require('./src/handlebars/dateformat');
var resize = require('./src/handlebars/resize');
var encode = require('./src/handlebars/encode');
var hashedAsset = require('./src/handlebars/hashed-asset');

var flagsPromise = flags.init();

Expand Down Expand Up @@ -52,13 +53,15 @@ module.exports = function(options) {
app.locals.__name = name = normalizeName(name);
app.locals.__environment = process.env.NODE_ENV || '';
app.locals.__isProduction = app.locals.__environment.toUpperCase() === 'PRODUCTION';
app.locals.__rootDirectory = directory;
helpers.resize = resize;
helpers.dateformat = dateformat;
helpers.paragraphs = paragraphs;
helpers.removeImageTags = removeImageTags;
helpers.ifEquals = ifEquals;
helpers.ifAll = ifAll;
helpers.encode = encode;
helpers.hashedAsset = hashedAsset;
helpers.topicUrl = topicUrl;

app.use('/' + name, express.static(directory + '/public', {
Expand Down
18 changes: 18 additions & 0 deletions src/handlebars/hashed-asset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use strict";
module.exports = function(options) {
var assetHash;
var file = options.fn(this);
try {
assetHash = require(options.data.root.__rootDirectory + '/public/' + file + '-asset-hash.json')[file];
} catch(err) {
if (process.env.NODE_ENV === 'production') {
throw new Error("./public/" + file + "-asset-hash.json not found, required for proper working of the application in production");
} else {
console.warn("./public/" + file + "-asset-hash.json not found. Falling back to un-fingerprinted files.");
}
}
if (assetHash) {
file = assetHash;
}
return '/' + options.data.root.__name+'/' + file;
};

0 comments on commit 1700647

Please sign in to comment.