From a4b3b3b47e5a71fa7d96e9623fabe8b1ccaa689d Mon Sep 17 00:00:00 2001 From: Lloyd Hilaiel <lloyd@hilaiel.com> Date: Mon, 23 Jan 2012 16:14:44 -0700 Subject: [PATCH] add Vary headers to help downstream caching proxies optimize caching of static resources - closes #938 --- bin/browserid | 26 ++++++++++++++++---------- lib/browserid/views.js | 3 ++- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/bin/browserid b/bin/browserid index c2eb01539..70fdce33c 100755 --- a/bin/browserid +++ b/bin/browserid @@ -142,9 +142,6 @@ wsapi.setup({ // #9 - handle views for dynamicish content views.setup(app); -// #10 - if nothing else has caught this request, serve static files -app.use(express.static(path.join(__dirname, "..", "resources", "static"))); - function doShutdown(readyForShutdownCB) { require('../lib/bcrypt.js').shutdown(); db.close(readyForShutdownCB) @@ -155,13 +152,6 @@ function doShutdown(readyForShutdownCB) { // the update logic shutdown.installUpdateHandler(app, doShutdown); -// #11.5 - custom 404 -app.use(function(req, res,next) { - res.statusCode = 404; - res.write("Cannot find this resource"); - res.end(); -}); - // #12 if the BROWSERID_FAKE_VERIFICATION env var is defined, we'll include // fake_verification.js. This is used during testing only and should // never be included in a production deployment @@ -169,6 +159,22 @@ if (process.env['BROWSERID_FAKE_VERIFICATION']) { require('../lib/browserid/fake_verification.js').addVerificationWSAPI(app); } +// if nothing else has caught this request, serve static files, but ensure +// that proper vary headers are installed to prevent unwanted caching +app.use(function(req, res, next) { + res.setHeader('Vary', 'Accept-Encoding,Accept-Language'); + next(); +}); + +app.use(express.static(path.join(__dirname, "..", "resources", "static"))); + +// custom 404 page +app.use(function(req, res,next) { + res.statusCode = 404; + res.write("Cannot find this resource"); + res.end(); +}); + // open the databse db.open(config.get('database'), function (error) { if (error) { diff --git a/lib/browserid/views.js b/lib/browserid/views.js index 7fba49f66..a1abaffae 100644 --- a/lib/browserid/views.js +++ b/lib/browserid/views.js @@ -25,6 +25,7 @@ const VIEW_PATH = path.join(__dirname, "..", "..", "resources", "views"); 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') === 'production') { // res.setHeader('Cache-Control', 'public, max-age=' + (maxAge / 1000)); res.setHeader('Last-Modified', stat.mtime.toUTCString()); @@ -32,7 +33,7 @@ function renderCachableView(req, res, template, options) { } else { res.setHeader('Cache-Control', 'no-store'); } - res.setHeader('Content-Type', 'text/html; charset=utf8'); + res.setHeader('Content-Type', 'text/html; charset=utf8'); if (connect.utils.conditionalGET(req)) { if (!connect.utils.modified(req, res)) { return connect.utils.notModified(res); -- GitLab