0

What is given:

  1. nginx version: nginx/1.9.3;
  2. /var/www/site/js/site.chrome.js - code specific for chrome;
  3. /var/www/site/js/site.firefox.js - code specific for firefox;
  4. site.chrome.js and site.firefox.js are selected on the browser side depending on which browser runs the code;
  5. UA: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36

The problem: Instead of specifying in the browser which if the two JS files are to be loaded I would like to offload this task to the nginx server so that

  1. the browser just points to an URL like http://server.com/js/site.js
  2. the nginx parsers the UA, extracts OS, browser name, and its version and looks for the site.js in the following directory structure:
js
 |
 +-site.js
 |
 +-linux
     |
     +-chrome
     |   |
     |   +-site.js
     |   |
     |   +-44
     |   |  |
     |   |  +-site.js
The js/site.js should be considered a fallback which means that if the nginx fail to find an appropriate site.js down the tree it uses the js/site.js file.

Given the aforementioned UA the nginx should look for the site.js in the following steps:

  1. js/linux/chrome/44/site.js
  2. js/linux/chrome/site.js
  3. js/linux/site.js
  4. js/site.js

The questions are:

  1. Does this way of organizing browser specific files in the directory tree at all has any sense, and if so...
  2. Can this approach be implemented with the nginx server;
  • 1
    Really bad idea. Especially since browsers tend to strip down or fake UAs. Much better to write scripts/css/whatever that will work for any browser using feature detecting. – Alexey Ten Aug 20 '15 at 12:35
  • OK, got it. Anyway, I am not trying to feature-detect. Working with youtube video player, I found out that under firefox and chrome player's event workflows are slightly different. I would like to have just two separate JS files loaded for each browser and OS. – Dmitry N. Medvedev Aug 20 '15 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.