46 lines
1.4 KiB
JavaScript
46 lines
1.4 KiB
JavaScript
'use strict';
|
|
|
|
module.exports = function setLastModifiedHeader () {
|
|
return function setLastModifiedHeaderMiddleware (req, res, next) {
|
|
if (req.method !== 'GET') {
|
|
return next();
|
|
}
|
|
|
|
const { mapConfigProvider, cache_buster: cacheBuster, logger } = res.locals;
|
|
|
|
if (cacheBuster) {
|
|
const cacheBusterTimestamp = parseInt(cacheBuster, 10);
|
|
const lastModifiedDate = Number.isFinite(cacheBusterTimestamp) && cacheBusterTimestamp !== 0
|
|
? new Date(cacheBusterTimestamp)
|
|
: new Date();
|
|
|
|
res.set('Last-Modified', lastModifiedDate.toUTCString());
|
|
|
|
return next();
|
|
}
|
|
|
|
mapConfigProvider.getAffectedTables((err, affectedTables) => {
|
|
if (err) {
|
|
err.message = `Error generating Last Modified Header. ${err.message}`;
|
|
logger.warn({ error: err });
|
|
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();
|
|
});
|
|
};
|
|
};
|