Files
explore/nextjs/utils/detectBotRequest.ts
T
tom goriunovandGitHub b6d5268188 Enhance OG tags with API data (#1806)
* simple fetch data for token instance page

* generate enhanced metadata

* group query params in page props

* make base gSSP function generic

* add metric for search engine bots

* measure api request duration

* get API data for token, address and dapp pages

* improve typings for apiData prop

* add ENV variable

* tweak logs for /media-type requests

* [skip ci] add prefix to default metrics
2024-04-16 10:27:47 +02:00

53 lines
1.4 KiB
TypeScript

import type { IncomingMessage } from 'http';
type SocialPreviewBot = 'twitter' | 'facebook' | 'telegram' | 'slack';
type SearchEngineBot = 'google' | 'bing' | 'yahoo' | 'duckduckgo';
type ReturnType = {
type: 'social_preview';
bot: SocialPreviewBot;
} | {
type: 'search_engine';
bot: SearchEngineBot;
} | undefined
export default function detectBotRequest(req: IncomingMessage): ReturnType {
const userAgent = req.headers['user-agent'];
if (!userAgent) {
return;
}
if (userAgent.toLowerCase().includes('twitter')) {
return { type: 'social_preview', bot: 'twitter' };
}
if (userAgent.toLowerCase().includes('facebook')) {
return { type: 'social_preview', bot: 'facebook' };
}
if (userAgent.toLowerCase().includes('telegram')) {
return { type: 'social_preview', bot: 'telegram' };
}
if (userAgent.toLowerCase().includes('slack')) {
return { type: 'social_preview', bot: 'slack' };
}
if (userAgent.toLowerCase().includes('googlebot')) {
return { type: 'search_engine', bot: 'google' };
}
if (userAgent.toLowerCase().includes('bingbot')) {
return { type: 'search_engine', bot: 'bing' };
}
if (userAgent.toLowerCase().includes('yahoo')) {
return { type: 'search_engine', bot: 'yahoo' };
}
if (userAgent.toLowerCase().includes('duckduck')) {
return { type: 'search_engine', bot: 'duckduckgo' };
}
}