Performance Budget Guard
Monitor bundle size and Core Web Vitals metrics during development, blocking deployments that exceed performance budgets with detailed reports. Automatically analyzes Next.js build output, checks bundle sizes against predefined budgets, and provides optimization recommendations. Hook triggers on PostToolUse for build-related operations and file changes that could affect performance.
$ npx claude-code-templates@latest --hook="performance/performance-budget-guard" --yesRequires Claude Code. The command adds this hook to your project's .claudedirectory â nothing runs on ToolZip's servers.
What's inside this hook
Component source
{
"description": "Monitor bundle size and Core Web Vitals metrics during development, blocking deployments that exceed performance budgets with detailed reports. Automatically analyzes Next.js build output, checks bundle sizes against predefined budgets, and provides optimization recommendations. Hook triggers on PostToolUse for build-related operations and file changes that could affect performance.",
"hooks": {
"PostToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "bash -c 'input=$(cat); COMMAND=$(echo \"$input\" | jq -r \".tool_input.command // empty\"); SUCCESS=$(echo \"$input\" | jq -r \".tool_response.success // false\"); if [ \"$SUCCESS\" = \"true\" ] && [[ \"$COMMAND\" =~ (npm\\ run\\ build|next\\ build|vercel\\ build|yarn\\ build) ]]; then echo \"đ Performance Budget Guard: Analyzing build output...\"; if [ -d \".next\" ]; then BUNDLE_SIZE=$(find .next/static/chunks -name \".js\" -exec stat -f%z {} + 2>/dev/null | awk \"{total += \\$1} END {print total}\"); BUNDLE_SIZE_KB=$((BUNDLE_SIZE / 1024)); BUDGET_LIMIT=350; echo \"đĻ Total bundle size: ${BUNDLE_SIZE_KB}KB\"; if [ $BUNDLE_SIZE_KB -gt $BUDGET_LIMIT ]; then echo \"đ¨ PERFORMANCE BUDGET EXCEEDED!\"; echo \"Current bundle size: ${BUNDLE_SIZE_KB}KB\"; echo \"Budget limit: ${BUDGET_LIMIT}KB\"; echo \"Overage: $((BUNDLE_SIZE_KB - BUDGET_LIMIT))KB\"; echo \"\" >&2; echo \"â ī¸ Performance budget exceeded by $((BUNDLE_SIZE_KB - BUDGET_LIMIT))KB!\" >&2; echo \"\" >&2; echo \"đ Bundle Analysis:\" >&2; find .next/static/chunks -name \".js\" -exec ls -lah {} + 2>/dev/null | sort -k5 -hr | head -5 >&2; echo \"\" >&2; echo \"đĄ Optimization recommendations:\" >&2; echo \"âĸ Use dynamic imports for large components\" >&2; echo \"âĸ Implement code splitting with next/dynamic\" >&2; echo \"âĸ Check for duplicate dependencies\" >&2; echo \"âĸ Optimize third-party libraries\" >&2; echo \"âĸ Run: npm run analyze for detailed bundle analysis\" >&2; exit 2; else echo \"â Bundle size within budget: ${BUNDLE_SIZE_KB}KB / ${BUDGET_LIMIT}KB\"; REMAINING=$((BUDGET_LIMIT - BUNDLE_SIZE_KB)); echo \"đ¯ Remaining budget: ${REMAINING}KB\"; if [ $REMAINING -lt 50 ]; then echo \"â ī¸ Warning: Less than 50KB remaining in performance budget\"; fi; fi; else echo \"â No .next build directory found. Run build first.\"; fi; else echo \"âšī¸ Performance check skipped (not a build command or failed build)\"; fi'",
"timeout": 30
}
]
},
{
"matcher": "Write|Edit|MultiEdit",
"hooks": [
{
"type": "command",
"command": "bash -c 'input=$(cat); FILE_PATH=$(echo \"$input\" | jq -r \".tool_input.file_path // empty\"); SUCCESS=$(echo \"$input\" | jq -r \".tool_response.success // false\"); if [ \"$SUCCESS\" = \"true\" ] && [[ \"$FILE_PATH\" =~ \\.(js|jsx|ts|tsx)$ ]] && [[ ! \"$FILE_PATH\" =~ node_modules ]]; then echo \"đ Performance Guard: Checking code changes in $FILE_PATH...\"; if [ -f \"$FILE_PATH\" ]; then FILE_SIZE=$(stat -f%z \"$FILE_PATH\" 2>/dev/null || stat -c%s \"$FILE_PATH\" 2>/dev/null); FILE_SIZE_KB=$((FILE_SIZE / 1024)); if [ $FILE_SIZE_KB -gt 100 ]; then echo \"â ī¸ Large file detected: ${FILE_SIZE_KB}KB\"; echo \"đĄ Consider splitting large components or lazy loading\"; fi; IMPORTS_COUNT=$(grep -c \"^import\" \"$FILE_PATH\" 2>/dev/null || echo \"0\"); if [ $IMPORTS_COUNT -gt 15 ]; then echo \"đĻ Many imports detected: $IMPORTS_COUNT imports\"; echo \"đĄ Consider consolidating imports or tree-shaking unused code\"; fi; if grep -q \"import.\\.*from\" \"$FILE_PATH\" 2>/dev/null; then echo \"đ¨ Wildcard import detected in $FILE_PATH\"; echo \"đĄ Use specific imports instead of wildcard imports for better tree-shaking\"; fi; if grep -q \"moment\" \"$FILE_PATH\" 2>/dev/null; then echo \"đ Moment.js usage detected\"; echo \"đĄ Consider using date-fns or native Date for smaller bundle size\"; fi; if grep -q \"lodash\" \"$FILE_PATH\" 2>/dev/null && ! grep -q \"lodash/\" \"$FILE_PATH\" 2>/dev/null; then echo \"đ§ Full Lodash import detected\"; echo \"đĄ Use specific Lodash functions: import debounce from \\\"lodash/debounce\\\"\"; fi; echo \"â Performance check completed for $FILE_PATH\"; else echo \"â File $FILE_PATH not found\"; fi; else echo \"âšī¸ Performance check skipped (not a JavaScript/TypeScript file or failed operation)\"; fi'",
"timeout": 15
}
]
}
]
}
}
Related Claude Code Hooks
Performance Monitor
Monitor system performance during Claude Code operations. Tracks CPU, memory usage, and execution time for performance optimization.
Simple Notifications
Send desktop notifications when Claude Code finishes working or needs user input. Works on macOS and Linux systems.
Smart Commit
Intelligent git commit creation with automatic message generation and validation. Creates meaningful commits based on file changes.
Update Search Year
Automatically adds current year to WebSearch queries when no year is specified. This hook intercepts WebSearch tool usage and appends the current year to queries that don't already contain a year, ensuring search results are current and relevant.
Smart Formatting
Smart code formatting based on file type. Automatically formats code using Prettier, Black, gofmt, rustfmt, and other language-specific formatters.
Lint On Save
Automatically run linting tools after file modifications. Supports ESLint for JavaScript/TypeScript, Pylint for Python, and RuboCop for Ruby.
Catalog data and component content are sourced from the open-source davila7/claude-code-templates project (MIT license). ToolZip curates the listing and writes original descriptions; every component links back to its original source. Claude Code is a product of Anthropic. ToolZip is an independent catalog and is not affiliated with or endorsed by Anthropic.