7

I've implemented the StackExchange MiniProfiler on a ASP.NET WebForms page which already references v1.7.1 of jQuery. The jQuery file is hosted locally, so my masterfile looks like this:

<script src="Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<%= MiniProfiler.RenderIncludes() %>

However, when I view the source of the output generated, I get something similar to this

<script src="Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">    
    ...
    load('/app/mini-profiler-resources/jquery.1.7.1.js?v=tNlJPuyuHLy/d5LQjyDuRbWKa0weCpmO3xkO6MH4TtA=', initMp);
</script>

Looking in the dev tools network tab, I can see that it's putting two requests in, one for Scripts/jquery-1.7.1.min.js and another for /app/mini-profiler-resources/jquery.1.7.1.js

Isn't this a fairly major redundancy issue? How do I stop the MiniProfiler includes from requesting, downloading and parsing another copy of the jQuery library?

growse
  • 3,554
  • 9
  • 43
  • 66

2 Answers2

10

The reason is that we are loading jQuery in noConflict. This eliminates the risk of having jQuery versions clash. For example, we are not sure MiniProfiler will still work if jQuery version 1.0 is on the page. To avoid any risk, we load our own version.

Now, I am open to a pull request that does a version check on jQuery prior to requesting it, if the "right" version is there we can simply alias jQueryMP to jQuery. However, this complicates the code and only solves the issue for a single version of jQuery.

Sam Saffron
  • 128,308
  • 78
  • 326
  • 506
  • 2
    Thanks for the explanation. Would it be worth including a default `bool=true` argument to the `RenderIncludes()` method to select the loading of jQuery? Users who then knew that they were loading the correct version elsewhere on the page could then call `RenderIncludes(false)`. Happy to do the legwork if you think this is a viable approach. – growse Apr 12 '12 at 07:40
  • 3
    fine for a patch, perhaps RenderIncludes(useExistingjQuery: true) - this in turn can generate `jQueryMP = jQuery;` instead of loading it – Sam Saffron Apr 12 '12 at 08:28
  • 1
    In the latest version, do this: StackExchange.Profiling.MiniProfiler.Settings.UseExistingjQuery = true; – pbz Dec 29 '12 at 12:23
  • 1
    @Sam, I like MiniProfiler load its own jQuery because I am not using it since I use ExtJs. However I found the one of the script using `jQuery` (which does not exist) instead of `MiniProfiler.jQuery`. It is in `/mini-profiler-resources/includes.js?v=...` that contain `(...)(jQuery)`. I am not sure why but it seems trying to mixed both jQuery'es. – CallMeLaNN Jun 09 '13 at 16:59
0

since they have different url, those are different resources for the browser. Maybe you just don't need the 1st script that you added.

or try this hack:

<script src="/app/mini-profiler-resources/jquery.1.7.1.js?v=tNlJPuyuHLy/d5LQjyDuRbWKa0weCpmO3xkO6MH4TtA="></script>
Gavriel
  • 18,880
  • 12
  • 68
  • 105
  • I'm not asking why the browser is loading both scripts, I'm asking why the auto-generated code from the MiniProfiler is deciding to load a script which has already been loaded, and how I can turn that off. – growse Apr 11 '12 at 11:53
  • read my second sentence: remove: – Gavriel Apr 11 '12 at 11:58
  • 1
    And then scripts that rely on jQuery won't execute if the MiniProfiler isn't rendered? – growse Apr 11 '12 at 12:06