02

Getting a perfect score on PageSpeed Insights for my own website

A green PageSpeed score, and what the AI skipped to get it

Getting a full green score on PageSpeed Insights is easy now. Just give the AI the following prompt:

prompt.md
Core task: https://pagespeed.web.dev, optimize the performance score to 90 or above.

Notes:
1. If there are any issues, list them out and ask me before starting work. Once you have my confirmation, you can proceed.
2. After finishing, if there were any major adjustments, write a clear report.
3. If you find bugs or areas that need optimization during development, handle and resolve them on your own.

Then go brew a cup of coffee, have a brunch, and when you come back, here is what you will see:

Mobile score
Desktop score

The Pre-AI Era

Let me talk about how performance optimization used to work. Three years ago, you could not hand this off to an AI. Tuning performance was expected to take a week or even longer.

First, you need to know how the score is calculated. The performance score is a weighted combination of five metrics: FCP, SI, LCP, TBT, and CLS. To get a perfect score, FCP must be under 1580 milliseconds, LCP under 1930 milliseconds, and TBT under 90 milliseconds. If any single metric exceeds its threshold, no matter how well the others perform, you will not get a green light.

For FCP, you need to understand which resources block the first paint, what the difference is between async and defer on a <script> tag, how to use PurgeCSS to strip out unused styles from your framework, and when to reach for preload. For LCP, you need to understand the differences between runtime only, static generation, and hydration in a frontend framework, and you also need to know how to do segmented rendering, using a stage variable to control when components initialize.

The hardest part was TBT. You had to move compression libraries that have nothing to do with the visual output into a Worker, squeeze each file's compilation time under 50 milliseconds, and eventually even had to manually break apart the initialization of third-party libraries.

The AI Era

I gave the AI a target of getting the performance score above 90. The homepage has a koi pond, which is the most performance-intensive part of the entire site. On the first pass, the AI simply bypassed the scoring mechanism to get the points: PageSpeed stops recording three seconds after the page loads. As long as the koi fish appear a little later, the test never sees them. So it delayed the koi's entrance.

In terms of the score, it definitely succeeded. The numbers looked great. But visitors had to wait longer before they could see the koi, and the user experience suffered. After thinking about it, I realized the problem was in my prompt. I only said what score I wanted but did not say what was not allowed to be sacrificed. So I rewrote the prompt like this:

prompt.md
Core:
1. Bring the performance score above 90 without sacrificing any part of the user experience.
2. After running the https://pagespeed.web.dev optimizations, independently check the scores until the target is met.

Notes:
1. If anything is unclear, list your questions before starting work. Once I confirm, you can proceed.
2. When the work is done, if there were any major adjustments, write a clear report explaining them.
3. If you find bugs or areas that need optimization during development, handle and resolve them on your own.

After adding that first rule, the AI stopped simply deferring visual effects and instead began hunting for the real performance bottlenecks. In the end, the production site scored between 97 and 98 on mobile and between 97 and 100 on desktop, and the koi on the homepage still appeared as before.

Goodhart's Law

This law has a widely circulated formulation:

When a measure becomes a target, it ceases to be a good measure.

When a measure becomes a target, it ceases to be a good measure.


PageSpeed Insights exists to help developers improve website performance and user experience, but when we simply asked the AI to raise the score, it found a way to boost the rating at the cost of the user experience.

This phenomenon is common in other fields as well. When we cannot directly measure what we truly want, we typically substitute other quantifiable metrics, such as test scores for learning outcomes or social engagement numbers for content value. These metrics are only approximations of real value. When we chase the numbers too hard, we risk gradually drifting away from the goal we originally set out to reach.

Coming back to this development experience, PageSpeed's score is a tool for measuring website quality, not website quality itself. A site that scores 100 is not necessarily more worth a user's time than one that scores 95.

How to Get AI to Complete Tasks Correctly

This experience also made me rethink how to hand development tasks to AI. We often talk about Prompt Engineering, but rather than figuring out how to make instructions more complex, I believe what matters more is being able to clearly define a task.

Now I break a task into four parts: goal, constraints, process, and acceptance criteria.

  1. What do we actually want to achieve?

  2. What must not be sacrificed in the pursuit of that goal?

  3. How do we diagnose the problem, propose a solution, and verify the result?

  4. How do we confirm the task is truly complete, not just superficially meeting the standard?

prompt.md
Goal:
While maintaining the existing design, functionality, and user experience, raise the PageSpeed Insights performance score for both mobile and desktop to 90 or above.

Constraints:
1. Do not remove, simplify, or deliberately defer existing above-the-fold animations or interactive effects in order to boost the score.
2. Do not detect the testing environment and serve different content to regular visitors.
3. Do not sacrifice visual quality, functional completeness, SEO, or accessibility.
4. When major architectural changes are involved, the rationale, scope of impact, and potential risks must be explained in advance.

Process:
1. Establish a performance test baseline before optimization.
2. Use Lighthouse, Chrome DevTools Performance, and Network to analyze performance bottlenecks.
3. Prioritize by improvement benefit, modification cost, and risk.
4. After each major change, verify whether existing functionality and visual effects are affected.
5. Perform multiple measurements under the same test conditions.

Acceptance:
1. Both mobile and desktop performance scores reach 90 or above.
2. The original design, functionality, animations, and interactions pass regression testing.
3. Provide FCP, SI, LCP, TBT, and CLS data from before and after optimization.
4. Document the rationale, implementation, improvement results, and potential risks of major changes.
5. If the target cannot be met within the stated constraints, report honestly. Do not force the target by sacrificing user experience.

This method applies not only to website performance optimization but also to code refactoring, UI design, database optimization, and even non-development AI tasks.

The goal defines where we are heading, the constraints define which paths are off limits, and the acceptance criteria help me confirm whether we have actually arrived at the destination.

Conclusion

Three years ago, what would take a full week of research, testing, and tuning can now largely be handed to AI. But I do not believe the technical knowledge I have built up over the years has lost its value because of that.

This time, precisely because I understand the principles behind these techniques, I can see that "deferring entrance" is a way of bypassing the test

This time, precisely because I understand the principles behind these techniques, I am able to judge that the "deferring entrance" approach AI proposed is a way of bypassing the test. AI has lowered the barrier to implementation, but it has not lowered the importance of professional judgment. As the cost of implementation keeps dropping, knowing what is worth doing, what should not be done, and how to assess the quality of the results will perhaps become the more important skill in this era.


Before, we needed to know how to solve a problem.

Now, we need to know more clearly what problem we actually want to solve.