Format policy: No PNG/JPG unless necessary. Force WebP/AVIF.
Sizing: srcset is mandatory.
Lazy loading: loading="lazy".
Fonts: FOIT/FOUT issues.
Limit count (System fonts are best).
Subsetting (only load used characters).
font-display: swap.
Alerts & Enforcement:
Lighthouse CI / Webpack Stats: Integrate into GitHub Actions/GitLab CI.
Tools:bundlesize, Lighthouse CI assertion.
RUM: Data from actual users (CrUX, Vercel Analytics, Sentry).
Alerting: Slack/PagerDuty integration for budget overruns.
4. Refining and Polishing:
Self-Correction during drafting: I initially thought about TTI (Time to Interactive), but INP is the current standard, so focus strictly on LCP/CLS/INP as requested.
Clarity: Use bolding for key terms. Use code blocks for config examples (JSON/YAML).
Actionability: Don't just say "optimize images," say "Enforce AVIF via CI checks."
5. Specific Content Generation (Mental Sandbox):
Config Example: I should provide a sample lighthouserc.json or similar to show how gates work programmatically.
Code Splitting: Mention React.lazy or import() generally.
Images: Mention fetchpriority.
6. Final Review against User Prompt:
LCP/CLS/INP? Yes.
Code splitting? Yes.
Font/Image policy? Yes.
Alerts? Yes.
This looks solid. I will now generate the response following this structure.
(Self-Correction on "Gates"): Make sure to emphasize that "Gates" implies a pass/fail mechanism in CI, not just looking at a dashboard.
(Self-Correction on INP): Since INP is new (replacing FID), I should emphasize it's about interaction latency over the lifetime of the page, not just the first input.
Let's write it.
This is a comprehensive guide to implementing Browser Performance Budgets. This structure moves from setting the standards (Metrics) to implementation techniques (Code/Assets) and finally to enforcement (Gates/Alerts).
1. The Gates: Core Web Vitals (CWV)
These are your user-centric quality gates. A performance budget usually sets the "Green" threshold as the passing grade.
Metric
What it Measures
Budget Threshold (Gate)
Failure Pattern
LCP (Largest Contentful Paint)
Loading speed of the main content.
≤ 2.5 seconds
Slow server response (TTFB), unoptimized images, render-blocking JS/CSS.
INP (Interaction to Next Paint)
Responsiveness to user clicks/inputs.
≤ 200 milliseconds
Heavy JavaScript execution blocking the main thread (e.g., React re-renders).
CLS (Cumulative Layout Shift)
Visual stability (layout jumps).
≤ 0.1
Images without dimensions, dynamic content injection, FOUC (Flash of Unstyled Content).
Implementation: Configure your CI/CD pipeline to fail the build if the median value of these metrics exceeds the threshold in a controlled test environment (e.g., Lighthouse CI).
2. Code Splitting: The Volume Knob
Modern SPAs (Single Page Apps) often ship too much JavaScript. Code splitting ensures you only load the code the user needs right now.
Route-Based Splitting: Lazy load components only when a user visits that specific route.
Component-Based Splitting: Load heavy dependencies conditionally (e.g., a complex chart library or a WYSIWYG editor) only when the user opens that modal or tab.
Third-Party Hygiene:
Use "Tree Shaking" to strip unused exports.
Replace heavy libraries with lighter alternatives (e.g., replace Moment.js with Day.js or date-fns).
Budget Gate: Set a hard limit on Total JS Bundle Size per route (e.g., Main bundle < 150KB gzipped).
3. Resource Policies: Fonts & Images
Assets are often the heaviest part of the payload.
A. Image Policy
Format Restriction: Enforce Next-Gen formats.
Rule: Serve WebP or AVIF. Fallback to JPEG/PNG only for legacy browsers.
Sizing: Prevent layout shifts and bandwidth waste.
Rule: All <img> tags must have explicit width and height attributes (for CLS) or use aspect-ratio in CSS.
Rule: Use srcset to serve 2x/3x images only to high-DPI screens.
Lazy Loading: Defer off-screen images.
HTML:<img loading="lazy" ... />
B. Font Policy
Subset fonts: Do not load the entire font library (e.g., all weights 100-900). Load only Regular (400) and Bold (700).
font-display: swap: Ensures text is visible immediately using a system font, swapping to the custom font once loaded. This prevents invisible text (FOIT) and improves LCP.
CSS:@font-face { font-display: swap; ... }
Preloading: Preload critical fonts required for the Above-the-Fold content to speed up LCP.
4. Enforcement & Alerts
A budget is useless if it isn't enforced.
A. CI/CD "Break the Build" (Hard Gates)
Integrate performance testing into your Pull Request workflow.