Question

I'm optimizing my ASP.net MVC 4 website. I'm currently using run time bundling to combine and minify my JavaScript and CSS content. I would like automate publishing of the minified content to a CDN (specifically Amazon Cloudfront) as they are created.

I am trying to determine the best strategy for integrating bundled files with a CDN. My specific questions are:

  • Are there any available libraries that would allow bundled files to be saved to a CDN rather than my local web server?
  • Should static files be pulled from a CDN prior to bundling or should they be pulled from a local Web server prior to bundling?
  • Are there any mechanisms to enable CDN write control only from my web farm (and not the general public)?
Was it helpful?

Solution 2

After more research I came across the Squishit.S3 library which does exactly what I needed. Essentially it piggy-backs on squishit, enabling bundled files to be copied to an S3/Cloudfront bucket at runtime. Configuration is a breeze and because it uses Amazon's APIs, credentials are used to write to the CDN. If you already use Squishit it's just a matter of adding a couple of default configuration lines to your global.asax and the rest is taken care of for you

OTHER TIPS

I don't personally buy into the "USE CDN FOR EVERYTHING STATIC!!" mentality, so I refuse to worry about copying local scripts to a CDN as you described. Sure, the big libraries can be referenced from existing major CDNs (Yahoo, Microsoft, Google), but for local scripts, it's really not worth the hassle, IMO.

Following that line of thinking, I've grown very fond of SquishIt. There's no extra XML config or preinitialization necessary to use. Just include it in the master or layout file like so:

<%= Bundle.Css()
        .Add("~/Content/Reset.less")
        .Add("~/Content/Site.less")
        .Add("~/Scripts/rcarousel/widget/css/rcarousel.css")
        .Add("~/Scripts/jquery.fancybox-1.3.4/fancybox/jquery.fancybox-1.3.4.css")
        .Add("~/Content/Fonts/Karla/stylesheet.css")
        .Render("~/Cache/Bundle.css") %>

<%= Bundle.JavaScript()
        .Add("~/Scripts/jquery-1.7.2.js")
        .Add("~/Scripts/jquery-ui-1.8.19.js")
        .Add("~/Scripts/modernizr-2.5.3.js")
        .Add("~/Scripts/rcarousel/widget/lib/jquery.ui.rcarousel.js")
        .Add("~/Scripts/jquery.fancybox-1.3.4/fancybox/jquery.fancybox-1.3.4.js")
        .Add("~/Scripts/jquery.youtubelite.js")
        .Render("~/Cache/Bundle.js") %>

Having said that, and more to your point:

1) I'm not aware of any bundling libraries that support automatic CDN deployment. The usual train of thought here is to have the CDN pull from your website directory and cache it. In this way, deployment is established via a pull mechanism, rather than a push. This article describes how to set up origin pull using CloudFront with a word press site. I'm sure the configuration is similar for ASP.NET.

2) Bundle from local copies. You probably reference the local copies in development already, so why add the CDN into the mix prior to go-live?

3) Most cloud storage systems (Amazon S3, Azure Storage, Rackspace Cloud Files) offer a way to publish files to the cloud that remain read-only to the public. This is API-dependent, so the method varies depending on your cloud storage provider.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top