Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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?

What problems could arise with that second approach, and might they outweigh the benefit of smaller responses for the supporting browsers?

like image 384
Joel Micah Donovan Avatar asked Oct 24 '12 17:10

Joel Micah Donovan


People also ask

What is user agent in JavaScript?

The userAgent property returns the user-agent header sent by the browser to the server. The userAgent property is read-only. The value returned, contains information about the browser name, version and platform. The web specification suggests that browsers should provide as little header information as possible.

What is AppleWebKit 537.36 Khtml like Gecko used for?

AppleWebKit/537.36 indicates what browser rendering engine is used. A rendering engine is what transforms HTML into an interactive webpage on the user's screen. The WebKit browser engine was developed by Apple and is primarily used by Safari, Chromium, and all other WebKit-based browsers. (KHTML, like Gecko).

Is user agent same as browser?

A user agent is any software that retrieves and presents Web content for end users or is implemented using Web technologies. User agents include Web browsers, media players, and plug-ins that help in retrieving, rendering and interacting with Web content.

What is user agent server?

User Agent Server (UAS) is a Voice over Internet Protocol (VoIP) application that responds to User Agent Client (UAC) service requests based on input or other external stimuli in Session Initiation Protocol (SIP) systems.


2 Answers

You could load "optional" stuff on demand using RequireJS (or similar).

1) On Page load... test for feature with small tests (Modernizr)

2) If test succeeds, use native, if fails, use RequireJS to load your other resources

3) Profit.

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...

like image 75
BLSully Avatar answered Oct 19 '22 04:10

BLSully


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. This has the following advantages:

  1. Feature detection is much more accurate and forward compatible than browser detection, even with browsers you've never even seen before.
  2. You get one copy of your javascript for all browsers which is generally much easier to test and deply and requires no server-side distribution logic.
  3. Developing one common set of javascript that adapts to client conditions is generally much easier than developing N separate versions of site javascript.
like image 39
jfriend00 Avatar answered Oct 19 '22 06:10

jfriend00