What is causing my scripts to be requested twice while using Modernizr.load (yepnope.js)
Asked Answered
R

1

20

I am using yepnope.js to load javascript files dynamically,and I've noticed that my scripts appear to be loaded twice according to Firebug and Webkit Inspector.

The problem is that in Firebug's Net panel (Firefox 4 latest), their response is a 200, not a 304. It seems to be slower than in Chrome.

I have uploaded this video showing the issue. You can see how the files jquery-1.6.1.min.js and libs.js are loaded an extra time.

The code I am using to do this is the following, simplified:

Modernizr.load({
                load: ['jquery-1.6.1.min.js', 'libs.js'],
                complete: function () {
                    console.log("loaded");
                }
});

Modernizr.load() is yepnope().

Roice answered 19/6, 2011 at 1:51 Comment(1)
@HappyDeveloper Have a look at yepnopejs' site - it also does the same. Perhaps it is how it works. But, it appears to download twice?Roice
R
25

There's a note about this in their documentation:

From http://yepnopejs.com/

I'm seeing two requests in my dev tools, why is it loading everything twice?

Depending on your browser and your server this could mean a couple different things. Due to the nature of how yepnope works, there are two requests made for every file. The first request is to load the resource into the cache and the second request is to execute it (but since it's in the cache, it should execute immediately). Seeing two requests is pretty normal as long as the second request is cached. If you notice that the second request isn't cached (and your script load times are doubling), then make sure you are sending the correct cache headers to allow the caching of your scripts. This is vital to yepnope. It will not work without proper caching enabled. We actually test to make sure things aren't loaded twice in our test suite, so if you think we may have a bug in your browser regarding double loading, we encourage you to run the test suite to see if the double loading test passes.

Resignation answered 20/6, 2011 at 5:53 Comment(3)
I realised this afterwards, forgive my ignorance, and thank you for your answer.Roice
I still don't understand why they had to make it this way. Facebook's bootloader thing doesn't have such issue with loading the file twice..Acriflavine
What's the best 'drop-in' replacement if 2 requests in the network activity is a problem?Morril

© 2022 - 2024 — McMap. All rights reserved.