[optim] dynamically disable ssr for browsers

This commit is contained in:
Avris 2021-06-26 00:51:43 +02:00
parent 7c39ce9b66
commit 54622b54bb
2 changed files with 18 additions and 1 deletions

View File

@ -155,7 +155,7 @@ export default {
BUCKET: `https://${process.env.AWS_S3_BUCKET}.s3-${process.env.AWS_REGION}.amazonaws.com`,
STATS_FILE: process.env.STATS_FILE,
},
serverMiddleware: ['~/server/index.js'],
serverMiddleware: ['~/server/no-ssr.js', '~/server/index.js'],
axios: {
baseURL: process.env.BASE_URL + '/api',
},

17
server/no-ssr.js Normal file
View File

@ -0,0 +1,17 @@
const USER_AGENT_BOTS = /bot|crawler|baiduspider|80legs|ia_archiver|voyager|curl|wget|yahoo! slurp|mediapartners-google|facebookexternalhit|twitterbot|whatsapp|php|python/;
const USER_AGENT_BROWSERS = /mozilla|msie|gecko|firefox|edge|opera|safari|netscape|konqueror|android/;
const isBrowser = (userAgent) => {
if (!userAgent) {
return false;
}
const isProbablyBot = !!userAgent.toLowerCase().match(USER_AGENT_BOTS);
const isProbablyBrowser = !!userAgent.toLowerCase().match(USER_AGENT_BROWSERS);
return isProbablyBrowser || !isProbablyBot;
}
export default function(req, res, next) {
res.spa = isBrowser(req.headers['user-agent']);
next()
}