mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 09:31:13 -05:00
feat: Improve bot detection for Vercel OG preview
This commit is contained in:
43
api/ssrOG.ts
43
api/ssrOG.ts
@@ -37,7 +37,23 @@ const SOCIAL_BOTS = {
|
|||||||
'slurp': 'yahoo',
|
'slurp': 'yahoo',
|
||||||
'duckduckbot': 'duckduckgo',
|
'duckduckbot': 'duckduckgo',
|
||||||
'baiduspider': 'baidu',
|
'baiduspider': 'baidu',
|
||||||
'yandexbot': 'yandex'
|
'yandexbot': 'yandex',
|
||||||
|
// Headless browsers & crawlers
|
||||||
|
'headless': 'headless-browser',
|
||||||
|
'chrome-lighthouse': 'lighthouse',
|
||||||
|
'puppeteer': 'puppeteer',
|
||||||
|
'playwright': 'playwright',
|
||||||
|
'selenium': 'selenium',
|
||||||
|
'phantomjs': 'phantomjs',
|
||||||
|
// Vercel & deployment platforms
|
||||||
|
'vercel': 'vercel',
|
||||||
|
'vercel-screenshot': 'vercel',
|
||||||
|
'prerender': 'prerender',
|
||||||
|
// Generic crawler patterns
|
||||||
|
'bot': 'generic-bot',
|
||||||
|
'crawler': 'generic-crawler',
|
||||||
|
'spider': 'generic-spider',
|
||||||
|
'scraper': 'generic-scraper'
|
||||||
};
|
};
|
||||||
|
|
||||||
interface BotDetection {
|
interface BotDetection {
|
||||||
@@ -229,20 +245,35 @@ export default async function handler(req: VercelRequest, res: VercelResponse) {
|
|||||||
const fullUrl = `https://${req.headers.host}${req.url}`;
|
const fullUrl = `https://${req.headers.host}${req.url}`;
|
||||||
const pathname = new URL(fullUrl).pathname;
|
const pathname = new URL(fullUrl).pathname;
|
||||||
|
|
||||||
console.log(`[SSR-OG] ${req.method} ${pathname} | UA: ${userAgent.substring(0, 60)}`);
|
|
||||||
|
|
||||||
// Bot detection
|
// Bot detection
|
||||||
const botDetection = detectBot(userAgent);
|
const botDetection = detectBot(userAgent);
|
||||||
|
|
||||||
|
// Enhanced logging
|
||||||
|
if (botDetection.isBot) {
|
||||||
|
console.log(`[SSR-OG] ✅ Bot detected: ${botDetection.platform} | ${req.method} ${pathname}`);
|
||||||
|
console.log(`[SSR-OG] Full UA: ${userAgent}`);
|
||||||
|
} else {
|
||||||
|
// Log undetected potential bots for debugging
|
||||||
|
const looksLikeBot = !userAgent.includes('Mozilla') ||
|
||||||
|
userAgent.includes('http') ||
|
||||||
|
userAgent.length < 50;
|
||||||
|
|
||||||
|
if (looksLikeBot) {
|
||||||
|
console.warn(`[SSR-OG] ⚠️ Possible undetected bot | ${req.method} ${pathname}`);
|
||||||
|
console.warn(`[SSR-OG] Full UA: ${userAgent}`);
|
||||||
|
} else {
|
||||||
|
console.log(`[SSR-OG] Regular user | ${req.method} ${pathname} | UA: ${userAgent.substring(0, 60)}...`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Read the built index.html
|
// Read the built index.html
|
||||||
const htmlPath = join(process.cwd(), 'dist', 'index.html');
|
const htmlPath = join(process.cwd(), 'dist', 'index.html');
|
||||||
let html = readFileSync(htmlPath, 'utf-8');
|
let html = readFileSync(htmlPath, 'utf-8');
|
||||||
|
|
||||||
if (botDetection.isBot) {
|
if (botDetection.isBot) {
|
||||||
console.log(`[SSR-OG] Bot detected: ${botDetection.platform}`);
|
|
||||||
|
|
||||||
// Fetch page-specific data
|
// Fetch page-specific data
|
||||||
const pageData = await getPageData(pathname, fullUrl);
|
const pageData = await getPageData(pathname, fullUrl);
|
||||||
|
console.log(`[SSR-OG] Generated OG tags: ${pageData.title}`);
|
||||||
|
|
||||||
// Generate and inject OG tags
|
// Generate and inject OG tags
|
||||||
const ogTags = generateOGTags(pageData);
|
const ogTags = generateOGTags(pageData);
|
||||||
@@ -250,8 +281,6 @@ export default async function handler(req: VercelRequest, res: VercelResponse) {
|
|||||||
|
|
||||||
res.setHeader('X-Bot-Platform', botDetection.platform || 'unknown');
|
res.setHeader('X-Bot-Platform', botDetection.platform || 'unknown');
|
||||||
res.setHeader('X-SSR-Modified', 'true');
|
res.setHeader('X-SSR-Modified', 'true');
|
||||||
} else {
|
|
||||||
console.log('[SSR-OG] Regular user - serving original HTML');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res.setHeader('Content-Type', 'text/html; charset=utf-8');
|
res.setHeader('Content-Type', 'text/html; charset=utf-8');
|
||||||
|
|||||||
Reference in New Issue
Block a user