Dev Ops

How to configure Nginx to serve statics from Amazon S3

Recently I needed to put a Nginx on front of a Amazon S3 bucket to serve statics files from a own subdomain and this is how I did it. Continue reading

Standard
Web development

How to beautify a JSON

JavaScript-logo
Some time ago I need beautify a JSON in a textarea and I found that JSON.stringify() provides a parameter to specify the String to use as a space, so you can set a tab or number of spaces at each JSON level.

Let’s see:

// This insert tabs at each level
JSON.stringify(jsonObject, null, "\t");
 
// This put two spaces at each level
JSON.stringify(jsonObject, null, 2);

Read more JSON.stringify()

Standard
Web development

How to failover to a local file if a CDN fails with RequireJS

requirejs-logo

 

To use a CDN is recommended for speed and a better performance but for example, what happens if a CDN fails or you want to enable offline mode?

If you are using RequireJS there is a way to do this, let’s see a simple example.

Failover system

Just add another route to the same object

requirejs.config({
  enforceDefine: true,
  paths: {
    jquery: [
      '//ajax.aspnetcdn.com/ajax/jquery/jquery-2.0.0.min',
      'js/jquery-2.0.0.min' 
    ] 
  } 
});

So if the first route fails, RequireJS will try to load the second one (and so on…).

Using it

There is no change about handling the variables

require(['jquery'], function($) {
  // Use $ normally...
});

Read more about config fallbacks
More information RequireJS API

Standard