<?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: Joeri Smits</title>
    <description>The latest articles on DEV Community by Joeri Smits (@joerismits).</description>
    <link>https://dev.to/joerismits</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%2F1000236%2Fb8ab00fb-b1e7-45f6-b0db-31655498f11f.jpeg</url>
      <title>DEV Community: Joeri Smits</title>
      <link>https://dev.to/joerismits</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/joerismits"/>
    <language>en</language>
    <item>
      <title>Ensure your Next.js app's performance is top-notch with Lighthouse CI and GitHub Actions</title>
      <dc:creator>Joeri Smits</dc:creator>
      <pubDate>Tue, 03 Jan 2023 18:18:04 +0000</pubDate>
      <link>https://dev.to/joerismits/ensure-your-nextjs-apps-performance-is-top-notch-with-lighthouse-ci-and-github-actions-4ne8</link>
      <guid>https://dev.to/joerismits/ensure-your-nextjs-apps-performance-is-top-notch-with-lighthouse-ci-and-github-actions-4ne8</guid>
      <description>&lt;p&gt;Web performance is a crucial aspect of modern web development, yet I find that many developers do not prioritize it during development. This often leads to a backlog of performance optimizations that must be addressed just before a project is launched. In this article, I will demonstrate how to use Google Lighthouse to ensure performance in a GitHub CI workflow for a Next.js project.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;TLDR;&lt;/code&gt;&lt;/strong&gt; I use &lt;a href="https://github.com/GoogleChrome/lighthouse-ci" rel="noopener noreferrer"&gt;the Google Chrome Lighthouse CI&lt;/a&gt; with a &lt;code&gt;.lighthouserc&lt;/code&gt; json configuration to test &lt;code&gt;next start&lt;/code&gt;. The Lighthouse CI GitHub app is used to return a pass or fail status check in a PR.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lighthouse CI configuration
&lt;/h2&gt;

&lt;p&gt;Google has already created a great tool for using Lighthouse during CI called &lt;a href="https://github.com/GoogleChrome/lighthouse-ci" rel="noopener noreferrer"&gt;the Google Chrome Lighthouse CI&lt;/a&gt;. It offers great support for various applications. &lt;/p&gt;

&lt;p&gt;First of all we install the &lt;code&gt;lhci&lt;/code&gt; package with &lt;code&gt;npm i --save-dev @lhci/cli&lt;/code&gt;. This will be of great use when testing our Lighthouse configuration.&lt;br&gt;
Since we need the Next.js server to run our application we cannot use the default Lighthouse configuration, which runs with &lt;code&gt;lhci autorun&lt;/code&gt;, and will look for &lt;code&gt;.html&lt;/code&gt; files in common static build folders.&lt;br&gt;
Instead we use the following Lighthouse configuration which needs to be stored as &lt;code&gt;.lighthouserc&lt;/code&gt; under the root of your project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"ci"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"collect"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"startServerCommand"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"npm run start"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"startServerReadyPattern"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ready on"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="s2"&gt;"http://localhost:3000"&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"numberOfRuns"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"settings"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
                &lt;/span&gt;&lt;span class="nl"&gt;"preset"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"desktop"&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"assert"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"preset"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"lighthouse:recommended"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"upload"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"target"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"temporary-public-storage"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"server"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This configuration tells Lighthouse to use &lt;code&gt;npm run start&lt;/code&gt; and wait for the &lt;code&gt;ready on&lt;/code&gt; output from the Next.js server. It will then start Chrome and perform a Lighthouse audit on the specified URL (in this case, &lt;code&gt;http://localhost:3000&lt;/code&gt;, because the Next.js application is running on port 3000). You can configure multiple URLs, but for this example we will only use one. The number of runs can be modified, but we will stick with one run for this example. The more runs you configure, the longer the overall audit will take. In this example, we are using the desktop preset settings to emulate a standard desktop and use the recommended audit standards.&lt;/p&gt;

&lt;p&gt;Give it a try by running &lt;code&gt;npx lhci collect&lt;/code&gt;. Note that it creates a new &lt;code&gt;.lighthouseci&lt;/code&gt; folder which contains the report.&lt;/p&gt;

&lt;h2&gt;
  
  
  Integrate Lighthouse CI with GitHub actions
&lt;/h2&gt;

&lt;p&gt;Now that we have Lighthouse set up and running in our local environment, we want to use GitHub actions to run Lighthouse checks every time we push a new commit. To do this, we will use &lt;a href="https://github.com/apps/lighthouse-ci" rel="noopener noreferrer"&gt;the Lighthouse CI app from the GitHub marketplace&lt;/a&gt;. This app will post the results of the Lighthouse audit to our pull request. To use it, enable the app in your organization or account and save the &lt;code&gt;LHCI_GITHUB_APP_TOKEN&lt;/code&gt; as a secret in your GitHub repository.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Repository &amp;gt; Settings &amp;gt; Secrets &amp;gt; Actions&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We will now create a GitHub action to run Lighthouse CI every time a new commit is pushed. To do this, create a file called &lt;code&gt;.github/workflows/lighthouseci.yml&lt;/code&gt; under the root of your project with the following configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;CI&lt;/span&gt;
&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;push&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;lighthouseci&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v2&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/setup-node@v1&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;node-version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;16&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npm install &amp;amp;&amp;amp; npm install -g @lhci/cli@0.8.x&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npm run build&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;lhci collect&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;lhci upload&lt;/span&gt;
        &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
            &lt;span class="na"&gt;LHCI_GITHUB_APP_TOKEN&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ secrets.LHCI_GITHUB_APP_TOKEN }}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This action will run &lt;code&gt;npm install&lt;/code&gt; and &lt;code&gt;npm run build&lt;/code&gt; every time a new commit is pushed. It will then execute Lighthouse and upload the results. Because we added the &lt;code&gt;LHCI_GITHUB_APP_TOKEN&lt;/code&gt;, the results will be attached as new status checks and will pass or fail based on the assert configuration in &lt;code&gt;.lighthouserc&lt;/code&gt;.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjv9k8t9wwqwyd3dirhd6.jpg" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjv9k8t9wwqwyd3dirhd6.jpg" alt="Lighthouse results displayed in a Pull request on GitHub" width="800" height="140"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we click at the right side on the details link, we will get the full Lighthouse report for this commit. This report allows us to inspect every detail, just like when we perform an audit in a tool like DevTools.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs388gx6aefdh21e2fqat.jpg" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs388gx6aefdh21e2fqat.jpg" alt="Top part of a full Lighthouse report" width="800" height="145"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for reading my article. I hope it was helpful for your development workflow. If you found it valuable, please consider liking it. It would mean a lot.&lt;/p&gt;

</description>
      <category>emptystring</category>
    </item>
  </channel>
</rss>
