2

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
  • As a temporary solution, we thread the minifier/combination tool on Application_Start in Global.asax, and starts that thread when the first request has ended, allowing us to request the web services through xxx/ws.asmx/js and then minify+combine that output. Commented Oct 23, 2010 at 20:45

2 Answers 2

1

You have two ways:

  1. On-demand combining & minification (outlined by Sohnee) (also check this article.)
  2. Build time - check this article.

And see previous question on SO for more info:
Concatenate and minify JavaScript on the fly OR at build time - ASP.NET MVC

Sign up to request clarification or add additional context in comments.

3 Comments

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, 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.
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).
1

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.

3 Comments

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).
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.
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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.