Skip to content

Instantly share code, notes, and snippets.

@hughbris
Last active August 29, 2015 14:21
Show Gist options
  • Save hughbris/761f4135dbd5a605de59 to your computer and use it in GitHub Desktop.
Save hughbris/761f4135dbd5a605de59 to your computer and use it in GitHub Desktop.
Pattern for testing development Javascript in production environments

This is nothing but a simple conditional pattern for situations where you find yourself needing to test Javascript development code in a live operational server environment. Yes, this indicates less than ideal configuration management, but it has happened to me more than once. Reasons I can think of are:

  • stoopid (quite often)
  • need to authenticate against APIs, some of which can be difficult or impossible to set up in non-production environments

To make it work, you just have to set a custom User Agent prefix in your testing browser and change the string at the top of the file to match it. Customising User Agent strings varies between browsers but there are usually plugins available if it is not in the configuration options (for example, User Agent Switcher for Firefox).

Of course, syntax errors are going to kill the whole deal … :(

var uaDevPrefix = 'foobar UA: ';
if( navigator['userAgent'].indexOf(uaDevPrefix) == 0 ) {
// unstable development code your live users and PHBs won't execute
}
else {
// the stable code, everyone else's browser will run this
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment