Linking to libraries? Better think again.

If you are linking to libraries, you might want to think again.

 

http://qz.com/646467/how-one-programmer-broke-the-internet-by-deleting-a-tiny-piece-of-code/

Geeks, making the world a better place
Quote · 6 May 2016

Best practices would be to host libraries locally and setup a cron job to automatically download/update the libraries every few hours. 

 

Helps your page speed dramatically and you wont have downtime / other loading issues with the hosted libraries.

 

Pretty easy:

1. make php file to download remote files

2. make cron job

3. update script to locally reference libraries

 

PHP File (analytics-cron.php)


<?php
// Remote file to download
$remoteFile = 'https://www.google-analytics.com/analytics.js'; //CHANGE THIS
$localfile = '/path-to-www/etc/lib/analytics.js';  //CHANGE THIS
//For Cpanel it will be /home/USERNAME/public_html/ga.js

// Connection time out
$connTimeout = 10;
$url = parse_url($remoteFile);
$host = $url['host'];
$path = isset($url['path']) ? $url['path'] : '/';

if (isset($url['query'])) {
  $path .= '?' . $url['query'];
}

$port = isset($url['port']) ? $url['port'] : '80';
$fp = @fsockopen($host, '80', $errno, $errstr, $connTimeout );
if(!$fp){
  // On connection failure return the cached file (if it exist)
  if(file_exists($localfile)){
    readfile($localfile);
  }
} else {
  // Send the header information
  $header = "GET $path HTTP/1.0\r\n";
  $header .= "Host: $host\r\n";
  $header .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6\r\n";
  $header .= "Accept: */*\r\n";
  $header .= "Accept-Language: en-us,en;q=0.5\r\n";
  $header .= "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n";
  $header .= "Keep-Alive: 300\r\n";
  $header .= "Connection: keep-alive\r\n";
  $header .= "Referer: http://$host\r\n\r\n";
  fputs($fp, $header);
  $response = '';

  // Get the response from the remote server
  while($line = fread($fp, 4096)){
    $response .= $line;
  }

  // Close the connection
  fclose( $fp );

  // Remove the headers
  $pos = strpos($response, "\r\n\r\n");
  $response = substr($response, $pos + 4);

  // Return the processed response
  echo $response;

  // Save the response to the local file
  if(!file_exists($localfile)){
    // Try to create the file, if doesn't exist
    fopen($localfile, 'w');
  }

  if(is_writable($localfile)) {
    if($fp = fopen($localfile, 'w')){
      fwrite($fp, $response);
      fclose($fp);
    }
  }
}
?>

 

Cron job be like... (ubuntu 14.04)


* */2 * * * /usr/bin/php /path-to-www/etc/cron/analytics-cron.php &> /dev/null

Quote · 7 May 2016

The thing mentioned by OP has nothing to do with serving asset locally or from a cdn. Its just some npm related stuff which dolphin doesn't use so its not a big issue here I assume.

so much to do....
Quote · 8 May 2016

Hi Arkane. This is really interesting information. One of the things which I never considered.

When running various sites against Pagespeed, Yslow, etc one of the things which takes a long time to load is the google analytics js. Below you've just provided a way for us to host it locally, which really makes sense (to me) since that script isn't there for the client, but really for the server.

Loading it locally will mean less DNS requests on the server side, resulting in quicker page loading times for the client. Hosting js scripts like this on CDN's also don't make sense, since the script needs to be loaded locally in this particular case.

Would you recommend doing this with other scripts and if so, which ones?

 

Best practices would be to host libraries locally and setup a cron job to automatically download/update the libraries every few hours. 

Helps your page speed dramatically and you wont have downtime / other loading issues with the hosted libraries.

 

Quote · 8 May 2016

Hi Prashank. Chat+ (used by Dolphin for the new non-flash chat) uses npm, so it's possible that could have broken.

 

The thing mentioned by OP has nothing to do with serving asset locally or from a cdn. Its just some npm related stuff which dolphin doesn't use so its not a big issue here I assume.

 

Quote · 8 May 2016

 

Hi Prashank. Chat+ (used by Dolphin for the new non-flash chat) uses npm, so it's possible that could have broken.

 

The thing mentioned by OP has nothing to do with serving asset locally or from a cdn. Its just some npm related stuff which dolphin doesn't use so its not a big issue here I assume.

 

 Oh...yea i remember now.

so much to do....
Quote · 8 May 2016

 

Would you recommend doing this with other scripts and if so, which ones?

really anything that pulls from an outside server... google, facebook, etc...

in some cases (like copy paste html widgets) the asset references other assets as they were local files (like: /path/file.xyz) and not with a full domain (like: http:// somesite.com/path/file.xyz)... in these rare cases it will not work to have them local... (i only ran into one i used, just found something else to replace it.)

anyhow... I'm going to do a write up with the files you need to make this work.

bottom line... page speed is very important, page speed and best practices affects your ranking... google actually recommends you run it this way. if you view the source of popular websites you'll see google analytics js is locally referenced.

Quote · 8 May 2016
 
 
Below is the legacy version of the Boonex site, maintained for Dolphin.Pro 7.x support.
The new Dolphin solution is powered by UNA Community Management System.