Skip to content

Instantly share code, notes, and snippets.

@bgerrissen
Created February 8, 2011 16:50
Show Gist options
  • Save bgerrissen/816731 to your computer and use it in GitHub Desktop.
Save bgerrissen/816731 to your computer and use it in GitHub Desktop.
UA/Feature detection Service

If UA (server side) is unknown, return feature detection kit that sets features object client side and sends results back to server so the server can cache it.

Any consequtive request for that specific UA will return a much smaller server cached features object script.

Lets say (theoretically) the full feature test script is 50kb and a cached feature set script 10kb. The first ever request for an unique UA will be a hit for 50kb and every other request after that (for that specific UA) for just 10kb.

For a high traffic website this can offer substantial savings.

Also, after saving say 20 unique UA feature tests, the service will also become rather static as 80 to 90% of the audience is covered. No biggy, the service will be useful still to feature test future browsers.

Now one can set something like this up on their own servers, but the biggest gain would be indeed if Yahoo, Google or Microsoft would offer this. Combining this with domain specific browser caching is a win win.

<script src="features.js"></script> 

// sends UA string to service (http header)
// service checks cached results
// returns cached result if present
// returns feature tests if no cached result

// example feature.js

(function(){

    // features will be set without tests when retreived from service cache
    var features = {};

    // all code below will not be present when using a cached result set.

    // tests here

    // send results to server for caching
    var cacheIt = document.createElement("script");

    // need to find a good way to encode and decode results into a computer readable string.
    cacheIt.src = "store-features?auth=KEY&features=ENCODEDSTRING";

    // UA string available in http headers
    document.getElementsByTagName("head").appendChild(cacheIt);

    // set classnames here?

}());

auth=key is embedded by service and the store-features service only accepts keys that it issued. each key is usable only once.

feature detection results get cached by service and are tied to a UA string. So this is NOT UA sniffing, but binding feature sets to UA strings.

@bgerrissen
Copy link
Author

Note: browser cache is not a factor, however... what about IE8 with IE7 mode?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment