Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jason Espin 368 posts 1335 karma points
    Apr 09, 2014 @ 13:05
    Jason Espin
    0

    Versioning CSS and Javascript files automatically in Umbraco 7

    I'm new to both .NET development and Umbraco due to recently starting a new role where I will be working heavily with both. As usual before starting development I am looking for some shortcut or helper functions that will enable me to work more productively. One set of helper functions I have found I have included below:

    public static string DatedContent(this UrlHelper urlHelper, string contentPath)
    {
      var datedPath = new StringBuilder(contentPath);
      datedPath.AppendFormat("{0}m={1}",
          contentPath.IndexOf('?') >= 0 ? '&' : '?',
          getModifiedDate(contentPath));
          return urlHelper.Content(datedPath.ToString());
    }
    
    private static string getModifiedDate(string contentPath)
    {
        return System.IO.File.GetLastWriteTime(HostingEnvironment.MapPath(contentPath)).ToString("yyyyMMddhhmmss");
    }
    

    The functions above are called as follows:

    <script src="@Url.DatedContent("~/scripts/product.js")"></script>
    

    The aim of these functions is to automatically version Javascript and CSS files to ensure that the latest versions of each JavaScript and CSS file are pulled through by appending a modified date query string to the end of the file references.

    My issue here is, and I apologise that this is such a newbie question, but where do I put this function in Umbraco 7 or is this functionality already supported?

    From what I have learnt so far, content pages all have some base properties that include date created and date modified however CSS and Script files do not have these properties so I cannot simply reference these object properties using inline Razor code and condense them with the ToString method as I am doing the functions above.

    My first thought was that these functions could be included at the top of the template where the razor code block is included automatically but as I am new to .Net I don't know if this is a standard convention.

    Any help would be greatly appreciated.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Apr 09, 2014 @ 15:57
    Jan Skovgaard
    0

    Hi Jason and welcome to our :)

    I don't have the answer to your question but I'm wondering if you know about the Optimus package? If not try checking it out and see if it does what you want perhaps? http://our.umbraco.org/projects/developer-tools/optimus

    Also the source code is available here if you want to make a fork https://github.com/TimGeyssens/BundlingAndMinificationForTheMasses

    /Jan

  • Jason Espin 368 posts 1335 karma points
    Apr 09, 2014 @ 16:38
    Jason Espin
    0

    Hi Jan

    Thanks for the suggestion. I will look into this but as I'm not too familiar with ASP just yet I was really looking for an indication of whether helper functions such as those shown above can generally be included within an Umbraco solution without too much hassle or whether this is frowned upon in favour of using some of the already built in functionality of Umbraco. I'll definitely look into Optimus in my own time on my own machine outside of the office though. Loving the name :)

Please Sign in or register to post replies

Write your reply to:

Draft