Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
West Wind WebSurge - Rest Client and Http Load Testing for Windows

ScriptManager and Resource Compression


:P
On this page:

I was putting on the finishing touches on a component I’m building for a customer which is using needs to be MS Ajax aware. Part of this was the post I made last week regarding dynamically figuring out whether to send ClientScript script and page insertions to the ScriptManager or the ClientScript manager dynamically. The primary reason for this is to ensure controls work properly when running inside UpdatePanels with MS Ajax. It all works fine.

 

One nice side benefit of using the ScriptManager to register WebResource urls is that you get automatic Resource Compression. Resource compression is rather useful and something that should be built into ASP.NET in the first place (actually it looks like this will be built in IIS 7 as the dynamic compression module, but I haven’t looked at this closer because it’s of limited use at the moment).

 

Anyway it looks like System.Web.Extensions has everything it needs to do the resource compression and I was hoping that when you call the Scriptmanager and its RegisterClientScriptResource it would automatically compress with code like this:

 

ScriptManager.RegisterClientScriptResource(this, this.GetType(), ControlResources.SCRIPTLIBRARY_SCRIPT_RESOURCE);

 

MS Ajax will automatically generate a call to ScriptResource.axd instead of WebResource.axd. ScriptResources are automatically compressed with GZIP.

 

This works fine as long a ScriptManager is on the page.

But even though ScriptManager.RegisterClientScriptResource is static, it only calls out to ScriptResource.axd when a ScriptManager is actually on the page so unless you explicitly put a ScriptManager on the page no resource compression takes place which is a bummer.

 

It also looks like Microsoft pulled out the WebResourceCompression module of the assembly so there’s no more generic compression mechanism which I thought was rather useful. Bummer...

Posted in ASP.NET  Microsoft AJAX  

The Voices of Reason


 

# DotNetSlackers: ScriptManager and Resource Compression


Bertrand Le Roy
December 18, 2006

# re: ScriptManager and Resource Compression

The compression module should be back in a while as a sample of sorts. The handler does more than just compression anyway.

Rick Strahl
December 18, 2006

# re: ScriptManager and Resource Compression

Bertrand - that's cool. When you say the handler does more - you mean ScriptResource.axd?

You know while you're mucking with ClientScript and Resource inclusion - how about trying to get the size of those resource Urls down to a more reasonable size <s>... It's OK for scripts, but for image links these huge URLs add up quickly especially if they're in some sort of list display...

Bertrand Le Roy
December 19, 2006

# re: ScriptManager and Resource Compression

Yes, the script resource handler is now also handling localization (it creates a JSON object from regular resx resources embedded in the assembly or its satellites and appends it to the script) and it's outputting the script notification statement at the end of the file. And it compresses and caches of course.
I agree that it would be nice to have shorter urls, but we have to code the assembly and resource name info into that. One thing we're trying to do is to always provide a way to reference static files instead, which enables the page developer to mitigate the problem at the cost of managing those files in the site.

Rick Strahl's Web Log
December 21, 2006

# UpdatePanels and ClientScript in custom Controls - Rick Strahl's Web Log

Due to changes in the MS Ajax Beta custom controls need to deal with the ClientScript object a bit differently if the control wants to be compatible with the MS Ajax UpdatePanel. It requires using the ScriptManager when available or using ClientScript when not.

stm
January 23, 2007

# re: ScriptManager and Resource Compression

Hi,

My script resources are not compressed by ScriptManager.RegisterClientScriptResource even when I have a ScriptManager on the page. I'm using the beta 2. Maybe I'm missing something?

Will Sahatdjian
June 06, 2007

# re: ScriptManager and Resource Compression

While it's great that the scripts are compressed, what about concatenation? Having multiple script tags on the page is going to slow page load down, perhaps more so than having uncompressed files.

Siderite
April 20, 2010

# re: ScriptManager and Resource Compression

I was profiling a web application and I noticed that a large hotspot for .NET code was coming from GZipStream, which is used by the script resource handler. I see from your post that the resource handler is using it and, since I am using a lot of controls, each with its own small script, I can only assume that it's from there. I know the pattern for this would be to place all the scripts in a single file and then register it, etc, but since I am not doing that at the moment, is there any way to <b>disable</b> the compression for the script handler module?

Rick Strahl
April 20, 2010

# re: ScriptManager and Resource Compression

@Siderite - with scriptmanager you don't get the option I don't think, although I do believe that the scripts are rendered in such a way that they should cache and not re-zip every time.

In 3.5 SP1 there are also script combining features available on script manager that might help.

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024