PronounsPage/server/no-ssr.ts
2024-06-18 16:22:05 +00:00

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();
}