diff --git a/lib/cartodb/controllers/named_maps.js b/lib/cartodb/controllers/named_maps.js index b0ba0278..9afaf530 100644 --- a/lib/cartodb/controllers/named_maps.js +++ b/lib/cartodb/controllers/named_maps.js @@ -319,6 +319,7 @@ NamedMapsController.prototype.instantiateTemplate = function(req, res, template_ } var tplhash = self.templateMaps.fingerPrint(template).substring(0,8); layergroup.layergroupid = cdbuser + '@' + tplhash + '@' + layergroup.layergroupid; + res.header('X-Layergroup-Id', layergroup.layergroupid); self.surrogateKeysCache.tag(res, new NamedMapsCacheEntry(cdbuser, template.name)); diff --git a/lib/cartodb/server_options.js b/lib/cartodb/server_options.js index 7dc1e905..4b2c595d 100644 --- a/lib/cartodb/server_options.js +++ b/lib/cartodb/server_options.js @@ -362,17 +362,22 @@ module.exports = function(redisPool) { var cacheChannel = me.buildCacheChannel(dbName, result.affectedTables); me.channelCache[cacheKey] = cacheChannel; - if (req.res && req.method == 'GET') { - var res = req.res; - var ttl = global.environment.varnish.layergroupTtl || 86400; - res.header('Cache-Control', 'public,max-age='+ttl+',must-revalidate'); - res.header('Last-Modified', (new Date()).toUTCString()); - res.header('X-Cache-Channel', cacheChannel); - } - // last update for layergroup cache buster response.layergroupid = response.layergroupid + ':' + result.lastUpdatedTime; response.last_updated = new Date(result.lastUpdatedTime).toISOString(); + + var res = req.res; + if (res) { + if (req.method === 'GET') { + var ttl = global.environment.varnish.layergroupTtl || 86400; + res.header('Cache-Control', 'public,max-age='+ttl+',must-revalidate'); + res.header('Last-Modified', (new Date()).toUTCString()); + res.header('X-Cache-Channel', cacheChannel); + } + + res.header('X-Layergroup-Id', response.layergroupid); + } + return null; }, function finish(err) { diff --git a/test/acceptance/multilayer.js b/test/acceptance/multilayer.js index 38256aef..28ec7419 100644 --- a/test/acceptance/multilayer.js +++ b/test/acceptance/multilayer.js @@ -69,6 +69,7 @@ suite(suiteName, function() { assert.equal(parsedBody.last_updated, expected_last_updated); if ( expected_token ) { assert.equal(parsedBody.layergroupid, expected_token + ':' + expected_last_updated_epoch); + assert.equal(res.headers['x-layergroup-id'], parsedBody.layergroupid); } else expected_token = parsedBody.layergroupid.split(':')[0]; next(null, res); @@ -1011,6 +1012,7 @@ suite(suiteName, function() { var parsedBody = JSON.parse(res.body); if ( expected_token ) { assert.equal(parsedBody.layergroupid, expected_token + ':' + expected_last_updated_epoch); + assert.equal(res.headers['x-layergroup-id'], parsedBody.layergroupid); } else { var token_components = parsedBody.layergroupid.split(':'); @@ -1091,6 +1093,7 @@ suite(suiteName, function() { var parsedBody = JSON.parse(res.body); if ( expected_token ) { assert.equal(parsedBody.layergroupid, expected_token + ':' + expected_last_updated_epoch); + assert.equal(res.headers['x-layergroup-id'], parsedBody.layergroupid); } else { var token_components = parsedBody.layergroupid.split(':'); diff --git a/test/acceptance/multilayer_server.js b/test/acceptance/multilayer_server.js index e2427543..7cf715dc 100644 --- a/test/acceptance/multilayer_server.js +++ b/test/acceptance/multilayer_server.js @@ -108,6 +108,7 @@ describe('tests from old api translated to multilayer', function() { function(res) { var parsed = JSON.parse(res.body); assert.ok(parsed.layergroupid); + assert.equal(res.headers['x-layergroup-id'], parsed.layergroupid); done(); } ); @@ -128,6 +129,7 @@ describe('tests from old api translated to multilayer', function() { function(res) { var parsed = JSON.parse(res.body); assert.ok(parsed.layergroupid); + assert.equal(res.headers['x-layergroup-id'], parsed.layergroupid); global.environment.postgres.host = backupDBHost; done(); @@ -150,6 +152,7 @@ describe('tests from old api translated to multilayer', function() { function(res) { var parsed = JSON.parse(res.body); assert.ok(parsed.layergroupid); + assert.equal(res.headers['x-layergroup-id'], parsed.layergroupid); global.environment.postgres_auth_pass = backupDBPass; done(); @@ -246,6 +249,8 @@ describe('tests from old api translated to multilayer', function() { assert.ok(res.headers.hasOwnProperty('x-cache-channel')); assert.equal(res.headers['x-cache-channel'], expectedCacheChannel); + assert.equal(res.headers['x-layergroup-id'], parsed.layergroupid); + done(); } ); diff --git a/test/acceptance/templates.js b/test/acceptance/templates.js index 9196c75d..71c5e667 100644 --- a/test/acceptance/templates.js +++ b/test/acceptance/templates.js @@ -1321,6 +1321,7 @@ describe('template_api', function() { layergroupid = parsed.layergroupid; assert.ok(layergroupid.match(/^localhost@/), "Returned layergroupid does not start with signer name: " + layergroupid); + assert.equal(res.headers['x-layergroup-id'], parsed.layergroupid); assert.ok(parsed.hasOwnProperty('last_updated'), "Missing 'last_updated' from response body: " + res.body); // TODO: check value of last_updated ?