VENNStart
Project / vennee.dev

PageSpeed gave my site 25 on mobile. My own Lighthouse said 90+

My own Lighthouse gave vennee.dev 90+. PageSpeed mobile said 25. A loader hid the LCP, Lighthouse simulated a slow network, and WebGL ran on the CPU.

Lê Anh Tuấn5 min read

PageSpeed Insights gave an earlier cube-based vennee.dev homepage 25 on mobile, while my local Lighthouse said 90-something. That page opened with a loader that folded nine squares into a 3D cube and handed over to a WebGL scene, and the gap came from bugs stacked on top of each other. Numbers are from Lighthouse 12 on a production build with default mobile settings (simulated 4G, CPU slowed 4x).

The hero has to be painted under the loader

LCP was the hero's tagline at 4.7 seconds. The hero was at opacity: 0 until the loader finished, and Chrome doesn't count transparent elements as LCP candidates.

Dropping the loader would be quicker. I kept it because it's the part of this page I care about. The hero is now visible in the HTML, under the loader's opaque backdrop. Chrome doesn't care whether an element is covered. The tagline counts from the first paint, and only then does the hero hide and animate.

My first version hid the hero on hydration, and scores swung between 72 and 92 on one build. Sometimes hydration finished before the first paint, because cached scripts arrived before the CSS. The fix waits until the browser has drawn at least one frame:

// hooks/usePaintedOnce.ts
export function usePaintedOnce() {
  const [painted, setPainted] = useState(false);
  useEffect(() => {
    let second = 0;
    const first = requestAnimationFrame(() => {
      second = requestAnimationFrame(() => setPainted(true));
    });
    return () => {
      cancelAnimationFrame(first);
      cancelAnimationFrame(second);
    };
  }, []);
  return painted;
}

The second rAF callback runs once the first frame is on screen. Hacky, but the next five runs all had LCP equal to FCP.

One import pulled three.js into the first bundle

The chunk with three.js, R3F and drei (581 KB uncompressed) was referenced straight from the initial HTML, even though the 3D scene loads through dynamic(). The culprit was one line in the loader:

import { useProgress } from "@react-three/drei";

drei pulls in three, and the build doesn't warn. I replaced it with a small store in lib/scene-progress.ts: the loader reads the store, and the scene calls drei's useProgress itself once it has loaded.

LCP on localhost can't be trusted

Localhost still reported a 4.3 second LCP, against 94 ms in the trace. By default Lighthouse loads at full speed and lets Lantern replay the dependency graph on a simulated slow network. For LCP, the "pessimistic" graph takes every request that finished before the observed LCP:

// lantern/metrics/LargestContentfulPaint.js (Lighthouse 12)
return FirstContentfulPaint.getFirstPaintBasedGraph(dependencyGraph, {
  cutoffTimestamp: lcp,
  treatNodeAsRenderBlocking: _ => true,
  additionalCpuNodesToTreatAsRenderBlocking: node => node.didPerformLayout(),
});

On localhost all the JS and fonts are done by the first paint. Lantern replays around 160 KB of fonts and 330 KB of JS over a 1.6 Mbps link and gets 4 seconds, so the faster the page loads locally, the harder it gets punished. Against the Vercel preview, more than half my runs came in between 1.5 and 2.5 seconds. I now use localhost LCP only to compare before and after.

Lantern is right that every byte before the first paint counts, so I cut fonts. The title font's italics and Regular face were preloaded everywhere and used nowhere. The title font moved to WOFF2, taking Bold from 57 KB to 31 KB. JetBrains Mono stopped preloading Cyrillic and Latin Extended. The subsets option in next/font only picks what gets preloaded, and the rest still loads through unicode-range.

The site's bot regex includes lighthouse, which never matches. Lighthouse 12's mobile user agent is a "moto g power (2022)" on Chrome 136.

Reproduce CPU-based WebGL rendering with SwiftShader

Without a GPU, Chrome can draw WebGL with SwiftShader, on the CPU. Recent desktop Chrome only falls back to it when started with --enable-unsafe-swiftshader; otherwise getContext returns null. Running Lighthouse with --use-angle=swiftshader reproduced it: TBT jumped to 1,770 ms, with a task of over a second where the page created a WebGL context.

