Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC CDN fallback for Style Bundle

Does MVC have a built in way to specify a CDN fallback for style sheets? I am trying to set up a fallback for the jQuery Mobile Structure style sheet. Here is my code in the RegisterBundles method:

var JQMstyleSheet = new StyleBundle("~/JQMstyle", "http://code.jquery.com/mobile/1.3.1/jquery.mobile.structure-1.3.1.min.css").Include("~/theme/jquery.mobile.structure-1.3.1.css");
JQMstyleSheet.CdnFallbackExpression = "window.jQuery.mobile";
bundles.Add(JQMstyleSheet);

When the page renders it outputs this to the html:

<script>
(window.jQuery.mobile)||document.write('<script src="/JQMstyle"><\/script>');
</script>

When the CDN fails it doesn't dynamically add the style sheet like it does perfectly for my javascript files. I think the problem is that it is trying to render a script, when it should be a style. Is there a different fallback property other than CdnFallbackExpression?

UPDATE

The Microsoft docs for System.Web.Optimization.StyleBundle shows a CdnFallbackExpression as an available property, however in the description it says "Gets the script extension rendered by the Scripts helper class..." http://msdn.microsoft.com/en-us/library/system.web.optimization.stylebundle(v=vs.110).aspx Is this a bug in the System.Web.Optimization.StyleBundle ? shouldn't that property by referencing the Styles helper class?

like image 344
Adrian Avatar asked Jan 08 '14 19:01

Adrian


Video Answer


1 Answers

TLDR;

Check out my solution which provides a StyleBundle extension method to solve the problem.

Style Bundle Fallback

Also

Yes there is a bug in the Microsoft ASP.NET Optimization Framework, documented here.

The solution is to modify the CdnFallbackExpression to be a javascript function that both checks for the stylesheet and loads the fallback, thus ignoring the bad script from the Optimization Framework.

There are a couple of tricky parts, especially checking for a stylesheet being loaded when it comes from another domain, like most CDN sources.

I have a solution on GitHub that you can use until the issue is fixed in the framework; however, I'd still watch out for the tricky part of determining when the stylesheet is actually loaded.

like image 99
GoClimbColorado Avatar answered Sep 27 '22 22:09

GoClimbColorado