Blazity
Performance
Effective and accurate measurement is essential to ensure smooth, user-friendly web experiences. In this section, we will discuss how developers can use various tools and metrics to assess and enhance performance in Next.js apps.
This article is part of The Expert Guide to Next.js Performance Optimization, a comprehensive Ebook you can download for free here.
The guide covers every aspect of building fast, scalable web apps with Next.js, including:
- Streaming, Suspense & Hydration
- Infrastructure
- Real-life Examples
- How to Measure Web Performance
- Bundle Analysis.
Lighthouse is a widely used tool developed by Google to analyze the quality of web pages. It audits various aspects, including performance, accessibility, and search engine optimization (SEO).
Benefits:
Integration with Next.js:
Limitations:
Note on In-Browser Testing:
In-browser testing with Lighthouse may not accurately mirror the broader user experience due to various environmental and technical variables.
WebPageTest complements Lighthouse by offering different insights and enhanced consistency across testing environments.
Advantages over Lighthouse:
When Lighthouse might be a better fit:
PageSpeed Insights combines both lab and field data to provide a comprehensive view of website performance. It integrates Lighthouse's synthetic testing capabilities with real-world performance data from the Chrome UX Report.
vs. Lighthouse:
vs. WebPageTest:
PageSpeed Insights uses the Chrome UX Report (CrUX) to provide real-world performance data. This offers several benefits:
Limitations of Field Data
When to Use PageSpeed Insights:
For Next.js applications deployed on Vercel, Speed Insights provides built-in real-user monitoring capabilities that offer immediate visibility into real-world application performance.
Detailed Performance Metrics:
Speed Insights collects various performance data points crucial for understanding how a website performs under real-user conditions. Metrics such as Real Experience Score (RES), First Contentful Paint (FCP), and Largest Contentful Paint (LCP) provide a detailed look at the site's loading performance and visual stability.
Dashboard Integration:
Once enabled, Speed Insights tracks data across all deployed environments, including preview and production. The data is accessible through a user-friendly dashboard within the Vercel project view, eliminating the need for additional scripts or code modifications.
Granular Data Views:
Visual Representations:
Integrating RUM through Vercel’s Speed Insights allows developers to make data-driven decisions based on how real users interact with their applications. This direct feedback loop enables the following:
While Vercel Speed Insights is deeply integrated with Next.js deployments on Vercel, there are other RUM solutions available for different hosting environments or specific monitoring needs:
Commercial Services:
When to Consider Other RUM Solutions:
Collecting your real-world metrics
You can use the useReportWebVitals
hook from next/web-vitals
to collect real-world performance metrics and send them to any service. Because it is deeply integrated with Next, this might prove more accurate than generic web vitals collection built into your analytics or data platform SDK.
Example: Sending Web Vitals to Google Analytics 4
Code
1import { useReportWebVitals } from 'next/web-vitals'
2/ [...]
3useReportWebVitals(metric => {
4 window.gtag('event', metric.name, {
5 value: Math.round(metric.name === 'CLS' ? metric.value * 1000 :
6metric.value), / values must be integers
7 event_label: metric.id, / id unique to current page load
8 non_interaction: true, / avoids affecting bounce rate.
9 });
10}
Get a quote
“We are very happy with the outcomes and look forward to continuing to work with them on larger initiatives.”
Brian Grafola
CTO at Vibes