Nothing appears on screen until the browser has the stylesheets and synchronous scripts in the page's <head>: they're render-blocking, and each one stands between the reader and the first paint. Write renderBlocking(head, matchesMedia). head is the head's elements in document order, as objects like { tag: "link", attributes: { rel: "stylesheet", href: "/css/menu.css" } }, with attribute names in lowercase and a boolean attribute such as async given as "". Return the elements that block rendering, in order, as the same objects.
Use the HTML Standard's rules for elements the parser finds in the head. A link blocks when its rel includes stylesheet (rel is a list of words separated by spaces, in any case), it has no disabled attribute, and its media matches. A style blocks when its media matches, since its CSS has to be parsed first. A script with a src blocks when it's a classic script without async or defer. A script is classic when its type is missing, empty, or a JavaScript MIME type such as text/javascript (in any case, with spaces around it ignored), and a module when its type is module (in any case); with any other type, like application/ld+json or importmap, it isn't run as a script file and never blocks.
blocking="render" makes a script block too: one with a src, classic or module, async or not, and a module script written inline, which then waits for its imports. blocking is a list of words as well, in any case. An inline classic script never blocks rendering: there's nothing to fetch, so it runs the moment the parser reaches it (holding up the parser while it runs, which is another matter). On a stylesheet, blocking="render" changes nothing: one whose media matches blocks anyway, and one whose media doesn't never does. Nothing else in these heads blocks: not meta, title, or base, nor links such as preload, preconnect, and icon.
matchesMedia(media) says whether a media value matches the screen; a missing or empty media always matches. When matchesMedia isn't given, treat the page as a screen: all and screen match, and every other value, print included, doesn't.
renderBlocking([
{ tag: "meta", attributes: { charset: "utf-8" } },
{ tag: "link", attributes: { rel: "stylesheet", href: "/css/menu.css" } },
{ tag: "link", attributes: { rel: "stylesheet", href: "/css/print.css", media: "print" } },
{ tag: "script", attributes: { src: "/js/consent.js" } },
{ tag: "script", attributes: { src: "/js/menu.js", defer: "" } },
]);
// [the menu.css link, the consent.js script]
const head = [
{ tag: "meta", attributes: { charset: "utf-8" } },
{ tag: "title", attributes: {} },
{ tag: "link", attributes: { rel: "preconnect", href: "https://fonts.gstatic.com", crossorigin: "" } },
{ tag: "link", attributes: { rel: "stylesheet", href: "/css/menu.css" } },
{ tag: "link", attributes: { rel: "preload", href: "/fonts/lora.woff2", as: "font", crossorigin: "" } },
{ tag: "link", attributes: { rel: "icon", href: "/favicon.svg" } },
{ tag: "script", attributes: { src: "/js/consent.js" } },
];
const blocking = renderBlocking(head);
expect(blocking).toEqual([head[3], head[6]]);
expect(blocking[0]).toBe(head[3]);
const head = [
{ tag: "script", attributes: { src: "/js/analytics.js", async: "" } },
{ tag: "script", attributes: { src: "/js/menu.js", defer: "" } },
{ tag: "script", attributes: { src: "/js/cart.js", type: "module" } },
{ tag: "script", attributes: { src: "/js/consent.js", type: "text/javascript" } },
{ tag: "script", attributes: { src: "/js/legacy.js", type: "" } },
];
expect(renderBlocking(head)).toEqual([head[3], head[4]]);
const head = [
{ tag: "script", attributes: { src: "/js/a.js", type: " TEXT/JavaScript " } },
{ tag: "script", attributes: { src: "/js/b.js", type: "application/javascript" } },
{ tag: "script", attributes: { src: "/data/menu.json", type: "application/json" } },
{ tag: "script", attributes: { type: "application/ld+json" } },
{ tag: "script", attributes: { type: "importmap", blocking: "render" } },
{ tag: "script", attributes: { src: "/js/c.js", type: "text/plain", blocking: "render" } },
];
expect(renderBlocking(head)).toEqual([head[0], head[1]]);
const head = [
{ tag: "script", attributes: { src: "/js/experiment.js", async: "", blocking: "render" } },
{ tag: "script", attributes: { src: "/js/theme.js", type: "Module", blocking: "Render" } },
{ tag: "script", attributes: { type: "module", blocking: "render" } },
{ tag: "script", attributes: { blocking: "render" } },
{ tag: "script", attributes: { defer: "" } },
{ tag: "script", attributes: { src: "/js/cart.js", type: "module", blocking: "" } },
];
expect(renderBlocking(head)).toEqual([head[0], head[1], head[2]]);
const head = [
{ tag: "link", attributes: { rel: "stylesheet", href: "/css/print.css", media: "print" } },
{ tag: "link", attributes: { rel: "stylesheet", href: "/css/receipt.css", media: "print", blocking: "render" } },
{ tag: "link", attributes: { rel: "stylesheet", href: "/css/all.css", media: "all" } },
{ tag: "link", attributes: { rel: "stylesheet", href: "/css/screen.css", media: "screen" } },
{ tag: "link", attributes: { rel: "stylesheet", href: "/css/empty.css", media: "" } },
{ tag: "link", attributes: { rel: "stylesheet", href: "/css/menu.css", blocking: "render" } },
];
expect(renderBlocking(head)).toEqual([head[2], head[3], head[4], head[5]]);
const head = [
{ tag: "link", attributes: { rel: "stylesheet", href: "/css/wide.css", media: "(min-width: 1024px)" } },
{ tag: "link", attributes: { rel: "stylesheet", href: "/css/narrow.css", media: "(max-width: 1023px)" } },
{ tag: "link", attributes: { rel: "stylesheet", href: "/css/screen.css", media: "screen" } },
{ tag: "style", attributes: { media: "(min-width: 1024px)" } },
{ tag: "link", attributes: { rel: "stylesheet", href: "/css/base.css" } },
];
const wideScreen = (media) => media === "(min-width: 1024px)";
expect(renderBlocking(head, wideScreen)).toEqual([head[0], head[3], head[4]]);
const head = [
{ tag: "link", attributes: { rel: "Stylesheet", href: "/css/menu.css" } },
{ tag: "link", attributes: { rel: " STYLESHEET ", href: "/css/cart.css" } },
{ tag: "link", attributes: { rel: "stylesheet", href: "/css/dark.css", disabled: "" } },
{ tag: "link", attributes: { rel: "preload", href: "/css/later.css", as: "style", blocking: "render" } },
{ tag: "script", attributes: { src: "/js/theme.js", async: "", blocking: "style RENDER" } },
];
expect(renderBlocking(head)).toEqual([head[0], head[1], head[4]]);
const head = [
{ tag: "style", attributes: {} },
{ tag: "style", attributes: { media: "print" } },
{ tag: "style", attributes: { media: "screen" } },
];
expect(renderBlocking(head)).toEqual([head[0], head[2]]);
expect(renderBlocking([])).toEqual([]);
Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.
Ctrl + Enter runs the tests (⌘ + Enter on a Mac).
I'm here to help with Find render-blocking resources. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.