Skip to the code

Find render-blocking resources

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]

What the tests check

  1. a stylesheet and a classic script block; meta, title, and other links don't
    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]);
    
  2. async, defer, and module scripts don't block on their own
    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]]);
    
  3. classic means a JavaScript MIME type, in any case and with spaces around it, and other types never block
    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]]);
    
  4. blocking=render makes async and module scripts block, and an inline module, but never an inline classic script
    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]]);
    
  5. a stylesheet blocks only when its media matches, whatever blocking says
    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]]);
    
  6. uses matchesMedia for every media value when it's given
    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]]);
    
  7. rel and blocking are lists of words in any case, and a disabled stylesheet doesn't block
    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]]);
    
  8. a style element blocks when its media matches, and an empty head blocks nothing
    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([]);
    

Learn more

solution.jsSaved in this browser

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).