UNPKG

nextjs-perfkit

Version:

Frontend performance analyzer for Next.js apps โ€“ render time tracking, memory logging, network analysis, DevTools UI.

234 lines (158 loc) โ€ข 5.32 kB
# ๐Ÿš€ nextjs-perfkit **Frontend performance analytics and DevTools UI for Next.js + React apps.** Track render times, memory usage, network performance, prop changes, and export logs for team insights โ€” all from a floating overlay. --- ![npm](https://img.shields.io/npm/v/nextjs-perfkit) ![downloads](https://img.shields.io/npm/dm/nextjs-perfkit) ![license](https://img.shields.io/npm/l/nextjs-perfkit) --- ## ๐Ÿ“ฆ Package Exports - CommonJS: `dist/cjs/index.js` (for Node.js, older bundlers) - ESModule: `dist/esm/index.js` (for modern bundlers like Webpack, Vite, Next.js) These are automatically selected based on your environment. You can also import directly if needed: ```js // ESM import { initPerfKit } from "nextjs-perfkit/dist/esm/index.js"; // CommonJS const { initPerfKit } = require("nextjs-perfkit/dist/cjs/index.js"); ``` --- ## ๐Ÿ“ฆ Installation ```bash npm install nextjs-perfkit ``` --- ## ๐ŸŽจ CSS Styling You must import the overlay CSS manually in your Next.js `_app.tsx` file: ```ts import "nextjs-perfkit/styles/overlay.css"; export default function MyApp({ Component, pageProps }) { return <Component {...pageProps} />; } ``` > โš ๏ธ If you forget to include this, the overlay will inject fallback inline styles with a warning. --- ## ๐Ÿš€ Quick Start ```ts import { initPerfKit } from "nextjs-perfkit"; if (process.env.NODE_ENV === "development") { initPerfKit(); // initializes DevTools floating panel } ``` --- ## ๐Ÿงช Example Usage ```tsx "use client"; import { useEffect } from "react"; import { useRenderHeatmap, useMemoryTracker, usePropDebugger, registerAlertHandler, checkMemoryUsage, trackNetworkRequest, downloadLogs, } from "nextjs-perfkit"; export default function Home() { useRenderHeatmap("HomePage"); useMemoryTracker(3000); usePropDebugger({ propA: "value" }); useEffect(() => { registerAlertHandler((msg, type, meta) => { console.warn(`[ALERT - ${type}]`, msg, meta); }); setInterval(() => { checkMemoryUsage(); }, 5000); trackNetworkRequest("/api/example"); }, []); return ( <main data-perf-label="HomePage"> <h1>๐Ÿš€ Hello PerfKit</h1> <button onClick={downloadLogs}>๐Ÿ“ฅ Download Logs</button> </main> ); } ``` --- ## ๐Ÿ“š Utility Highlights ### `useMemoryTracker(intervalMs?: number)` Tracks memory usage via `performance.memory` and logs it regularly. ๐Ÿšจ Triggers an alert if heap usage exceeds a defined threshold (default: 100MB). ### `useRenderHeatmap(label: string)` Measures render duration of a component and outlines it with a visual border if slow. ๐Ÿ“ Useful for identifying visual bottlenecks. ### `usePropDebugger(props)` Logs changed props that cause a re-render. ๐Ÿ” Helps debug unnecessary re-renders in components. ### `registerAlertHandler(callback)` Registers a custom global alert handler. ๐Ÿ’ก Use this to display toast messages or logs when memory/network thresholds are crossed. ### `trackNetworkRequest(url: string)` Monitors fetch calls and tracks repeated calls to the same endpoint within a short window (10s by default). ๐Ÿ“ก Prevents performance issues caused by over-fetching. ### `downloadLogs()` Exports all collected logs (render, memory, network, etc.) as a downloadable `.json` file. ๐Ÿ“ Perfect for team debugging or offline analysis. --- ## ๐Ÿ•ต๏ธโ€โ™‚๏ธ How to use `usePropDebugger` The `usePropDebugger` hook helps you track which props are changing and causing your component to re-render. This is useful for debugging unnecessary renders and optimizing your React components. ### Example ```tsx "use client"; import { usePropDebugger } from "nextjs-perfkit"; export default function MyComponent(props) { usePropDebugger(props); return ( <div> <h2>Check the console for prop changes!</h2> </div> ); } ``` - Just call `usePropDebugger(props)` at the top of your component. - On every render, it will log which props have changed since the last render. --- ## ๐Ÿ“‘ Types You can import type-safe log definitions like so: ```ts import type { PerfLogEntry } from "nextjs-perfkit/types"; ``` --- ## ๐Ÿ“ค Exportable Logs PerfKit collects structured logs from memory, network, and render events. You can export them using: ```ts import { downloadLogs } from "nextjs-perfkit"; downloadLogs(); // Triggers a JSON download ``` --- ## ๐Ÿ›  Project Structure ``` src/ โ”œโ”€โ”€ hooks/ # useRenderHeatmap, useMemoryTracker, usePropDebugger โ”œโ”€โ”€ utils/ # trackMemory, network, alerts, logging โ”œโ”€โ”€ types.ts # exported types โ”œโ”€โ”€ DevToolsOverlay.tsx # floating overlay component โ”œโ”€โ”€ index.ts # main entry exports styles/ โ”œโ”€โ”€ overlay.css # external UI styles ``` --- ## ๐Ÿ›  Future Roadmap ## ๐Ÿ“ข Alerts & Thresholds - Memory usage > 100MB โ†’ โš ๏ธ alert in UI - Fetch calls โ†’ โฑ๏ธ duration log in console - Render time > 16ms โ†’ ๐Ÿ”ด red outline --- ## ๐Ÿ‘ฅ Contributing 1. Fork the repo 2. Clone locally: `git clone https://github.com/deepbratt/nextjs-perfkit.git` 3. Install deps: `npm install` 4. Test with a Next.js sample project --- ## ๐Ÿ“ License MIT ยฉ 2025 [Deep Bratt](https://github.com/deepbratt) --- Made with โค๏ธ for frontend performance engineering.