diff --git a/api/ssrOG.ts b/api/ssrOG.ts index 2c29a96b..30aaae46 100644 --- a/api/ssrOG.ts +++ b/api/ssrOG.ts @@ -15,6 +15,7 @@ type VercelResponse = ServerResponse & { }; import { detectBot } from './botDetection/index.js'; +import { vercelLogger } from './utils/logger.js'; interface PageData { title: string; @@ -76,7 +77,10 @@ async function getPageData(pathname: string, fullUrl: string): Promise } } } 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 } } } 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 if (botDetection.isBot) { - console.log(`[SSR-OG] ✅ Bot detected: ${botDetection.platform || 'unknown'} | Confidence: ${botDetection.confidence} (${botDetection.score}%) | Method: ${botDetection.detectionMethod}`); - console.log(`[SSR-OG] Path: ${req.method} ${pathname}`); - console.log(`[SSR-OG] UA: ${userAgent}`); - if (botDetection.metadata.signals.length > 0) { - console.log(`[SSR-OG] Signals: ${botDetection.metadata.signals.slice(0, 5).join(', ')}${botDetection.metadata.signals.length > 5 ? '...' : ''}`); - } + vercelLogger.info('Bot detected', { + platform: botDetection.platform || 'unknown', + confidence: botDetection.confidence, + score: botDetection.score, + method: botDetection.detectionMethod, + path: `${req.method} ${pathname}`, + userAgent, + signals: botDetection.metadata.signals.slice(0, 5) + }); } else { // Log potential false negatives if (botDetection.score > 30) { - console.warn(`[SSR-OG] ⚠️ Low confidence bot (${botDetection.score}%) - not serving SSR | ${req.method} ${pathname}`); - console.warn(`[SSR-OG] UA: ${userAgent}`); - console.warn(`[SSR-OG] Signals: ${botDetection.metadata.signals.join(', ')}`); + vercelLogger.warn('Low confidence bot - not serving SSR', { + score: botDetection.score, + path: `${req.method} ${pathname}`, + userAgent, + signals: botDetection.metadata.signals + }); } 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) { // Fetch page-specific data 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 const ogTags = generateOGTags(pageData); @@ -246,7 +265,10 @@ export default async function handler(req: VercelRequest, res: VercelResponse): res.status(200).send(html); } 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 try { diff --git a/api/utils/logger.ts b/api/utils/logger.ts new file mode 100644 index 00000000..66d42142 --- /dev/null +++ b/api/utils/logger.ts @@ -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)); + } +}; diff --git a/public/robots.txt b/public/robots.txt index 6018e701..a84dd319 100644 --- a/public/robots.txt +++ b/public/robots.txt @@ -12,3 +12,5 @@ Allow: / User-agent: * Allow: / + +Sitemap: https://thrillwiki.com/sitemap.xml