Advanced

The critical path and what to inline

The critical path is the set of resources the first paint waits for. Everything else is optional and should not be on that path. Once you can name the set, the fix is mechanical: shrink that set, and make its members small.

What you will see

At least one small inline block for the first-patch rules, with the full sheet still linked.

About 1 min of reading

Try It

Name the critical set for one page

1

List the subresources

grep -oE '<(link|script|img)[^>]*(href|src)=' views/partials/head.ejs | head -10

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Everything the head requests. This is the candidate path.
2

Test one removal

comment out the font link, reload, note the first-paint time

Before you run it, say out loud what it will print. Then open the answer.

What it prints
If paint is earlier, the font was on the path and needs a smaller or later load.
3

Move the optional ones

grep -nE '

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Images below the fold should carry loading="lazy" so they are off the path.
4

Inline the patch

grep -c '

Before you run it, say out loud what it will print. Then open the answer.

What it prints
At least one small inline block for the first-patch rules, with the full sheet still linked.
Do not inline the whole stylesheet. It is re-downloaded on every HTML request and never cached across pages.

Data Flow

Resources to a path
1 Load the page and record first paint
↓
2 Block one resource, reload, compare
↓
3 Changed: it was on the path
↓
4 Unchanged: it was optional, make it lazy
Why this works
How to find the path

Block one resource at a time and reload. If the first paint changes, that resource was on the path. A handful of reloads names the whole set.

Where inlining hurts

Inlined bytes are re-sent on every HTML request and cannot be cached separately. Inline the small first-patch only, and keep the rest as a tokened file.

Examples

Name the set

Write down the resources first paint waits for. On a well-built page it is the HTML, one CSS file, and one or two fonts. Anything else is on the path by accident.

Inline only the first-patch CSS

The rules the first screen needs can be inlined so the first paint waits for one request instead of two. Inline the patch, not the whole stylesheet, or you lose the long cache.

Everything else is lazy

A resource the first paint does not wait for should be requested after it. That is what defer, async, loading=lazy, and IntersectionObserver are for.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← The network path: connections and negotiation
Previous Lesson
The network path: connections and negotiation
Next Lesson
Deferring work that is not needed yet
Deferring work that is not needed yet →