2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2018-07-10 19:22:38 +08:00
|
|
|
module.exports = function setLastModifiedHeader () {
|
2019-10-22 01:07:24 +08:00
|
|
|
return function setLastModifiedHeaderMiddleware (req, res, next) {
|
2018-03-21 21:11:54 +08:00
|
|
|
if (req.method !== 'GET') {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2019-11-14 18:36:47 +08:00
|
|
|
const { mapConfigProvider, cache_buster: cacheBuster } = res.locals;
|
2018-03-21 21:11:54 +08:00
|
|
|
|
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();
|
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();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|