2019-07-27 00:42:09 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-15 19:11:49 +08:00
|
|
|
const ONE_MINUTE_IN_SECONDS = 60;
|
|
|
|
const THREE_MINUTE_IN_SECONDS = 60 * 3;
|
|
|
|
const FIVE_MINUTES_IN_SECONDS = ONE_MINUTE_IN_SECONDS * 5;
|
|
|
|
const TEN_MINUTES_IN_SECONDS = ONE_MINUTE_IN_SECONDS * 10;
|
|
|
|
const FIFTEEN_MINUTES_IN_SECONDS = ONE_MINUTE_IN_SECONDS * 15;
|
|
|
|
const THIRTY_MINUTES_IN_SECONDS = ONE_MINUTE_IN_SECONDS * 30;
|
|
|
|
const ONE_HOUR_IN_SECONDS = ONE_MINUTE_IN_SECONDS * 60;
|
|
|
|
const ONE_YEAR_IN_SECONDS = ONE_HOUR_IN_SECONDS * 24 * 365;
|
|
|
|
|
2019-07-27 00:42:09 +08:00
|
|
|
const defaultCacheTTL = {
|
|
|
|
ttl: ONE_YEAR_IN_SECONDS,
|
|
|
|
fallbackTtl: FIVE_MINUTES_IN_SECONDS
|
|
|
|
};
|
2019-10-15 19:11:49 +08:00
|
|
|
|
|
|
|
const validFallbackTTL = [
|
|
|
|
ONE_MINUTE_IN_SECONDS,
|
|
|
|
THREE_MINUTE_IN_SECONDS,
|
|
|
|
FIVE_MINUTES_IN_SECONDS,
|
|
|
|
TEN_MINUTES_IN_SECONDS,
|
|
|
|
FIFTEEN_MINUTES_IN_SECONDS,
|
|
|
|
THIRTY_MINUTES_IN_SECONDS,
|
|
|
|
ONE_HOUR_IN_SECONDS
|
|
|
|
];
|
|
|
|
|
|
|
|
const { ttl, fallbackTtl } = Object.assign(defaultCacheTTL, global.settings.cache);
|
2019-07-27 00:42:09 +08:00
|
|
|
|
|
|
|
module.exports = function cacheControlHeader () {
|
2019-10-15 19:11:49 +08:00
|
|
|
if (!validFallbackTTL.includes(fallbackTtl)) {
|
|
|
|
throw new Error(`Invalid fallback TTL value for Cache-Control header. Got ${fallbackTtl}, expected ${validFallbackTTL.join(', ')}`);
|
|
|
|
}
|
|
|
|
|
2019-07-27 00:42:09 +08:00
|
|
|
return function cacheControlHeaderMiddleware (req, res, next) {
|
|
|
|
const { cachePolicy } = res.locals.params;
|
|
|
|
const { affectedTables, mayWrite } = res.locals;
|
|
|
|
|
|
|
|
if (cachePolicy === 'persist') {
|
|
|
|
res.header('Cache-Control', `public,max-age=${ONE_YEAR_IN_SECONDS}`);
|
|
|
|
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (affectedTables && affectedTables.getTables().every(table => table.updated_at !== null)) {
|
2019-10-15 19:11:49 +08:00
|
|
|
const maxAge = mayWrite ? 0 : ttl;
|
2019-07-27 00:42:09 +08:00
|
|
|
res.header('Cache-Control', `no-cache,max-age=${maxAge},must-revalidate,public`);
|
|
|
|
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2019-10-15 19:11:49 +08:00
|
|
|
const maxAge = fallbackTtl;
|
|
|
|
res.header('Cache-Control', `no-cache,max-age=${computeNextTTL({ ttlInSeconds: maxAge })},must-revalidate,public`);
|
2019-07-27 00:42:09 +08:00
|
|
|
|
|
|
|
return next();
|
|
|
|
};
|
|
|
|
};
|
2019-10-15 19:11:49 +08:00
|
|
|
|
|
|
|
function computeNextTTL ({ ttlInSeconds } = {}) {
|
|
|
|
const nowInSeconds = Math.ceil(Date.now() / 1000);
|
|
|
|
const secondsAfterPreviousTTLStep = nowInSeconds % ttlInSeconds;
|
|
|
|
const secondsToReachTheNextTTLStep = ttlInSeconds - secondsAfterPreviousTTLStep;
|
|
|
|
|
|
|
|
return secondsToReachTheNextTTLStep;
|
|
|
|
}
|