diff --git a/bin/browserid b/bin/browserid index 77b89ecdb14a896475fc591c776fe869e6d69d15..6f23273cbcb5e5c86adc231d3831e548b7175c6b 100755 --- a/bin/browserid +++ b/bin/browserid @@ -138,7 +138,7 @@ var static_root = path.join(__dirname, "..", "resources", "static"); app.use(cachify.setup(assets(config.get('supported_languages')), { - prefix: 'v', + prefix: config.get('cachify_prefix'), production: config.get('use_minified_resources'), root: static_root, })); diff --git a/lib/configuration.js b/lib/configuration.js index ab8ca16037197cc166a9e2cf0e62c099792593ca..eca775e276a164affb00a5fa666e676a9b37f779 100644 --- a/lib/configuration.js +++ b/lib/configuration.js @@ -67,6 +67,10 @@ var conf = module.exports = convict({ doc: "The scheme of the public URL. Calculated from the latter.", format: "string", }, + cachify_prefix: { + doc: "The prefix for cachify hashes in URLs", + format: 'string = "v"' + }, use_minified_resources: { doc: "Should the server serve minified resources?", format: 'boolean = true', diff --git a/scripts/compress-worker.js b/scripts/compress-worker.js index 0cf43ce21f143f5a9a9af75cc387babd7a4687aa..4c9616bffe4a048a05e3cbcd4288ef65f60fb2ad 100644 --- a/scripts/compress-worker.js +++ b/scripts/compress-worker.js @@ -1,6 +1,9 @@ const +cachify = require('connect-cachify'), +config = require('../lib/configuration.js'), fs = require('fs'), jsp = require("uglify-js").parser, +logger = require('../lib/logging.js').logger, pro = require("uglify-js").uglify, uglifycss = require('uglifycss'), mkdirp = require('mkdirp'), @@ -9,6 +12,12 @@ path = require('path'); function compressResource(staticPath, name, files, cb) { var orig_code = ""; var info = undefined; + + // Cachify only used in compress for CSS Images, so no asserts needed + cachify.setup({}, { + prefix: config.get('cachify_prefix'), + root: staticPath + }); function writeFile(final_code) { mkdirp(path.join(staticPath, path.dirname(name)), function (err) { if (err) cb(err); @@ -31,7 +40,8 @@ function compressResource(staticPath, name, files, cb) { final_code = pro.split_lines(pro.gen_code(ast), 32 * 1024); // compressed code here } else if (/\.css$/.test(name)) { // compress css - final_code = uglifycss.processString(orig_code); + var cach_code = cachify_embedded(orig_code); + final_code = uglifycss.processString(cach_code); } else { return cb("can't determine content type: " + name); } @@ -78,6 +88,15 @@ function compressResource(staticPath, name, files, cb) { isBuildNeeded(); } +function cachify_embedded (css_src) { + return css_src.replace(/url\s*\(['"](.*)\s*['"]\s*\)/g, function (str, url) { + // This will throw an error if url doesn't exist. This is good as we will + // catch typos during build. + logger.info("For " + str + " making " + url + " into " + cachify.cachify(url)); + return "url('" + cachify.cachify(url) + "')"; + }); +} + process.on('message', function(m) { var startTime = new Date;