Fix logging and robots.txt

This commit is contained in:
gpt-engineer-app[bot]
2025-11-04 22:01:27 +00:00
parent 40ebc3c11b
commit ffd71f51fb
3 changed files with 71 additions and 14 deletions

View File

@@ -15,6 +15,7 @@ type VercelResponse = ServerResponse & {
}; };
import { detectBot } from './botDetection/index.js'; import { detectBot } from './botDetection/index.js';
import { vercelLogger } from './utils/logger.js';
interface PageData { interface PageData {
title: string; title: string;
@@ -76,7 +77,10 @@ async function getPageData(pathname: string, fullUrl: string): Promise<PageData>
} }
} }
} catch (error) { } catch (error) {
console.error(`[SSR-OG] Error fetching park data: ${error}`); vercelLogger.error('Error fetching park data', {
error: error instanceof Error ? error.message : String(error),
slug
});
} }
} }
@@ -115,7 +119,10 @@ async function getPageData(pathname: string, fullUrl: string): Promise<PageData>
} }
} }
} catch (error) { } catch (error) {
console.error(`[SSR-OG] Error fetching ride data: ${error}`); vercelLogger.error('Error fetching ride data', {
error: error instanceof Error ? error.message : String(error),
slug: rideSlug
});
} }
} }
@@ -204,20 +211,29 @@ export default async function handler(req: VercelRequest, res: VercelResponse):
// Enhanced logging with detection details // Enhanced logging with detection details
if (botDetection.isBot) { if (botDetection.isBot) {
console.log(`[SSR-OG] ✅ Bot detected: ${botDetection.platform || 'unknown'} | Confidence: ${botDetection.confidence} (${botDetection.score}%) | Method: ${botDetection.detectionMethod}`); vercelLogger.info('Bot detected', {
console.log(`[SSR-OG] Path: ${req.method} ${pathname}`); platform: botDetection.platform || 'unknown',
console.log(`[SSR-OG] UA: ${userAgent}`); confidence: botDetection.confidence,
if (botDetection.metadata.signals.length > 0) { score: botDetection.score,
console.log(`[SSR-OG] Signals: ${botDetection.metadata.signals.slice(0, 5).join(', ')}${botDetection.metadata.signals.length > 5 ? '...' : ''}`); method: botDetection.detectionMethod,
} path: `${req.method} ${pathname}`,
userAgent,
signals: botDetection.metadata.signals.slice(0, 5)
});
} else { } else {
// Log potential false negatives // Log potential false negatives
if (botDetection.score > 30) { if (botDetection.score > 30) {
console.warn(`[SSR-OG] ⚠️ Low confidence bot (${botDetection.score}%) - not serving SSR | ${req.method} ${pathname}`); vercelLogger.warn('Low confidence bot - not serving SSR', {
console.warn(`[SSR-OG] UA: ${userAgent}`); score: botDetection.score,
console.warn(`[SSR-OG] Signals: ${botDetection.metadata.signals.join(', ')}`); path: `${req.method} ${pathname}`,
userAgent,
signals: botDetection.metadata.signals
});
} else { } else {
console.log(`[SSR-OG] Regular user (score: ${botDetection.score}%) | ${req.method} ${pathname}`); vercelLogger.info('Regular user request', {
score: botDetection.score,
path: `${req.method} ${pathname}`
});
} }
} }
@@ -228,7 +244,10 @@ export default async function handler(req: VercelRequest, res: VercelResponse):
if (botDetection.isBot) { if (botDetection.isBot) {
// 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}`); vercelLogger.info('Generated OG tags', {
title: pageData.title,
pathname
});
// Generate and inject OG tags // Generate and inject OG tags
const ogTags = generateOGTags(pageData); const ogTags = generateOGTags(pageData);
@@ -246,7 +265,10 @@ export default async function handler(req: VercelRequest, res: VercelResponse):
res.status(200).send(html); res.status(200).send(html);
} catch (error) { } catch (error) {
console.error('[SSR-OG] Error:', error); vercelLogger.error('SSR processing failed', {
error: error instanceof Error ? error.message : String(error),
pathname
});
// Fallback: serve original HTML // Fallback: serve original HTML
try { try {

33
api/utils/logger.ts Normal file
View File

@@ -0,0 +1,33 @@
/**
* Vercel Serverless Function Logger
* Provides structured JSON logging for Vercel API routes
* Matches the edge function logging pattern for consistency
*/
type LogLevel = 'info' | 'warn' | 'error';
interface LogContext {
[key: string]: unknown;
}
function formatLog(level: LogLevel, message: string, context?: LogContext): string {
return JSON.stringify({
timestamp: new Date().toISOString(),
level,
message,
service: 'vercel-ssrog',
...context
});
}
export const vercelLogger = {
info: (message: string, context?: LogContext) => {
console.info(formatLog('info', message, context));
},
warn: (message: string, context?: LogContext) => {
console.warn(formatLog('warn', message, context));
},
error: (message: string, context?: LogContext) => {
console.error(formatLog('error', message, context));
}
};

View File

@@ -12,3 +12,5 @@ Allow: /
User-agent: * User-agent: *
Allow: / Allow: /
Sitemap: https://thrillwiki.com/sitemap.xml