使用Modernizr.load(yepnope.js)导致我的脚本被请求两次的原因是什么 [英] What is causing my scripts to be requested twice while using Modernizr.load (yepnope.js)

查看:182
本文介绍了使用Modernizr.load(yepnope.js)导致我的脚本被请求两次的原因是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 yepnope.js 动态加载javascript文件,我注意到我的脚本似乎是根据Firebug和Webkit Inspector加载两次。

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.

问题是在Firebug的Net面板(Firefox 4最新版)中,他们的响应是 200 ,而不是 304 。它似乎比在Chrome中慢。

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.

我上传了此视频显示了该问题。您可以看到文件 jquery-1.6.1.min.js libs.js 如何加载额外的时间。

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() yepnope()

推荐答案

在他们的文档中有关于此的说明:

There's a note about this in their documentation:

来自 http://yepnopejs.com/


我在dev
工具中看到两个请求,为什么它会加载
两次?

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

根据您的浏览器和
您的服务器,这可能意味着几个
不同的东西。由于
的性质,yepnope如何工作,每个文件都有两个
请求。
第一个请求是将资源
加载到缓存中,第二个请求
是执行它(但由于它在
缓存中,它应该执行
立即)。只要第二个
请求被缓存,看到两个请求就是
非常正常。如果您注意到
第二个请求未缓存(并且
您的脚本加载时间加倍),则
然后确保您发送
正确的缓存标头以允许
缓存脚本。对于yepnope来说,这是至关重要的
。如果没有启用
正确的缓存,它将无法工作。我们实际上是
测试,以确保在我们的测试套件中没有加载
两次,所以如果你
认为我们的
浏览器中有关于双重加载的错误,我们
鼓励你将测试套件运行到
,看看双重加载测试是否通过。

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.

这篇关于使用Modernizr.load(yepnope.js)导致我的脚本被请求两次的原因是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