Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 3 min read

Which files are blocking your first paint? A 20-line console snippet

When a page shows nothing for the first second or two, the usual suspects are the stylesheets and scripts in the <head>. The browser won't paint until it has them. Lighthouse lists these under "render-blocking requests",

When a page shows nothing for the first second or two, the usual suspects are the stylesheets and scripts in the <head>. The browser won't paint until it has them. Lighthouse lists these under "render-blocking requests", but I often just want the list for one page, on a real connection, right now.

Chrome tags every resource it downloads with a renderBlockingStatus. So you can ask the page directly.

The snippet

Open the page, let it finish loading, open DevTools > Console and paste:

(() => {
  const fcp = performance.getEntriesByName('first-contentful-paint')[0];
  const rows = performance.getEntriesByType('resource')
    .filter((e) => e.renderBlockingStatus === 'blocking')
    .map((e) => {
      const url = new URL(e.name);
      return {
        file: url.pathname.split('/').pop() || url.pathname,
        host: url.host,
        type: e.initiatorType,
        startMs: Math.round(e.startTime),
        doneMs: Math.round(e.responseEnd),
        kB: e.transferSize ? +(e.transferSize / 1024).toFixed(1) : 'n/a',
      };
    })
    .sort((a, b) => b.doneMs - a.doneMs);
  console.table(rows);
  if (fcp) console.log(`First contentful paint: ${Math.round(fcp.startTime)} ms`);
})();

You get a table of every render-blocking file, sorted so the one that finished last is on top. That top row is usually the one worth looking at first, since nothing paints until the slowest of them is in.

Reading the table

A few things that tripped me up the first time:

kB says "n/a". transferSize is 0 when the file came from cache, and it's always 0 for files from another domain unless that server sends a Timing-Allow-Origin header. Most font and tag-manager CDNs don't. For honest numbers, tick "Disable cache" in the Network tab and reload. Third-party files will still show n/a, but their doneMs timing is still there.

startMs is big for something in the head. That's often a file discovered late, like a font stylesheet pulled in by another stylesheet with @import. Two round trips before paint instead of one.

The table is empty. Either nothing blocks (nice), or you're not in a Chromium browser. renderBlockingStatus is supported in Chrome and Edge from version 107. Firefox only has it in preview builds and Safari doesn't support it, so there you'll get an empty table rather than an error.

What I usually find, and the fix

On client sites the list is rarely long. It's normally the same three kinds of thing:

  • A script in the head without defer. Chat widgets, review widgets, A/B testing tools. If it doesn't need to run before the page shows, add defer or move it to the end of the body. In Webflow that's the Footer code box instead of Head code. In WordPress, wp_enqueue_script has accepted 'strategy' => 'defer' since 6.3.
  • A web font stylesheet from another domain. Self-host the font files, or at least add preconnect for the font host so the connection starts earlier.
  • One big theme stylesheet. That one is usually supposed to block. The question is whether it's 40 kB or 400 kB.

A/B testing scripts are the exception to "just defer it". Many of them block on purpose so visitors don't see the original version flash before the variant. Check with whoever runs the tests before you touch it.

Before and after

Run the snippet before a fix and note the top row and the first contentful paint line. Make one change, reload with cache disabled, run it again. If the file is gone from the table, or the top row's doneMs dropped, you've moved first paint. If nothing changed, you fixed something that wasn't the bottleneck, which is also worth knowing before you touch the next thing.

Do it on a throttled connection too (pick one of the throttling presets in the Network tab). On fibre everything looks fine.

If you want to see which tools and scripts a site loads before you even open DevTools, we made a free checker for that: https://rizehub.ph/products/tech-inspector

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.