mirror of
https://gitlab.com/PronounsPage/PronounsPage.git
synced 2025-09-22 12:03:25 -04:00
24 lines
1.0 KiB
TypeScript
24 lines
1.0 KiB
TypeScript
import type { Request, Response, NextFunction } from 'express';
|
|
|
|
import isHighLoadTime from './overload.js';
|
|
|
|
const USER_AGENT_BOTS = /bot|crawler|baiduspider|80legs|ia_archiver|voyager|curl|wget|yahoo! slurp|mediapartners-google|facebookexternalhit|twitterbot|whatsapp|php|python|mastodon/;
|
|
const USER_AGENT_BROWSERS = /mozilla|msie|gecko|firefox|edge|opera|safari|netscape|konqueror|android/;
|
|
|
|
const isBrowser = (userAgent: string | undefined): boolean => {
|
|
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: Request, res: Response, next: NextFunction): void {
|
|
if (process.env.NODE_ENV === 'production' && !req.url.startsWith('/card/@') && !req.url.startsWith('/blog.atom')) {
|
|
res.spa = isBrowser(req.headers['user-agent']) || isHighLoadTime(global.config.locale);
|
|
}
|
|
next();
|
|
}
|