Javascript Best Practices and ASP.NET

ASP.NET 2.0 has a way of including script ClientScriptManager.RegisterClientScriptResource, which will cause a file that’s attached to the assembly as an embedded resource to be included as a linked <script> block, ie.

<script type="text/javascript"  src="/WebResource.axd?xxxxx" />

This is a pretty good way to manage scripts in your application since you can bundle up your scripts as resources on the assembly. Another thing that’s nice about this is that while the query string on WebResource.axd is totally impenetrable, it’s generated based on the source assembly’s name and timestamp, so you get some kind of automatic versioning mechanism.

By default, WebResource.axd outputs a Last-Modified headers and respects If-Modified-Since. For client side caching, WebResource.axd outputs an Expires headers and sets Cache-Control: private. The way I read the spec, this should allow caching at the browser, but the net effect in my testing seems to be that the browser doesn’t cache the linked script. So the caching support is halfway there – if you’re using RegisterClientScriptResource, ASP.NET will at least have to serve the request, even if it hasn’t changed. This seems like a missed opportunity, since embedded resources won’t change unless the assembly is recompiled, and the WebResource.axd URL should change when an assembly is recompiled. It really seems to me that Cache-Control: public would be a reasonable setting.

One other missed opportunity is in compression, both in minification and GZip/Deflate compression. For the latter, Rick Strahl has an interesting solution to GZip compression. You could do something similar for minification, or make minification part of your build system.

— Gordon Weakliem

---

Comment

Commenting is closed for this article.