All posts

How I Achieved a Perfect Lighthouse Score

I rebuilt my site with simpler rules for Claude, reduced JavaScript to zero, and reached a perfect Lighthouse score.

I recently built this website with the help of Claude. It was a straightforward project, so I just chose Astro as the framework, put some basic guardrails in place, left the code to Claude, and trusted the agent for building it.

I launched the website. Although it looked as I intended and it was overall fast to load and navigate, out of habit I ran a few Lighthouse tests. The results were unacceptable to me for a simple website like this.

Lighthouse report before the rewrite: performance 90, LCP 3.6 s
Lighthouse report before the rewrite: performance 90, LCP 3.6 s

What was slowing the site down

I ran the Lighthouse test and bundle analyser multiple times and asked Claude to analyse them and find the reasons for those numbers.

This is what we found:

  • JavaScript: 7 files totalling 204 kB, most of it React and React DOM.
  • CSS: 1 file at 11.4 kB. That’s small, but it could have been much smaller.
  • Performance: the score was 90, with a 3.6s LCP.
  • Build time: Cloudflare builds took about 1m30s.

From here, I took a look at the code, and almost all of the UI had been written in React. This was completely unnecessary, as not all the UI elements had interactivity.

The other issue was the amount of CSS. Claude decided to use CSS modules, handled things like fonts by hand, and loaded the theme upfront instead of using Astro’s official way of loading them properly.

And the last issue was the number of files in the codebase. Claude had created an atomic file and CSS file for almost every piece of UI, which was bloating the codebase.

Not all of these were affecting the performance score; some had increased the build time and the overall built package size.

How I fixed it

Git diff after the hard reset: 17,156 lines added, 24,250 removed
Git diff after the hard reset: 17,156 lines added, 24,250 removed

I hard reset the entire codebase, created a base Astro project, and defined these rules for Claude:

  • Astro components first; only create React components for complex UI interactivity.
  • Write the styles within the same file as the Astro component unless the styles are shared.
  • Write the JavaScript code within the same component file, unless it is shared.
<!-- Component -->
<header>...</header>

<!-- Styles -->
<style>...</style>

<!-- JS Code -->
<script>...</script>

These decisions create a leaner codebase that is easier to navigate for me and for agents.

Before and after

After the rewrite and deploying the new version, I reran the same tests and the results looked like this:

  • JavaScript: 0 files, 0 kB (100% less). All the unnecessary JS is gone.
  • CSS: 1 file at 5.0 kB, down from 11.4 kB (56% less).
  • Performance: the score rose from 90 to 100 (11% higher).
  • Build time: Cloudflare builds dropped from about 1m30s to about 45s (50% less).
Lighthouse report after the rewrite: performance 100, LCP 1.4 s
Lighthouse report after the rewrite: performance 100, LCP 1.4 s

What I’m taking from this

Once again I reminded myself of good software principles like “KISS” and to remember to use them when applicable, even when letting an agent write the code.

Claude made a mess because I gave it only a goal and no limits, so it kept adding. I learnt that simple isn’t the default for AI agents, and they need guidance and steering to achieve it.

If I started all over again, I would simply talk with the agent before it starts, try to understand how it wants to build, and try to help it like a good colleague.

All posts