javascript – Compress external JS files

Question:

On Google's PageSpeed , some warnings I sometimes get is to activate the compression of JS and CSS files. But since I work with external APIs and scripts, sometimes there's no way to do this.

  • Would you be able to get around this "problem" somehow?

Compressing the external file, or, within its call on my site.

  • do something to make it "lighter"?

Answer:

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

It uses as base the Javascript Minifier website 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 it with the jQuery plugin through their url https://code.jquery.com/jquery-3.2.1.js and it 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, as the compression is done in real time, the request becomes slow and it ends up not being an advantage to place it directly in the body of your website's page, since every time it will perform this compression. One way out would be to insert an if before starting the compression and compare the date of the two files to check if your script is up to date.

Scroll to Top
AllEscort