[ssr] disable even for bots, if en and period of high load

This commit is contained in:
Avris 2021-07-09 20:42:39 +02:00
parent 36f2ae771d
commit 1e3b46ef3c
2 changed files with 25 additions and 3 deletions

View File

@ -12,6 +12,8 @@ const description = translations.description;
const banner = process.env.BASE_URL + '/api/banner/zaimki.png';
const colour = '#C71585';
process.env.LOCALE = locale;
const bodyParser = require('body-parser');
const buildFlags = () => {

View File

@ -11,7 +11,27 @@ const isBrowser = (userAgent) => {
return isProbablyBrowser || !isProbablyBot;
}
export default function(req, res, next) {
res.spa = process.env.NODE_ENV === 'production' && isBrowser(req.headers['user-agent']);
next()
const overload = {
en: [[17, 23]],
};
const isHighLoad = (timestamp, overloadPeriods) => {
if (overloadPeriods === undefined) {
return false;
}
for (let [periodStart, periodEnd] of overloadPeriods) {
if (timestamp.getUTCHours() >= periodStart && timestamp.getUTCHours() < periodEnd) {
return true;
}
}
return false;
}
export default function(req, res, next) {
if (process.env.NODE_ENV === 'production') {
res.spa = isBrowser(req.headers['user-agent']) || isHighLoad(new Date, overload[process.env.LOCALE]);
}
next();
}