That context existed for the loader line showing your graphics card's name. The first context has to boot SwiftShader, and getContext is synchronous, so the main thread waited over a second for text almost nobody reads. My first fix made it worse: a GPU check that also created a context on the main thread, with one run hitting 7,880 ms of TBT. Probing in a Web Worker with OffscreenCanvas moves context creation off the main thread:

// lib/webgl-support.ts (trimmed)
const WORKER_SOURCE = `
  const canvas = new OffscreenCanvas(1, 1);
  const gl = canvas.getContext("webgl", { failIfMajorPerformanceCaveat: true });
  const info = gl && gl.getExtension("WEBGL_debug_renderer_info");
  postMessage({
    // Refused under the caveat flag: is there a software context at all?
    supported: Boolean(gl || new OffscreenCanvas(1, 1).getContext("webgl")),
    caveat: !gl,
    renderer: info ? String(gl.getParameter(info.UNMASKED_RENDERER_WEBGL)) : "",
  });
`;

const SOFTWARE_RENDERER = /swiftshader|llvmpipe|softpipe|software|basic render driver/i;

The 3D cube only turns on with a hardware renderer. Otherwise the page keeps the CSS cube. failIfMajorPerformanceCaveat is supposed to refuse software renderers, but with --enable-unsafe-swiftshader Chrome handed over a context anyway, so I also check the renderer name. Browsers without WebGL in workers, like older Safari, count as having a GPU.

I don't see this as gaming PageSpeed. Virtual machines, old office PCs and blocklisted drivers take the same path. With the GPU disabled, mobile went from 61 to 86 and TBT from 1,770 ms to 10 ms. Desktop went from 78 to 99.

The loader waits for the scene's first frame

With WebGL after the first paint, phones sometimes showed the loader's 2D face fading over an empty canvas, the cube popping in half a second later. The handover condition had an older bug:

&& ((liteStartupRef.current && !returning) || (!active && webglProgress >= 1))

On "lite" devices the first half is always true, so the WebGL check got skipped, and only slow connections exposed it. Now the scene reports its first drawn frame from a component placed after the cube in the same Suspense:

function SceneDrawnSignal() {
  const sent = React.useRef(false);
  useFrame(() => {
    if (sent.current) return;
    sent.current = true;
    publishSceneProgress({ drawn: true });
  });
  useEffect(() => () => publishSceneProgress({ drawn: false }), []);
  return null;
}

The loader waits for drawn for at most 2.5 seconds after everything else is ready. Until the GPU probe answers, the wait is 0.8 seconds, and machines without a GPU don't wait. On simulated 4G the canvas appeared at 4,633 ms and the loader entered reveal at 5,085 ms.

The CSS cube was sized in vh, the height with the address bar hidden, while the canvas matches the visible area at load. I haven't measured it on a real phone, but svh is the unit that matches. The size now goes through a --cube-vh variable, 1vh by default and 1svh under @supports.

Scores after the fixes

Lighthouse mobile on the preview swings between 80 and 99, median around 90. I haven't found why some runs draw the first frame much later. I'll only see the real PageSpeed number once this is in production. Real-user data is 28 days of Chrome UX Report, which means weeks.

Next time PageSpeed looks oddly low, I'll first rerun Lighthouse with --use-angle=swiftshader. It takes under a minute.

  • Performance
  • Lighthouse
  • Core Web Vitals
  • WebGL
  • Next.js
  • React Three Fiber

I'm Tuấn (Venn), a web developer in Ho Chi Minh City. I mostly build Next.js sites and web apps, and I take freelance work.

Work with me

More posts

  1. 01Guide4 min read

    Deploying Next.js and NestJS to AWS EC2 with Docker Compose

    How I deploy a pnpm monorepo to one EC2 box. Images get built in GitHub Actions and Docker Compose pulls them from GHCR. The server has no code.

  2. 02Guide4 min read

    How I store OAuth tokens for social media APIs in NestJS

    OAuth for Facebook, TikTok and YouTube in a NestJS API. State and PKCE, encrypted tokens, refreshing before each publish, and when to ask for a reconnect.

  3. 03vennee.dev3 min read

    llms.txt on my Next.js site, and now Lighthouse grades it

    vennee.dev has a hand-written llms.txt and an llms-full.txt built from the blog. Notes on the spec, .txt routes in Next.js, and Lighthouse 13's new audit.