ToolkitScriptManager: there's a new ScriptManager in town

Published Tue, Jun 12 2007 15:22

Besides fixing some bugs and adding some new features to the existing controls, the new release of the AJAX Toolkit introduces the ToolkitScriptManager control. This new control lets you combine several embedded scripts on a single download, which might improve the performance of your page in some scenarios.

Its usage is simple: you add the ScriptCombineAttribute attribute to the assembly that contains the JS files you want to be combined and add a ToolkitScriptManager control to the page (making sure that the CombineScripts property of the control is set to true - which is the default).

When you apply the attribute to an assembly, all the scripts embedded on the dll that are need for a specific page will be combined in a single download. You can also specify which files should and shouldn't be combined by setting the ExcludeScripts and IncludeScripts properties (as you can guess by their names, the IncludeScripts property lets you specify which scripts should be combined or excluded from the single download file that is generated by the control).

There are some interesting side effects associated with this new model. The first thing i've noticed is that the usage of the WebResourceAttribute is no longer necessary. The only thing i needed to do to download the javascript code was to embed the file on the dll. To me this isn't a big issue. Others might disagree.

Another difference when you think about loading js files: you no longer need to call the Sys.Application.notifyScriptLoaded method at the end of the js files that are combined into a single download.The control automatically calls that method for you, so there's nothing to worry about.

To me, the control's greatest advantage is that  you'll automatically get a single download which is compressed (using the GZIP or Deflate algorithm) and cached by the browser. The internal implementation of the control is also interesting. The control inserts a script element whose generated url points to the current page and uses a query parameter (named _scriptcombiner_) to specify the list of scripts that should be combined into a single one. During the Init event, the control checks the value of that query string parameter. If it's not null, it'll generate a custom response that inserts the requested js files on the page and end the current request.

[Update: Delay has a great post which explains in great depth how this new control works. If you want to know everything about it, go read it now :)]

Filed under: ,

Comments

# Delay said on Tuesday, June 12, 2007 4:17 PM

Luis, Great write-up! If you're interested, please have a look at my blog for a little more detail on how ToolkitScriptManager works - and why. :) * http://blogs.msdn.com/delay/archive/2007/06/11/script-combining-made-easy-overview-of-the-ajax-control-toolkit-s-toolkitscriptmanager.aspx. I've just created a work item for the WebResourceAttribute issue you mention - thanks for pointing that out. * http://www.codeplex.com/AtlasControlToolkit/WorkItem/View.aspx?WorkItemId=11023 Your comment about notifyScriptLoaded is good - though you'll notice that none of the AJAX Control Toolkit scripts called that method because ScriptResource.axd adds it automatically for any scripts it serves. Thanks!

# Bertrand Le Roy said on Tuesday, June 12, 2007 4:47 PM

Minor remark: if you use the regular script manager, you don't need to add the notification line either.

# luisabreu said on Tuesday, June 12, 2007 4:52 PM

howdy.

thanks.

yeah, you're right about extenders and scriptresource. and if i'm not mistaken, you can also add that line automatically when loading a script by using the notifyscriptloaded property of the scriptreference object.

however, i  thought it was best to mention this because there's always the chance of having a dll with  embedded js files and you might be using a scriptelement to add a reference to that js file.

maybe there should be a recommendation for not adding the notifyScriptLoaded method to the end of the file (making us use the notifyscriptloaded property of the scriptreference element).

btw, a question: isn't there a possibility of running out of chars on the query string when you have several js files and several assemblies?

# luisabreu said on Tuesday, June 12, 2007 4:55 PM

Yes, Bertrand. As always, you're correct. :)

# Delay said on Tuesday, June 12, 2007 5:11 PM

Luis, Yes, the length of the combined script URL is something we'd like to decrease. I mention it briefly in my blog, but hadn't gotten around to opening a work item until earlier today. (Just a bit before you asked, in fact!) http://www.codeplex.com/AtlasControlToolkit/WorkItem/View.aspx?WorkItemId=11025

# luisabreu said on Tuesday, June 12, 2007 5:46 PM

Delay: the query string length was something i forgot to mention in my initial post. compressing it looks good to me :)

# Delay said on Wednesday, June 20, 2007 5:26 PM

FYI, as of the 10618 release of the AJAX Control Toolkit, WebResourceAttribute is now required and the combined script URLs are much shorter. Additionally, there's a new CombinedScriptsHandlerUrl property that's pretty handy:

blogs.msdn.com/.../script-combining-made-better-overview-of-improvements-to-the-ajax-control-toolkit-s-toolkitscriptmanager.aspx

# luisabreu said on Thursday, June 21, 2007 3:28 AM

Hello.

yes, I've been told that there are improvements, but still wasn't able to download and test the latest version.

# LA.NET [EN] said on Thursday, June 21, 2007 3:40 AM

And it seems like it solves some of the issues i've mentioned before and brings improves to other

# Trent said on Wednesday, February 06, 2008 3:40 PM

Has anybody noticed the increased PreRenderComplete times when using this control?  My best guess is that there is some expensive operation during this method when trying to combine scripts.  The implications were significant within our app.  Script combination only saved a *fraction* of total download size, where I gained .7 seconds on the server for rendering this script.  Definetly a deal breaker.

# ASPInsiders said on Monday, May 12, 2008 4:02 PM

You can get them from here (VS) and here (.NET). I'm reading what's new on Brad Abrams post and

# Stephen said on Wednesday, November 05, 2008 4:27 AM

When applying the script do you add in for all pages or just for the ones that you want to go faster?

Leave a Comment

(required) 
(required) 
(optional)
(required)