What is @vercel/speed-insights?
@vercel/speed-insights is an npm package designed to provide insights into the performance of web applications. It leverages Google's Lighthouse to analyze and generate reports on various performance metrics, helping developers optimize their applications for better speed and user experience.
What are @vercel/speed-insights's main functionalities?
Generate Performance Report
This feature allows you to generate a performance report for a given URL. The `getSpeedInsights` function fetches the performance metrics and returns a detailed report.
const { getSpeedInsights } = require('@vercel/speed-insights');
async function generateReport(url) {
const report = await getSpeedInsights({ url });
console.log(report);
}
generateReport('https://example.com');
Custom Configuration
This feature allows you to customize the performance report by specifying options such as the strategy (mobile or desktop) and the categories of metrics to include in the report.
const { getSpeedInsights } = require('@vercel/speed-insights');
async function generateCustomReport(url) {
const options = {
strategy: 'mobile',
categories: ['performance', 'accessibility']
};
const report = await getSpeedInsights({ url, options });
console.log(report);
}
generateCustomReport('https://example.com');
Save Report to File
This feature allows you to save the generated performance report to a file. The `fs` module is used to write the report data to a specified file path.
const { getSpeedInsights } = require('@vercel/speed-insights');
const fs = require('fs');
async function saveReportToFile(url, filePath) {
const report = await getSpeedInsights({ url });
fs.writeFileSync(filePath, JSON.stringify(report, null, 2));
console.log(`Report saved to ${filePath}`);
}
saveReportToFile('https://example.com', './report.json');
Other packages similar to @vercel/speed-insights
lighthouse
Lighthouse is an open-source, automated tool for improving the quality of web pages. It can be run as a Chrome Extension or from the command line. It audits performance, accessibility, progressive web apps, SEO, and more. Compared to @vercel/speed-insights, Lighthouse offers a broader range of audits and can be integrated into various CI/CD pipelines.
psi
psi (PageSpeed Insights) is a Node.js module for Google's PageSpeed Insights API. It provides performance reports and suggestions for improving the speed of web pages. While @vercel/speed-insights focuses on Lighthouse metrics, psi directly interfaces with the PageSpeed Insights API, offering a different set of performance insights.
web-vitals
web-vitals is a small, modular library for measuring all the Web Vitals metrics on real users. It is designed to be used in production environments to capture real user performance data. Unlike @vercel/speed-insights, which generates synthetic performance reports, web-vitals focuses on real user monitoring (RUM) to provide insights based on actual user interactions.
Vercel Speed Insights
Performance insights for your website
Overview
@vercel/speed-insights
automatically tracks web vitals and other performance metrics for your website.
This package does not track data in development mode.
It has 1st class integration with:
Framework | Package |
---|
Next.js | @vercel/speed-insights/next |
Nuxt | @vercel/speed-insights/nuxt |
Sveltekit | @vercel/speed-insights/sveltekit |
React | @vercel/speed-insights/react |
Astro | @vercel/speed-insights/astro |
Vue | @vercel/speed-insights/vue |
It also supports other frameworks, vanilla JS and static websites.
Quickstart
-
Enable Vercel Speed Insights for a project in the Vercel Dashboard.
-
Add the @vercel/speed-insights
package to your project
-
Inject Speed Insights to your app
- If you are using Next.js, React, Nuxt or Vue you can use the framework-specific
<SpeedInsights />
component to inject the script into your app. - If you are using Sveltekit, you can use the
injectSpeedInsights()
function @vercel/speed-insights/sveltekit
in your top-level +layout.js/ts
file. - For other frameworks, you can use the
inject
function add the tracking script to your app. - If you want to use Vercel Speed Insights on a static site without npm, follow the instructions in the documentation.
-
Deploy your app to Vercel and see data flowing in.
Documentation
Find more details about this package in our documentation.