Get your <head>
in order
Inspired by Harry Roberts' work on ct.css and Vitaly Friedman's Nordic.js 2022 presentation:
How you order elements in the <head>
can have an effect on the (perceived) performance of the page.
This script helps you identify which elements are out of order.
โจ New: Install the Capo Chrome extension โจ
- Copy capo.js
- Run it in a new DevTools snippet, or use a bookmarklet generator
- Explore the console logs
![capo screenshot](https://private-user-images.githubusercontent.com/1120896/239570699-b29672f9-1f05-4a05-a85e-df27acd153bd.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ1NTQ0MjAsIm5iZiI6MTcyNDU1NDEyMCwicGF0aCI6Ii8xMTIwODk2LzIzOTU3MDY5OS1iMjk2NzJmOS0xZjA1LTRhMDUtYTg1ZS1kZjI3YWNkMTUzYmQucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDgyNSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA4MjVUMDI0ODQwWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ZDQ5OGY0NTEwNmRmZWM1YzU5ZmZkYTJkYWNiNTQ5Nzg4YzRlZTdkMmFiNGM1ZWFlNTUyZjU1Y2NmMWQ4NjIwMCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.FxB6N_CIhpKIyjwwDp1Tf0rHqDqxFjJiB1jZQ7tw1Bc)
For applications that add lots of dynamic content to the <head>
on the client, it'd be more accurate to look at the server-rendered <head>
instead.
WIP see crx/
You can use the capo
WebPageTest custom metric to evaluate only the server-rendered HTML <head>
. Note that because this approach doesn't output to the console, we lose the visualization.
You can also use the httparchive.fn.CAPO
function on BigQuery to process HTML response bodies in the HTTP Archive dataset. Similar to the WebPageTest approach, the output is very basic.
Alternatively, you can use local overrides in DevTools to manually inject the capo.js script into the document so that it runs before anything else, eg the first child of <body>
. Harry Roberts also has a nifty video showing how to use this feature. This has some drawbacks as well, for example the inline script might be blocked by CSP.
Another idea would be to use something like Cloudflare workers to inject the script into the HTML stream. To work around CSP issues, you can write the worker in such a way that it parses out the correct nonce
and adds it to the inline script. (Note: Not tested, but please share examples if you get it working! ๐)
The script logs two info groups to the console: the actual order of the <head>
, and the optimal order. In this collapsed view, you can see at a glance whether there are any high impact elements out of order.
Each "weight" has a corresponding color, with red being the highest and blue/grey being the lowest. See capo.js for the exact mapping.
Here are a few examples.
![image](https://private-user-images.githubusercontent.com/1120896/239564002-5c19e758-9f88-42c1-81e8-9f757a6c92be.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ1NTQ0MjAsIm5iZiI6MTcyNDU1NDEyMCwicGF0aCI6Ii8xMTIwODk2LzIzOTU2NDAwMi01YzE5ZTc1OC05Zjg4LTQyYzEtODFlOC05Zjc1N2E2YzkyYmUucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDgyNSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA4MjVUMDI0ODQwWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ODA1MDEyODA3NDIzMjQ0YzRhZTYyM2ZmMmIxNGRlYTNkNzY2ZmEyMzc2MTUxODJjODdjNGMwZTgwOWIxYjkzNSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.Eh9KWuzy01xdGTOdCO2pTd3B9kQjeseZ__jn4NS2CIU)
![image](https://private-user-images.githubusercontent.com/1120896/239564471-798a0e99-04dd-4d27-a241-b5d77320a46e.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ1NTQ0MjAsIm5iZiI6MTcyNDU1NDEyMCwicGF0aCI6Ii8xMTIwODk2LzIzOTU2NDQ3MS03OThhMGU5OS0wNGRkLTRkMjctYTI0MS1iNWQ3NzMyMGE0NmUucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDgyNSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA4MjVUMDI0ODQwWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9MTNkZTkxZTc2OGIwZmMwM2UzYjk1OGVjYWIwYmMxNDIxYjRkNjRiMTJlYmY3MGJiYmZmODJjYzA4NzU2M2ViOSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.xsGBUFZRlpS4oOANgfXuqEqIcb3htIMAEbvFylsh8Ew)
![image](https://private-user-images.githubusercontent.com/1120896/239565494-fe6bb67c-697a-4fdf-aa28-52429239fcf5.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZX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LWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA4MjVUMDI0ODQwWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9YzlmM2M2MTJkNzZjNWNjM2Y0NGNjZDJkMWVjZDMxMWI4MDBkMmVlMTY4Y2YxMDMyYzY2MDU0NWE1ZGU3NGM2ZSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.rWS6OA3IbTcxC0ch0vA1L3zbrirmzMQUVMm5mHAFcuE)
Expanding the actual or sorted views reveals the detailed view. This includes an itemized list of each <head>
element and its weight as well as a reference to the actual or sorted <head>
element.
Here you can see a drilled-down view of the end of the <head>
for the NYT site, where high impact origin trial meta elements are set too late.
![image](https://private-user-images.githubusercontent.com/1120896/239567027-c0342d54-9e23-4b91-8df1-277c251ee0c5.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ1NTQ0MjAsIm5iZiI6MTcyNDU1NDEyMCwicGF0aCI6Ii8xMTIwODk2LzIzOTU2NzAyNy1jMDM0MmQ1NC05ZTIzLTRiOTEtOGRmMS0yNzdjMjUxZWUwYzUucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDgyNSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA4MjVUMDI0ODQwWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9N2ZlODg3NTdmNmM2N2YzZDYyODc0NTFmZTE0YjYzZjYxNGIwYTQ2YTFmYmQ0Mjk0ZDA3OTY1NmE2Y2ExMmE3ZCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.C99EWWsxrAsMc5gXSSrwq3G6I-M2w5ff9OjZgaTvml8)