From 1e3b46ef3c833aafcd10acf4943dd5f931f728a3 Mon Sep 17 00:00:00 2001 From: Avris Date: Fri, 9 Jul 2021 20:42:39 +0200 Subject: [PATCH] [ssr] disable even for bots, if en and period of high load --- nuxt.config.js | 2 ++ server/no-ssr.js | 26 +++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/nuxt.config.js b/nuxt.config.js index 9f8290e3..d88d446a 100644 --- a/nuxt.config.js +++ b/nuxt.config.js @@ -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 = () => { diff --git a/server/no-ssr.js b/server/no-ssr.js index f364d0f8..d1d72700 100644 --- a/server/no-ssr.js +++ b/server/no-ssr.js @@ -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(); }