[optim] dynamically disable ssr for browsers
This commit is contained in:
parent
7c39ce9b66
commit
54622b54bb
|
@ -155,7 +155,7 @@ export default {
|
||||||
BUCKET: `https://${process.env.AWS_S3_BUCKET}.s3-${process.env.AWS_REGION}.amazonaws.com`,
|
BUCKET: `https://${process.env.AWS_S3_BUCKET}.s3-${process.env.AWS_REGION}.amazonaws.com`,
|
||||||
STATS_FILE: process.env.STATS_FILE,
|
STATS_FILE: process.env.STATS_FILE,
|
||||||
},
|
},
|
||||||
serverMiddleware: ['~/server/index.js'],
|
serverMiddleware: ['~/server/no-ssr.js', '~/server/index.js'],
|
||||||
axios: {
|
axios: {
|
||||||
baseURL: process.env.BASE_URL + '/api',
|
baseURL: process.env.BASE_URL + '/api',
|
||||||
},
|
},
|
||||||
|
|
|
@ -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()
|
||||||
|
}
|
Reference in New Issue