views:

31

answers:

2

Hello,

I'm in the process of creating a build mechanism that will automatically minify and combine a certain set of JavaScripts.

The problem I'm facing is that to actually incorporate these web services, I need to get that content somehow.

I'm aware that it's tentatively possible using the WebClient class and invoking the actual URL, but that feels a bit overkill and would not work when we're triggering this mechanism on Application_Start in Global.asax.

First of all, is it even possible, and if so, and pointers on how I should approach this?

+1  A: 

Why not just create it on the fly?

Call the URL from the script tag and cache the output that you generate...

<script src="YourCodePage.aspx" type="text/javascript"></script>

You can then create the minified version on the fly the first time it is requested, but cache the response to make things quicker for other visitors to your site.

Sohnee
That would be an approach for sure, but could be hard to handle when you need dynamic javascript (e.g. minified versions for each subpage), the approach we'll be using will create all these specific minified versions on the fly, when a caching value is changed in web.config (which allows us to hot-patching javascripts without republishing).
peol
You could adjust your caching strategy depending on the request, so a static script would have a long cache, and something specific would have no cache. You would need to cache-bust the URI in the src attribute for those too if you want subsequent requests to re-get the script.
Sohnee
Exactly, but by combining them all, we generate unique combined (and minified) files whenever needed. If we need to hotfix a javascript, we could just upload the source file and edit a unique caching number in the web.config and the app would re-generate a new, unique combined file (which works flawlessly except for the dynamic *.asmx/js).
peol
+1  A: 

You have two ways:

  1. On-demand combining & minification (outlined by Sohnee) (also check this article: http://www.codeproject.com/KB/aspnet/CssAndJavaScriptOptimizer.aspx?display=Print)
  2. Build time - check this article: http://encosia.com/2009/05/20/automatically-minify-and-combine-javascript-in-visual-studio/

And see previous question on SO for more info: http://stackoverflow.com/questions/890561/concatenate-and-minify-javascript-on-the-fly-or-at-build-time-asp-net-mvc

VinayC
Thank you for the links. I will check them out but the approach we're using works great except for the web services files, which seems to create their content when accessed (e.g. when you add /js to the URL) - and that's really the only issue we're having at the moment.
peol
@peol, your combining/minifying logic can be in a method that gets called on Application_Start. It would place result files at configured location in file system. Then your actual handler (web services) could be written to look into that location.
VinayC
I'm not sure what you mean, the actual combine+minify stuff works, but not on asmx-files as they don't generate any javascript files (you have to request them specifically with ws.asmx/js), I wanted to get that output without having to actually request that file (i.e render the javascript programmatically).
peol