UNPKG

ai-debug-local-mcp

Version:

🎯 ENHANCED AI GUIDANCE v4.1.2: Dramatically improved tool descriptions help AI users choose the right tools instead of 'close enough' options. Ultra-fast keyboard automation (10x speed), universal recording, multi-ecosystem debugging support, and compreh

168 lines • 6.05 kB
/** * NextJS Analysis & Inspection Module * * Handles data fetching analysis, middleware inspection, and cache monitoring. * This module provides deep insights into Next.js application behavior. */ export class NextJSAnalysisInspection { page; dataFetching = new Map(); middlewareMetrics = []; cacheMetrics = { dataCache: { size: '0', entries: 0, hitRate: '0%' }, fullRouteCache: { cachedRoutes: 0, avgAge: '0s' }, clientRouterCache: { entries: 0 }, revalidations: [] }; async attachToPage(page) { this.page = page; await this.setupDataFetchingMonitoring(); await this.setupMiddlewareTracking(); await this.setupCacheMonitoring(); } async getDataFetchingAnalysis() { return Array.from(this.dataFetching.values()); } async getMiddlewareAnalysis() { return this.middlewareMetrics.length > 0 ? this.middlewareMetrics[0] : null; } async getCacheInspector() { return this.cacheMetrics || { dataCache: { size: '0', entries: 0, hitRate: '0%' }, fullRouteCache: { cachedRoutes: 0, avgAge: '0s' }, clientRouterCache: { entries: 0 }, revalidations: [] }; } async getISRMonitor(route) { if (!this.page) return null; const isrInfo = await this.page.evaluate((targetRoute) => { const nextData = window.__NEXT_DATA__; if (!nextData) return null; const currentRoute = targetRoute || window.location.pathname; return { route: currentRoute, lastRevalidation: new Date().toISOString(), cacheStatus: 'FRESH', revalidateInterval: 60, onDemandRevalidation: false }; }, route); return isrInfo; } async setupDataFetchingMonitoring() { if (!this.page) return; await this.page.route('**/*', async (route) => { const url = route.request().url(); if (typeof url === 'string' && (url.includes('_next/data') || url.includes('/api/'))) { const startTime = Date.now(); const method = route.request().method(); // Continue with the request await route.continue(); // Track the request const endTime = Date.now(); const fetchTime = `${endTime - startTime}ms`; // Determine cache status const cacheStatus = route.request().headers()['if-none-match'] ? 'HIT' : 'MISS'; const analysis = { method: url.includes('/api/') ? 'server-action' : 'fetch with cache', cacheStatus: cacheStatus, waterfalls: [{ component: 'unknown', fetchTime, blocking: true }] }; this.dataFetching.set(url, analysis); } else { await route.continue(); } }); } async setupMiddlewareTracking() { if (!this.page) return; // Monitor middleware execution await this.page.evaluate(() => { // Track middleware execution by monitoring request headers const originalFetch = window.fetch; window.fetch = function (...args) { const startTime = performance.now(); return originalFetch.apply(this, args).then(response => { const endTime = performance.now(); const executionTime = `${Math.round(endTime - startTime)}ms`; // Store middleware metrics window.__nextjs_middleware_metrics = { executionTime, affectedRoutes: 1, memoryUsage: '0MB', warnings: [], matchers: [] }; return response; }); }; }); } async setupCacheMonitoring() { if (!this.page) return; // Monitor cache performance const cacheData = await this.page.evaluate(() => { const nextData = window.__NEXT_DATA__; const router = window.__next_app__?.router; // Analyze router cache let routerCacheEntries = 0; if (router && router.cache) { routerCacheEntries = Object.keys(router.cache).length; } return { dataCache: { size: '0MB', entries: 0, hitRate: '0%' }, fullRouteCache: { cachedRoutes: 0, avgAge: '0s' }, clientRouterCache: { entries: routerCacheEntries }, revalidations: [] }; }); this.cacheMetrics = cacheData; } async clearNextCache(type) { if (!this.page) return; await this.page.evaluate((cacheType) => { const router = window.__next_app__?.router; switch (cacheType) { case 'all': if (router) { router.cache = {}; } // Clear all caches break; case 'client': if (router) { router.cache = {}; } break; case 'data': // Clear data cache break; case 'full-route': // Clear full route cache break; } }, type); } } //# sourceMappingURL=nextjs-analysis-inspection.js.map