2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2018-07-10 19:22:38 +08:00
|
|
|
module.exports = function setLastModifiedHeader () {
|
2018-03-21 21:11:54 +08:00
|
|
|
return function setLastModifiedHeaderMiddleware(req, res, next) {
|
|
|
|
if (req.method !== 'GET') {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
const { mapConfigProvider, cache_buster } = res.locals;
|
|
|
|
|
|
|
|
if (cache_buster) {
|
|
|
|
const cacheBuster = parseInt(cache_buster, 10);
|
2019-10-21 17:13:54 +08:00
|
|
|
const lastModifiedDate = Number.isFinite(cacheBuster) && cacheBuster !== 0 ?
|
|
|
|
new Date(cacheBuster) :
|
|
|
|
new Date();
|
2018-03-21 21:11:54 +08:00
|
|
|
|
2018-03-21 21:43:00 +08:00
|
|
|
res.set('Last-Modified', lastModifiedDate.toUTCString());
|
|
|
|
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2018-03-21 21:11:54 +08:00
|
|
|
mapConfigProvider.getAffectedTables((err, affectedTables) => {
|
|
|
|
if (err) {
|
|
|
|
global.logger.warn('ERROR generating Last Modified Header:', err);
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!affectedTables) {
|
2018-03-21 21:43:00 +08:00
|
|
|
res.set('Last-Modified', new Date().toUTCString());
|
|
|
|
|
2018-03-21 21:11:54 +08:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
const lastUpdatedAt = affectedTables.getLastUpdatedAt();
|
2018-03-21 21:43:00 +08:00
|
|
|
const lastModifiedDate = Number.isFinite(lastUpdatedAt) ? new Date(lastUpdatedAt) : new Date();
|
2018-03-21 21:11:54 +08:00
|
|
|
|
|
|
|
res.set('Last-Modified', lastModifiedDate.toUTCString());
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|