Skip to content

Commit 1d0301d

Browse files
committed
feat: enhance CSS loading performance with preload links and polyfill
1 parent 3f763b7 commit 1d0301d

1 file changed

Lines changed: 72 additions & 6 deletions

File tree

Web/Pages/Shared/_Layout.cshtml

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,20 @@
8484

8585
<!-- Feeds and Sitemap -->
8686
<link rel="sitemap" type="application/xml" title="Sitemap" href="/sitemap.xml" />
87-
<link rel="alternate" type="application/rss+xml" title="Latest Tips" href="/feed.rss" />
88-
<!-- Fonts and External CSS -->
87+
<link rel="alternate" type="application/rss+xml" title="Latest Tips" href="/feed.rss" /> <!-- Fonts and External CSS -->
8988
<link href="https://fonts.googleapis.com/css2?family=Outfit:wght@800&display=swap" rel="stylesheet" />
90-
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
91-
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css" />
92-
<link id="prism-theme" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism.min.css" /> <!-- Site CSS -->
89+
90+
<!-- Preload critical CSS for faster loading -->
91+
<link rel="preload" href="~/lib/bootstrap/dist/css/bootstrap.min.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
92+
<noscript><link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css"></noscript>
93+
94+
<link rel="preload" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
95+
<noscript><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css"></noscript>
96+
97+
<link rel="preload" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism.min.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
98+
<noscript><link id="prism-theme" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism.min.css"></noscript>
99+
100+
<!-- Site CSS -->
93101
<environment exclude="Development">
94102
<link rel="stylesheet" href="~/css/bundle.min.css" asp-append-version="true" />
95103
</environment>
@@ -230,7 +238,65 @@
230238
}
231239
}
232240
});
233-
});
241+
}); </script>
242+
243+
<!-- CSS loading polyfill for better browser support -->
244+
<script>
245+
/*! loadCSS. [c]2017 Filament Group, Inc. MIT License */
246+
(function(w){
247+
"use strict";
248+
if(!w.loadCSS){
249+
w.loadCSS = function(){};
250+
}
251+
var rp = loadCSS.relpreload = {};
252+
rp.support = (function(){
253+
var ret;
254+
try {
255+
ret = w.document.createElement("link").relList.supports("preload");
256+
} catch (e) {
257+
ret = false;
258+
}
259+
return function(){
260+
return ret;
261+
};
262+
})();
263+
rp.bindMediaToggle = function(link){
264+
var finalMedia = link.media || "all";
265+
function enableStylesheet(){
266+
if(link.addEventListener){
267+
link.removeEventListener("load", enableStylesheet);
268+
}
269+
link.media = finalMedia;
270+
}
271+
if(link.addEventListener){
272+
link.addEventListener("load", enableStylesheet);
273+
}
274+
link.onloadcssdefined = enableStylesheet;
275+
};
276+
rp.poly = function(){
277+
if(rp.support()){
278+
return;
279+
}
280+
var links = w.document.getElementsByTagName("link");
281+
for(var i = 0; i < links.length; i++){
282+
var link = links[i];
283+
if(link.rel === "preload" && link.getAttribute("as") === "style" && !link.getAttribute("data-loadcss")){
284+
link.setAttribute("data-loadcss", true);
285+
rp.bindMediaToggle(link);
286+
}
287+
}
288+
};
289+
if(!rp.support()){
290+
rp.poly();
291+
var run = w.setInterval(rp.poly, 500);
292+
if(w.addEventListener){
293+
w.addEventListener("load", function(){
294+
rp.poly();
295+
w.clearInterval(run);
296+
});
297+
}
298+
}
299+
})(this);
234300
</script>
235301

236302
@await RenderSectionAsync("Scripts", required: false)

0 commit comments

Comments
 (0)