<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Iván Martínez Agüero</title>
    <description>The latest articles on DEV Community by Iván Martínez Agüero (@geekoteka).</description>
    <link>https://dev.to/geekoteka</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F104808%2Fa8c7f679-1a78-4a23-bf8f-05f199814967.jpeg</url>
      <title>DEV Community: Iván Martínez Agüero</title>
      <link>https://dev.to/geekoteka</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/geekoteka"/>
    <language>en</language>
    <item>
      <title>Integrating Lighthouse Reporting in Next.js with Puppeteer and Render</title>
      <dc:creator>Iván Martínez Agüero</dc:creator>
      <pubDate>Mon, 03 Jun 2024 11:28:06 +0000</pubDate>
      <link>https://dev.to/geekoteka/integrating-lighthouse-reporting-in-nextjs-with-puppeteer-and-render-46hj</link>
      <guid>https://dev.to/geekoteka/integrating-lighthouse-reporting-in-nextjs-with-puppeteer-and-render-46hj</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this article, I'll walk you through a project where I integrated Lighthouse reporting into a Next.js application using Puppeteer and deployed it on Render.com. The performance score might be lower when testing on the local server, but it improves in the production environment.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flabs.systec.dev%2Fblog%2Funderstanding-website-performance.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flabs.systec.dev%2Fblog%2Funderstanding-website-performance.png" alt="Understanding Website Performance" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;This project involves a Next.js component that makes a request to an API route. The API route connects to a service that generates the Lighthouse report. The implementation uses Puppeteer and Lighthouse running a Chrome instance on a remote server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flabs.systec.dev%2F_next%2Fimage%3Furl%3D%252Fblog%252Funderstanding-lighthouse-image1.png%26w%3D828%26q%3D75" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Flabs.systec.dev%2F_next%2Fimage%3Furl%3D%252Fblog%252Funderstanding-lighthouse-image1.png%26w%3D828%26q%3D75" alt="Lighthouse Reporting" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Project Setup
&lt;/h2&gt;

&lt;p&gt;Here is a brief overview of the setup and deployment process:&lt;/p&gt;

&lt;h3&gt;
  
  
  GitHub Repository
&lt;/h3&gt;

&lt;p&gt;You can find the source code for the project on GitHub: &lt;a href="https://github.com/alcance/lighthouse-reporter" rel="noopener noreferrer"&gt;GitHub - alcance/lighthouse-reporter&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  API Deployment
&lt;/h3&gt;

&lt;p&gt;I deployed the API on Render.com. Although it took some effort, I managed to get it running with Puppeteer and Lighthouse. You can explore the API on RapidAPI: &lt;a href="https://rapidapi.com/alcance-o0F_JAYX0/api/lighthouse-autoreporter" rel="noopener noreferrer"&gt;Lighthouse AutoReporter on RapidAPI&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Current Features
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;JSON Report:&lt;/strong&gt; Generates a Lighthouse report in JSON format.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PDF Report:&lt;/strong&gt; Generates and sends a Lighthouse report in PDF format via email.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Email Functionality:&lt;/strong&gt; The service can send the generated PDF report to the user's email.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Challenges and Solutions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Performance Issues
&lt;/h3&gt;

&lt;p&gt;Running the service on a machine with limited resources (0.5 CPUs and 512MB RAM) affects the performance score. For instance, a website like &lt;a href="https://labs.systec.dev" rel="noopener noreferrer"&gt;https://labs.systec.dev&lt;/a&gt; usually scores 80-90 on performance, but on the Render machine, it scored around 35. Upgrading to 2 CPUs and 2GB RAM can significantly improve the performance.&lt;/p&gt;

&lt;h3&gt;
  
  
  Handling CORS
&lt;/h3&gt;

&lt;p&gt;I encountered CORS issues, which I resolved by correctly setting the headers. Additionally, I opted for the Pro plan on Render.com ($85/month) for better resource allocation (4GB RAM, 2 CPUs).&lt;/p&gt;

&lt;h3&gt;
  
  
  Rate Limiting and Abuse Prevention
&lt;/h3&gt;

&lt;p&gt;To prevent abuse, I plan to integrate browser fingerprinting and set a limit on the number of requests per client. The &lt;a href="https://github.com/fingerprintjs/fingerprintjs" rel="noopener noreferrer"&gt;FingerprintJS&lt;/a&gt; library can generate a visitor ID to track and limit requests.&lt;/p&gt;

&lt;h3&gt;
  
  
  Caching
&lt;/h3&gt;

&lt;p&gt;Initially, I considered using Vercel functions for caching but decided against it due to complexity. Instead, a simple table-based cache was sufficient for my needs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Future Improvements
&lt;/h2&gt;

&lt;p&gt;Here are some ideas for further enhancing the service:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;User Management:&lt;/strong&gt; Implement a robust system for handling users and their requests.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Enhanced Reporting:&lt;/strong&gt; Add more detailed audit content to the PDF reports.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Integration with Gemini API:&lt;/strong&gt; Connect to the Gemini API to interpret report results and allow users to ask questions about their performance.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This project demonstrates how to integrate Lighthouse reporting into a Next.js application using Puppeteer and Render. The setup, although challenging at times, provides a valuable service for generating on-the-fly Lighthouse reports. Feel free to explore the &lt;a href="https://github.com/alcance/lighthouse-reporter" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt; and &lt;a href="https://rapidapi.com/alcance-o0F_JAYX0/api/lighthouse-autoreporter" rel="noopener noreferrer"&gt;RapidAPI&lt;/a&gt; to see the project in action.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>performance</category>
      <category>a11y</category>
      <category>seo</category>
    </item>
  </channel>
</rss>
