diff --git a/lib/cartodb/controllers/layergroup.js b/lib/cartodb/controllers/layergroup.js index b7b6803a..9da86ff4 100644 --- a/lib/cartodb/controllers/layergroup.js +++ b/lib/cartodb/controllers/layergroup.js @@ -11,6 +11,7 @@ const rateLimit = require('../middleware/rate-limit'); const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimit; const cacheChannelHeader = require('../middleware/cache-channel-header'); const surrogateKeyHeader = require('../middleware/surrogate-key-header'); +const lastModifiedHeader = require('../middleware/last-modified-header'); const sendResponse = require('../middleware/send-response'); const DataviewBackend = require('../backends/dataview'); const AnalysisStatusBackend = require('../backends/analysis-status'); @@ -87,9 +88,9 @@ LayergroupController.prototype.register = function(app) { ), getTile(this.tileBackend, 'map_tile'), setCacheControlHeader(), - setLastModifiedHeader(), cacheChannelHeader(), surrogateKeyHeader({ surrogateKeysCache: this.surrogateKeysCache }), + lastModifiedHeader(), incrementSuccessMetrics(global.statsClient), sendResponse(), incrementErrorMetrics(global.statsClient), @@ -116,9 +117,9 @@ LayergroupController.prototype.register = function(app) { ), getTile(this.tileBackend, 'map_tile'), setCacheControlHeader(), - setLastModifiedHeader(), cacheChannelHeader(), surrogateKeyHeader({ surrogateKeysCache: this.surrogateKeysCache }), + lastModifiedHeader(), incrementSuccessMetrics(global.statsClient), sendResponse(), incrementErrorMetrics(global.statsClient), @@ -146,9 +147,9 @@ LayergroupController.prototype.register = function(app) { ), getTile(this.tileBackend, 'maplayer_tile'), setCacheControlHeader(), - setLastModifiedHeader(), cacheChannelHeader(), surrogateKeyHeader({ surrogateKeysCache: this.surrogateKeysCache }), + lastModifiedHeader(), incrementSuccessMetrics(global.statsClient), sendResponse(), incrementErrorMetrics(global.statsClient), @@ -175,9 +176,9 @@ LayergroupController.prototype.register = function(app) { ), getFeatureAttributes(this.attributesBackend), setCacheControlHeader(), - setLastModifiedHeader(), cacheChannelHeader(), surrogateKeyHeader({ surrogateKeysCache: this.surrogateKeysCache }), + lastModifiedHeader(), sendResponse() ); @@ -203,9 +204,9 @@ LayergroupController.prototype.register = function(app) { ), getPreviewImageByCenter(this.previewBackend), setCacheControlHeader(), - setLastModifiedHeader(), cacheChannelHeader(), surrogateKeyHeader({ surrogateKeysCache: this.surrogateKeysCache }), + lastModifiedHeader(), sendResponse() ); @@ -229,9 +230,9 @@ LayergroupController.prototype.register = function(app) { ), getPreviewImageByBoundingBox(this.previewBackend), setCacheControlHeader(), - setLastModifiedHeader(), cacheChannelHeader(), surrogateKeyHeader({ surrogateKeysCache: this.surrogateKeysCache }), + lastModifiedHeader(), sendResponse() ); @@ -272,9 +273,9 @@ LayergroupController.prototype.register = function(app) { ), getDataview(this.dataviewBackend), setCacheControlHeader(), - setLastModifiedHeader(), cacheChannelHeader(), surrogateKeyHeader({ surrogateKeysCache: this.surrogateKeysCache }), + lastModifiedHeader(), sendResponse() ); @@ -297,9 +298,9 @@ LayergroupController.prototype.register = function(app) { ), getDataview(this.dataviewBackend), setCacheControlHeader(), - setLastModifiedHeader(), cacheChannelHeader(), surrogateKeyHeader({ surrogateKeysCache: this.surrogateKeysCache }), + lastModifiedHeader(), sendResponse() ); @@ -322,9 +323,9 @@ LayergroupController.prototype.register = function(app) { ), dataviewSearch(this.dataviewBackend), setCacheControlHeader(), - setLastModifiedHeader(), cacheChannelHeader(), surrogateKeyHeader({ surrogateKeysCache: this.surrogateKeysCache }), + lastModifiedHeader(), sendResponse() ); @@ -347,9 +348,9 @@ LayergroupController.prototype.register = function(app) { ), dataviewSearch(this.dataviewBackend), setCacheControlHeader(), - setLastModifiedHeader(), cacheChannelHeader(), surrogateKeyHeader({ surrogateKeysCache: this.surrogateKeysCache }), + lastModifiedHeader(), sendResponse() ); @@ -609,20 +610,6 @@ function getPreviewImageByBoundingBox (previewBackend) { }; } -function setLastModifiedHeader () { - return function setLastModifiedHeaderMiddleware (req, res, next) { - let { cache_buster: cacheBuster } = res.locals; - - cacheBuster = parseInt(cacheBuster, 10); - - const lastUpdated = res.locals.cache_buster ? new Date(cacheBuster) : new Date(); - - res.set('Last-Modified', lastUpdated.toUTCString()); - - next(); - }; -} - function setCacheControlHeader () { return function setCacheControlHeaderMiddleware (req, res, next) { res.set('Cache-Control', 'public,max-age=31536000'); diff --git a/lib/cartodb/controllers/map.js b/lib/cartodb/controllers/map.js index ea8b309a..670ed9d7 100644 --- a/lib/cartodb/controllers/map.js +++ b/lib/cartodb/controllers/map.js @@ -13,6 +13,7 @@ const dbConnSetup = require('../middleware/db-conn-setup'); const authorize = require('../middleware/authorize'); const cacheChannelHeader = require('../middleware/cache-channel-header'); const surrogateKeyHeader = require('../middleware/surrogate-key-header'); +const lastModifiedHeader = require('../middleware/last-modified-header'); const sendResponse = require('../middleware/send-response'); const NamedMapMapConfigProvider = require('../models/mapconfig/provider/named-map-provider'); const CreateLayergroupMapConfigProvider = require('../models/mapconfig/provider/create-layergroup-provider'); @@ -115,7 +116,7 @@ MapController.prototype.composeCreateMapMiddleware = function (endpointGroup, us augmentLayergroupData(), cacheChannelHeader(), surrogateKeyHeader({ surrogateKeysCache: this.surrogateKeysCache }), - setLastModified(), + lastModifiedHeader({ now: true }), setLastUpdatedTimeToLayergroup(), setCacheControl(), setLayerStats(this.pgConnection, this.statsBackend), @@ -400,16 +401,6 @@ function augmentLayergroupData () { }; } -function setLastModified () { - return function setLastModifiedMiddleware (req, res, next) { - if (req.method === 'GET') { - res.set('Last-Modified', (new Date()).toUTCString()); - } - - next(); - }; -} - function setLastUpdatedTimeToLayergroup () { return function setLastUpdatedTimeToLayergroupMiddleware (req, res, next) { const { mapConfigProvider, analysesResults } = res.locals; diff --git a/lib/cartodb/middleware/last-modified-header.js b/lib/cartodb/middleware/last-modified-header.js index 51211661..18c0d961 100644 --- a/lib/cartodb/middleware/last-modified-header.js +++ b/lib/cartodb/middleware/last-modified-header.js @@ -1,4 +1,4 @@ -module.exports = function setLastModifiedHeader () { +module.exports = function setLastModifiedHeader ({ now = false } = {}) { return function setLastModifiedHeaderMiddleware(req, res, next) { if (req.method !== 'GET') { return next(); @@ -8,10 +8,16 @@ module.exports = function setLastModifiedHeader () { if (cache_buster) { const cacheBuster = parseInt(cache_buster, 10); + const lastModifiedDate = Number.isFinite(cacheBuster) ? new Date(cacheBuster) : new Date(); - if (Number.isFinite(cacheBuster)) { - res.set('Last-Modified', new Date(cacheBuster).toUTCString()); - } + res.set('Last-Modified', lastModifiedDate.toUTCString()); + + return next(); + } + + // REVIEW: to keep 100% compatibility with maps controller + if (now) { + res.set('Last-Modified', new Date().toUTCString()); return next(); } @@ -23,14 +29,13 @@ module.exports = function setLastModifiedHeader () { } if (!affectedTables) { + res.set('Last-Modified', new Date().toUTCString()); + return next(); } const lastUpdatedAt = affectedTables.getLastUpdatedAt(); - - const lastModifiedDate = Number.isFinite(lastUpdatedAt) ? - new Date(lastUpdatedAt) : - new Date(); + const lastModifiedDate = Number.isFinite(lastUpdatedAt) ? new Date(lastUpdatedAt) : new Date(); res.set('Last-Modified', lastModifiedDate.toUTCString());