Skip to content
Snippets Groups Projects
Commit edd34ebb authored by Lloyd Hilaiel's avatar Lloyd Hilaiel
Browse files

use etagify for content-based hashes in ETag headers - issue #1331

parent 1971ecc8
No related branches found
No related tags found
No related merge requests found
......@@ -9,6 +9,6 @@
"driver": "json"
},
"express_log_format": "dev_bid",
"email_to_console": true
"email_to_console": true,
"env": "local"
}
......@@ -11,14 +11,14 @@ connect = require('connect'),
config = require('../configuration.js'),
und = require('underscore'),
util = require('util'),
httputils = require('../httputils.js');
httputils = require('../httputils.js'),
etagify = require('etagify');
// all templated content, redirects, and renames are handled here.
// anything that is not an api, and not static
const
path = require('path');
const VIEW_PATH = path.join(__dirname, "..", "..", "resources", "views");
// none of our views include dynamic data. all of them should be served
......@@ -26,27 +26,23 @@ const VIEW_PATH = path.join(__dirname, "..", "..", "resources", "views");
// cache headers maximally leveraging the same logic that connect uses
// issue #910
function renderCachableView(req, res, template, options) {
fs.stat(path.join(VIEW_PATH, template), function (err, stat) {
res.setHeader('Date', new Date().toUTCString());
res.setHeader('Vary', 'Accept-Encoding,Accept-Language');
if (config.get('env') !== 'local') {
// allow caching, but require revalidation via ETag
res.setHeader('Cache-Control', 'public, max-age=0');
res.setHeader('ETag', util.format('"%s-%s-%s"', stat.size, Number(stat.mtime), req.lang));
} else {
res.setHeader('Cache-Control', 'no-store');
}
res.setHeader('Content-Type', 'text/html; charset=utf8');
if (connect.utils.conditionalGET(req)) {
if (!connect.utils.modified(req, res)) {
return connect.utils.notModified(res);
}
}
res.render(template, options);
});
if (config.get('env') !== 'local') {
// allow caching, but require revalidation via ETag
res.etagify();
res.setHeader('Cache-Control', 'public, max-age=0');
} else {
// disable all caching for local dev
res.setHeader('Cache-Control', 'no-store');
}
res.setHeader('Date', new Date().toUTCString());
res.setHeader('Vary', 'Accept-Encoding,Accept-Language');
res.setHeader('Content-Type', 'text/html; charset=utf8');
res.render(template, options);
}
exports.setup = function(app) {
app.use(etagify());
app.set("views", VIEW_PATH);
app.set('view options', {
......
......@@ -14,6 +14,7 @@
"connect-cookie-session": "0.0.2",
"connect-logger-statsd": "0.0.1",
"ejs": "0.4.3",
"etagify": "0.0.1",
"express": "2.5.0",
"iconv": "1.1.3",
"jwcrypto": "0.1.1",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment