Compress external JS files

Asked

Viewed 801 times

3

In the PageSpeed Google, some warnings I sometimes get, is to enable compression of JS and CSS files. But because I work with Apis and external scripts, sometimes I can’t do that.

  • There would be a way around this "problem" somehow?

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

  • do something to make him "lighter"?
  • Why can’t you drop them and leave on the project?

  • Because for example, updates, if I download, I will have to download every time it is updated, another thing is that, in this project that I am, I do not have access to the source code, I can only edit html, css, javascript, etc...

  • I get it. I don’t know (and I believe) that you don’t have to compress external js resources because you’re being a client. Or you would have to call some minificado and/or compacted or download and work it. The ideal is to download, even with the updates, in fact I imagine that at most check once a day.

  • The Javascript Minifier has an API that you can perform compression. It does not help you?

  • I believe this guy does more or less what Gulp does @bio, does not minify in download time an external resource.

  • @Lucascosta I think so, this link has an example of how to perform real-time compression with PHP. Below is a library that a user created to do this, tested and worked with jQuery. It compresses in real time and writes the compressed file to a project folder. The only problem I noticed is that the compression was very long.

  • Have you tried making asynchronous calls to these files?

  • Darley, searching, when I make these calls, will it load the JS file only after the site loaded? I didn’t get it very well.

Show 3 more comments

1 answer

1

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

It relies on the site’s API Javascript Minifier. 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 downside is that as compression is done in real time, the request becomes slow and it turns out to be no advantage to put directly into the body of the page of your site, since every time it will perform this compression. An exit would be to insert a if before you start compressing and compare the date of the two files to check if your script is up to date.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.