Compress external JS files

3

In PageSpeed of Google, some warnings that I sometimes receive, is to enable compression of JS and CSS files. But since I work with external APIs and scripts, sometimes I can not do this.

  • Would you like to get around this "problem" somehow?

Compressing the external file, or, within the call of it in my site.

  • Do something to make it "lighter"?
asked by anonymous 27.11.2017 / 13:09

1 answer

1

You can use PHP-JS-CSS-Minifier , it is a PHP library that compresses javascript and css files in real time and saves it to a project folder.

Based on the Javascript Minifier site API. Just include the codes as in the example:

include_once("minifier.php");

$js = array(
    "js/application.js"     => "js/application.min.js",
    "js/main.js"            => "js/main.min.js"
);

$css = array(
    "css/application.css"   => "css/application.min.css",
    "css/main.css"          => "css/main.min.css"
);

minifyJS($js);
minifyCSS($css);

I tested with the jQuery plugin through their url https://code.jquery.com/jquery-3.2.1.js and worked as expected:

include_once("minifier.php");

$js = array(
    "https://code.jquery.com/jquery-3.2.1.js"   => "js/jquery-3.2.1.min.js"
);

minifyJS($js);

The disadvantage is that since the compression is done in real time, the request becomes slow and ends up not being an advantage to place directly in the body of the page of your site, since every time it will perform this compression. One output would be to insert a if before starting the compression and compare the date of the two files to check if your script is up to date.

    
27.11.2017 / 14:20