I have a few goals I'd like to achieve but I'm unsure of how to get there:
- Create a single-click deployment for my web project that includes a minified javascript file
- Version my minified JavaScript file to prevent browser caching of static content whenever a new build is pushed
- Reference the versioned JavaScript file on RELEASE builds, and the non-minified version of the JavaScript files with DEBUG builds
From this article:
http://encosia.com/automatically-minify-and-combine-javascript-in-visual-studio/
I've added JSMin to minify my files using a command like this:
"$(SolutionDir)Tools\jsmin.exe" < "$(ProjectDir)Scripts\myfile.js" > "$(ProjectDir)Scripts\myfile.min.js"
I'll also be adding this to my web pages for preserving non-minified files during debug mode:
<% if (HttpContext.Current.IsDebuggingEnabled) { %>
<script type="text/javascript" src="scripts\myfile.js"></script>
<% } else { %>
<script type="text/javascript" src="scripts/myfile.min.js"></script>
<% } %>
So really I'm left with trying to figure out how to prevent myfile.min.js being seen as static content by a web browser when it is updated. If my goal wasn't single-click deployment I could just add a version number manually, but that doesn't seem like a solid approach. Thoughts?
Rather than littering your source code with
if/else
all over the place, you might consider implementing anIHttpFilter
that does the minification on-the-fly. Something like what this article does should work nicely.Your javascript should be cacheable, and the overhead of minification on the webserver will be minimal. Implementing it as an
IHttpFilter
also means you don't have to add routes or change existing URLs, and you can add theIHttpFilter
conditionally only in your release configuration so you can still debug your javascript during development.As for versioning, an HTML helper (here's a link with some more info) would solve the problem nicely by tacking on the version number as a query string parameter. You could even go so far as to have your debug version of the
IHttpFilter
throw an exception if the version parameter doesn't exist, so you have something to remind you to use the HTTP helper whenever you add a new javascript file.