CdnFallbackExpression for StyleBundles creates a <script> instead of a <link>

Sep 6, 2013 at 11:53 PM
Edited Sep 6, 2013 at 11:55 PM
This is my attempt at providing CSS fallback for a Bootstrap3 bundle.
BundleTable.Bundles.Add(new StyleBundle("~/bundles/bootstrap", "http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css") 
{
    CdnFallbackExpression = @"
function (url) {
    var length = document.styleSheets.length;
    for (var i = 0; i < length; i++) {
        var sheet = document.styleSheets[i];
        if (sheet.href = url) {
            var rules = sheet.rules ? sheet.rules : sheet.cssRules;
            if (rules == null || rules.length == 0) {
                return false;
            }
        }
    }
}('http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css')"
}.Include("~/Content/bootstrap/bootstrap.css"));
BundleTable.Bundles.UseCdn = true;
The rendered output is
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">
<script>(function (url) {
    var length = document.styleSheets.length;
    for (var i = 0; i < length; i++) {
        var sheet = document.styleSheets[i];
        if (sheet.href = url) {
            var rules = sheet.rules ? sheet.rules : sheet.cssRules;
            if (rules == null || rules.length == 0) {
                return false;
            }
        }
    }
}('http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css'))||document.write('<script src="/bundles/bootstrap"><\/script>');</script>
Note the fallback creates a <script> element instead of <link> even though it's a StyleBundle


https://github.com/qntmfred/CssCdnFallback
Sep 17, 2013 at 12:51 PM
I noticed this before even writing any code. What a silly bug! I've done you a favour and created an issue for this: https://aspnetoptimization.codeplex.com/workitem/104