[英]Serving JavaScript based on User-Agent
I am curious about the pros and cons of using user agent detection in a web server to determine which version of a JavaScript resource to send to the client. 我对在Web服务器中使用用户代理检测来确定要发送到客户端的JavaScript资源版本的优缺点感到好奇。
In particular, if some web browsers support a feature natively while others require a verbose JavaScript workaround, is it better to serve the workaround to everyone and run it only if it's needed client-side, or to serve the workaround only to the browsers that require it and send a thin wrapper around native features to the rest? 特别是,如果某些Web浏览器本机支持某个功能,而另一些Web浏览器则需要冗长的JavaScript解决方法,那么最好将解决方法提供给所有人并仅在需要客户端时运行它,或者仅将解决方法提供给需要此功能的浏览器并围绕其他本地功能发送薄包装纸?
What problems could arise with that second approach, and might they outweigh the benefit of smaller responses for the supporting browsers? 第二种方法可能会出现什么问题,它们是否可能超过支持浏览器的较小响应所带来的好处?
You could load "optional" stuff on demand using RequireJS
(or similar). 您可以使用RequireJS
(或类似方法)按需加载“可选”的东西。
1) On Page load... test for feature with small tests (Modernizr) 1)页面加载...使用小型测试测试功能(Modernizr)
2) If test succeeds, use native, if fails, use RequireJS to load your other resources 2)如果测试成功,请使用本机,如果失败,请使用RequireJS加载其他资源
3) Profit. 3)利润。
This does assume you don't mind extra http requests....too many of these test, load, repeat processes can slow things down more than just including one large(r) file, so it's case dependent, but there is definitely reasonable middle ground... 这的确假设您不介意额外的http请求。...太多的这些测试,加载,重复过程不仅会使包含一个large(r)文件放慢脚步,而且视情况而定,但是这确实合理中间地...
Usually, it is a better solution to send one copy of your javascript to all clients and have the javascript itself do feature detection to decide how to best treat each browser. 通常,将javascript的一个副本发送给所有客户端并让javascript本身进行功能检测以决定如何最好地对待每个浏览器是一个更好的解决方案。 This has the following advantages: 这具有以下优点:
This in neither Pro or Con, but speaking from SEO point of view, you should consider that Googlebot will always see the "workaround" version. 无论是Pro还是Con,无论是从SEO角度来讲,您都应该考虑到Googlebot总是会看到“解决方法”版本。 (which I`m assuming is the default one, when no User-Agent was recognized) (我假设这是默认选项,当时未识别出User-Agent)
I`m saying that here because I saw several sites take a dive, when implementing user-agent/cookie based custom JS rules. 我之所以这么说是因为在实现基于用户代理/ Cookie的自定义JS规则时,我看到了几个站点。
Going back to your original question, I would suggest using a single version approach - simply because its much more manageable and does not require you to keep track of several versions of the script. 回到您最初的问题,我建议使用单一版本方法-仅仅是因为它更易于管理,并且不需要您跟踪脚本的多个版本。
@BLSully also raised an excellent point here (+1) about the extra HTTP requests this will cause. @BLSully在这里(+1)还提出了一个很好的观点,指出这将导致额外的HTTP请求。 Chances are that your overall site speed will plummet or any gains will be greatly diminished. 您的整体站点速度可能会直线下降,或者任何收益都会大大减少。
There are many better things you could do for acceleration - if this is indeed your goal here... 您可以为加速做很多更好的事情-如果这确实是您的目标...
Another possibility (without additional http request) is to use user agent header for sending different versions of content. 另一种可能性(无其他http请求)是使用用户代理标头发送不同版本的内容。 Have a look at device atlas article on this topic or have a look at referenced article about usage of this technique in wild . 请参阅有关此主题的设备图集文章 ,或参阅有关在野外使用此技术的参考文章。
I see as pros: 我认为是优点:
Cons: 缺点:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.