[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 banner = process.env.BASE_URL + '/api/banner/zaimki.png';
const colour = '#C71585'; const colour = '#C71585';
process.env.LOCALE = locale;
const bodyParser = require('body-parser'); const bodyParser = require('body-parser');
const buildFlags = () => { const buildFlags = () => {

View File

@ -11,7 +11,27 @@ const isBrowser = (userAgent) => {
return isProbablyBrowser || !isProbablyBot; return isProbablyBrowser || !isProbablyBot;
} }
export default function(req, res, next) { const overload = {
res.spa = process.env.NODE_ENV === 'production' && isBrowser(req.headers['user-agent']); en: [[17, 23]],
next() };
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();
} }