On a mission to enhance my website's performance, I set out to achieve the perfect score on PageSpeed Insights. Everything was going smoothly until I encountered an issue with CSS delivery.
I initially achieved a flawless result by utilizing the preload tag, but it failed to load in Firefox for some unknown reason. This led me to explore alternative solutions.
I then transitioned to the following approach:
<link rel="stylesheet" href="~/css/site.min.css" media="none" onload="if(media !== 'all')media='all';">
<noscript><link rel="stylesheet" href="~/css/site.min.css"></noscript>
Although this method proved to be effective, PageSpeed Insights did not recognize it, resulting in only an 85 score rating.
A similar outcome occurred when I attempted using
<link rel="stylesheet" href="~/css/site.min.css" media="none"/>
in the head section and <link rel="stylesheet" href="~/css/site.min.css">
at the end of the body.
Subsequently, I experimented with loading my CSS via JavaScript as follows:
<noscript id="deferred-styles">
<link rel="stylesheet" href="~/css/site.min.css"/>
</noscript>
<script>
var loadDeferredStyles = function() {
var addStylesNode = document.getElementById("deferred-styles");
var replacement = document.createElement("div");
replacement.innerHTML = addStylesNode.textContent;
document.body.appendChild(replacement);
addStylesNode.parentElement.removeChild(addStylesNode);
};
var raf = requestAnimationFrame ||
mozRequestAnimationFrame ||
webkitRequestAnimationFrame ||
msRequestAnimationFrame;
if (raf) raf(function() { window.setTimeout(loadDeferredStyles, 0); });
else window.addEventListener('load', loadDeferredStyles);
</script>
Surprisingly, this approach yielded the same unsatisfactory result! Despite employing code from their own site, PageSpeed Insights overlooked it. Can anyone shed light on why this might be the case?