Windshaft-cartodb/lib/api/middlewares/last-modified-header.js

45 lines
1.4 KiB
JavaScript
Raw Normal View History

'use strict';
module.exports = function setLastModifiedHeader () {
2019-10-22 01:07:24 +08:00
return function setLastModifiedHeaderMiddleware (req, res, next) {
if (req.method !== 'GET') {
return next();
}
2019-11-14 18:36:47 +08:00
const { mapConfigProvider, cache_buster: cacheBuster } = res.locals;
2019-11-14 18:36:47 +08:00
if (cacheBuster) {
const cacheBusterTimestamp = parseInt(cacheBuster, 10);
const lastModifiedDate = Number.isFinite(cacheBusterTimestamp) && cacheBusterTimestamp !== 0
? new Date(cacheBusterTimestamp)
2019-10-22 01:07:24 +08:00
: new Date();
res.set('Last-Modified', lastModifiedDate.toUTCString());
return next();
}
mapConfigProvider.getAffectedTables((err, affectedTables) => {
if (err) {
global.logger.warn(err, 'ERROR generating Last Modified Header');
return next();
}
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();
res.set('Last-Modified', lastModifiedDate.toUTCString());
res.locals.cache_buster = lastUpdatedAt;
next();
});
};
};