{"id":106,"date":"2025-08-05T13:57:11","date_gmt":"2025-08-05T13:57:11","guid":{"rendered":"http:\/\/www.bacondrivencoding.com\/2008\/?p=106"},"modified":"2025-08-05T13:57:11","modified_gmt":"2025-08-05T13:57:11","slug":"incompetent-browsers-gold-plating-the-css-loader","status":"publish","type":"post","link":"https:\/\/www.bacondrivencoding.com\/2008\/2025\/08\/incompetent-browsers-gold-plating-the-css-loader\/","title":{"rendered":"Incompetent Browsers: gold plating the CSS loader"},"content":{"rendered":"<p>Previously: <a href=\"https:\/\/www.bacondrivencoding.com\/2008\/2025\/07\/incompetent-browsers-browser-grading\/\">Browser Grading<\/a>, <a href=\"https:\/\/www.bacondrivencoding.com\/2008\/2025\/07\/incompetent-browsers-browser-detection\/\">Historical Browser Detection<\/a>, <a href=\"https:\/\/www.bacondrivencoding.com\/2008\/2025\/07\/incompetent-browsers-towards-a-complete-modern-solution-part-i\/\">Requirements and Loaders<\/a>, <a href=\"https:\/\/www.bacondrivencoding.com\/2008\/2025\/08\/incompetent-browsers-towards-a-complete-modern-solution-part-ii\/\">Detection and Fallback<\/a>, <a href=\"https:\/\/www.bacondrivencoding.com\/2008\/2025\/08\/incompetent-browsers-a-complete-modern-solution\/\">The Complete Solution<\/a>.<\/p>\n<p>We have a good quality solution, and I recommend that you stick with it. But what if we went a little crazy?<\/p>\n<h3>Madness<\/h3>\n<p>Our solution has a problem: it uses <tt>document.write()<\/tt>. And if you&#8217;re using Lighthouse to evaluate your page quality, this will ding you. (I think this is overblown. The primary reason people want you to avoid <tt>doocument.write()<\/tt> is because of bad adtech snippets that <a href=\"https:\/\/csswizardry.com\/2023\/01\/why-not-document-write\/#documentwrite-executes-synchronously\" rel=\"noopener\" target=\"_blank\">synchronously load JavaScript<\/a>, blocking parsing for the entire download time. That&#8217;s not what we&#8217;re doing here, but the advice is overtuned.) But sometimes your boss tells you to get rid of it so you get a clean lighthouse score.<\/p>\n<p>So we have another alternative. We can <a href=\"https:\/\/www.bacondrivencoding.com\/2008\/2025\/07\/incompetent-browsers-towards-a-complete-modern-solution-part-i\/#loading-css\" rel=\"noopener\" target=\"_blank\">load the CSS asynchronously, and block rendering ourselves<\/a>.<\/p>\n<p>Now if we just wrote a normal HTML page, We&#8217;d link the CSS in the <tt>head<\/tt> and the JavaScript at the end of the <tt>body<\/tt>, meaning that when the JavaScript loads, the DOM is completely built and the CSS has all been loaded. If we loaded our JavaScript asynchronously, we could schedule our activity for <tt>DOMContentLoaded<\/tt> or even for <tt>load<\/tt> if we needed them. But when we asynchronously load our CSS and our JavaScript, we&#8217;ll need some other event to rely on.<\/p>\n<p>So we can change our loader to fire off the events that we want!<\/p>\n<pre>\r\n<code>\r\nvar target = document.getElementsByTagName(\"script\")[0];\r\nvar head = target.parentNode;\r\n\r\nfunction hideDocumentUntilLoaded() {\r\n  function bootstrap() {\r\n    document.readyforscripting = true;\r\n    var e = new CustomEvent(\"readyforscripting\");\r\n    document.dispatchEvent(e);\r\n  }\r\n  var hide = document.createElement(\"style\");\r\n  hide.innerText = \"body {display:none;}\";\r\n  head.insertBefore(hide, target);\r\n  document.addEventListener(\r\n    \"stylesloaded\",\r\n    function () {\r\n      head.removeChild(hide);\r\n      if (document.readyState != \"loading\") {\r\n        bootstrap();\r\n      } else {\r\n        document.addEventListener(\"DOMContentLoaded\", bootstrap, false);\r\n      }\r\n    },\r\n    false\r\n  );\r\n}\r\n\r\nvar waiting = {};\r\nvar complete = {};\r\n\r\nvar handler = {\r\n  handleEvent: function (event) {\r\n    complete[event.target.href] = event.type === \"load\";\r\n    delete waiting[event.target.href];\r\n    var size = 0;\r\n    for (var key in waiting) {\r\n      if (Object.hasOwnProperty.call(waiting, key)) {\r\n        size++;\r\n      }\r\n    }\r\n    if (size === 0) {\r\n      try {\r\n        var e = new CustomEvent(\"stylesloaded\", { detail: complete });\r\n        document.dispatchEvent(e);\r\n      } catch (e) {\r\n        if (\r\n          e.name === \"TypeError\" &&\r\n          e.message === \"Object doesn't support this action\"\r\n        ) {\r\n          \/\/ swallow error from IE 9-11 CustomEvent is not new-able\r\n        } else {\r\n          throw e;\r\n        }\r\n      }\r\n    }\r\n  },\r\n};\r\nfunction insertStyle(src) {\r\n  var ss = document.createElement(\"link\");\r\n  ss.setAttribute(\"rel\", \"stylesheet\");\r\n  ss.setAttribute(\"href\", src);\r\n\r\n  if (ss.addEventListener) {\r\n    ss.addEventListener(\"load\", handler, false);\r\n    ss.addEventListener(\"error\", handler, false);\r\n    waiting[ss.href] = true;\r\n  }\r\n\r\n  head.insertBefore(ss, target);\r\n}\r\n\r\ninsertStyle(\"main.css\");\r\ninsertStyle(\"more.css\");\r\ninsertStyle(\"evenmore.css\");\r\nif (document.addEventListener) hideDocumentUntilLoaded();\r\n<\/code>\r\n<\/pre>\n<p>As before, we hide the document with a <tt>style<\/tt> tag which we remove once the real CSS is loaded. But now we track which <tt>link<\/tt> tags are in progress and only remove it once they&#8217;re all done. (We use <tt>handleEvent<\/tt> here so we can process on both successful load and error.) When all the styles have loaded or failed, we fire a <tt>stylesloaded<\/tt> event.<\/p>\n<p>When both <tt>stylesloaded<\/tt> and <tt>DomContentLoaded<\/tt> have fired, we fire <tt>readyforscripting<\/tt> and set <tt>document.readyforscripting<\/tt>, which we can use in our main script files to only start doing DOM manipulation when we&#8217;re actually ready. IE doesn&#8217;t let us make custom events, so those events don&#8217;t trigger in IE. (Not that you should be loading anything in IE anyway.)<\/p>\n<link rel=\"stylesheet\" href=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/highlight.js\/11.11.1\/styles\/default.min.css\">\n<script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/highlight.js\/11.11.1\/highlight.min.js\"><\/script><br \/>\n<script>hljs.highlightAll();<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Previously: Browser Grading, Historical Browser Detection, Requirements and Loaders, Detection and Fallback, The Complete Solution. We have a good quality solution, and I recommend that you stick with it. But what if we went a little crazy? Madness Our solution has a problem: it uses document.write(). And if you&#8217;re using Lighthouse to evaluate your page [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-106","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/posts\/106","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/comments?post=106"}],"version-history":[{"count":5,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/posts\/106\/revisions"}],"predecessor-version":[{"id":112,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/posts\/106\/revisions\/112"}],"wp:attachment":[{"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/media?parent=106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/categories?post=106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacondrivencoding.com\/2008\/wp-json\/wp\/v2\/tags?post=106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}