2

I’m hoping to investigate/implement a CDN (initially just via a sub-domain, though moving over to CDN in time) and am having a mare finding resources that talk about handling of versions of files on that sub-domain.

Most places I’ve worked previously have implemented caching of resources (images, javascript, css, etc.) and when wanting to change an image, have gone through the painful process of just changing the filename of the image, and changing the reference to it in the source code (so that customers see the new, not the cached image).

What I want to achieve

what I'd like is: resources.domain.com

with sub-folders such as:

  • scripts/
  • images/
  • css/
  • etc.

not a problem, and will help with the yslow/page speed scores (assuming cookieless domain etc.)

But versioning of assets is something I want to resolve.

E.g.

resources.domain.com/images/promo_banner1.jpg

I'd probably have to cache and expire perhaps every 10-15 days.

Assuming we have something key come in as a business request, and we need to change it, I want to be able to override that. From what I understand, I could append a querystring (?1.1) to it to force browsers to see it as a different resource.

I know I can do this in MVC (or indeed ASP.NET) by creating a 'CompanyResource' html helper that will lookup against perhaps a resource file or something similar to see if we have a new version, and if so, append the version number as a querystring element, but there has to be a better way?

So, what has the community come up with?

  • how best to deal with resources in a sub domain (assume I've read all of the yslow/google backup docs around this)
  • what have folks come up with to handle versioning of assets (to minimise overall code changes when something updates) - code based helper methods to deliver assets based upon some rules?

Hopefully I haven't waffled too much.

Thanks for any and all help :)

Cheers, Terry

Terry_Brown
  • 1,408
  • 10
  • 24
  • Not sure if this is supported by MVC, but with Webforms you can use the .skin file and then SkinID your files. This requires no code changes in the app or html changes. However, the app would need to be recycled when the changes were made. – mxmissile Jan 31 '10 at 10:57
  • I thought skin files only worked with server controls? Want to avoid them really if so, like having full control over markup. Just noticed that SO uses a ?v=999 type url on their resources, so I'd best look into any SO podcasts/documentation I can to see if there's anything in there :) – Terry_Brown Jan 31 '10 at 11:11
  • Yea, that's probably your best bet. With Webforms/Server controls not an option. – mxmissile Jan 31 '10 at 15:36
  • visit this ... http://mvccdn.codeplex.com – vrluckyin Feb 16 '11 at 01:10

1 Answers1

3

just noticed this one hadn't been answered - we resovled our problem with Html helpers under ASP.NET MVC using the following.

In web.config we stored the 'resources_url' (in this case resources.mycompany.co.uk), and called the following:

<%= Html.MyCompanyResourceScript("~/scripts/jquery-1.4.2.min.js") %>

Which translated to an Html helper:

public static string MycompanyResourceScript(this HtmlHelper helper, string url)
{
    string _out = String.Format(@"<script type=""text/javascript"" src=""{0}""></script>", url.ToMyCompanyUrlAction(helper.ViewContext.HttpContext));
    return _out;
}
public static string ToMyCompanyUrlAction(this string url, HttpContextBase context)
{
return String.Format("{0}://{1}{2}", 
    (context.Request.IsSecureConnection ? "https" : "http"),
    WebConfigurationManager.AppSettings["resources_url"],
    VirtualPathUtility.ToAbsolute(url, "/"));
}

We created helpers for MyCompanyResourceImage and MyCompanyResourceCss with suitable parameters for alt tags/media types etc. also.

This means we can (from the outset) host via our own resources domain, and if in future we choose to move that over to a CDN, then we can do so with minimal fuss.

Hope that helps someone.

Cheers, Terry

Terry_Brown
  • 1,408
  • 10
  • 24
  • I am working on something similar right now. I don't want every file compiled into a single file always. And I only want to force a re-cache of files that have a new version (since many JS library files might not have been altered) and push those to the CDN. – Ian Patrick Hughes Nov 03 '11 at 02:52
  • @Terry How did you solve your versioning issue with this solution? – Rich C Mar 29 '16 at 19:50
  • Hey @RichC we still aren't ideal with it, but in our web.config we have 3 app settings: version_scripts, version_images, version_css. The one we change most regularly is version_css - it's done manually still by the developer if they update CSS, though it could be automated based on checksums etc. we then incorporate that version into the URL string (https://resourcesurl/css/v1234/my.css) and use URL rewriting to cancel out the v1234 part. Works a treat, though is convoluted. The helper class above just picks that up though easily enough and incorporates it into the URL automatically. – Terry_Brown Mar 30 '16 at 10:12