Weedpacket;11065589 wrote:Query: the reason for having minification/compression. Is it an obfuscation thing? If it's to reduce over-the-wire bandwidth you could instead have the server deliver it gzip-compressed. If the resource is compressed then minification doesn't produce any further reduction (the minification would only strip out redundancies that zipping would strip out anyway).
I have to admit I do like the idea of some of this code being obfuscated as it contains a couple of payment gateway api keys in it. It is presumably safe to put these in one's Javascript as that's what the payment gateway recommends. But the primary motive is page load speed. The client has made it clear that they want pages to load quickly under the belief that our search engine ranking would be adversely impacted for slow page load times.
I'm not familiar with gzip delivery options -- although I vaguely recall that this is possible. As a developer, I'm mostly interested in having compressed delivery be as easy as possible. Our process for reducing these Javascript files currently involves manually minifying them with some tool and we have a single PHP controller that aggregates all our minified JS into a single JS file and then delivers the aggregated, minified JS. Our server caches this JS server-side to enhance performance. I believe we also send a header with the JS to cache it on the browser side. Having loaded it just now, the file, which comprises nearly all our site's JavaScript, is about 225KB. Here's the request header:
GET /minify/script.js HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: https://example.com/my-account/payment
Cookie: csrf=0c17676b225ab12e1cd495bd9517b695; mysite_session=nnslsd5a7vfdhde0h4ovkdebbkabskmr
DNT: 1
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
The no-cache stuff worries me a bit. I wonder if perhaps it's because I did a shift-click on refresh to force page reload.
Here's the response header:
HTTP/1.1 200 OK
Date: Fri, 02 Mar 2018 18:45:16 GMT
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.23
Set-Cookie: csrf=0c17676b225ab12e1cd495bd9517b695; expires=Fri, 02-Mar-2018 20:45:16 GMT; Max-Age=7200; path=/; domain=.example.com
Expires: Sun, 01 Apr 2018 18:45:17 GMT
Cache-Control: max-age=2592000
Pragma: cache
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: Content-Type: application/javascript; charset=UTF-8
If I'm not mistaken, that Content-Type header says we are clearly not sending the content gzipped. And yes I know php 5.5.9 is old. This is my workstation running Ubuntu 14 LTS. I'll be upgrading to Ubuntu 18 LTS in April or so.
Weedpacket;11065589 wrote:You could refer to the object by name ("window", I guess) rather than using [font=monospace]this[/font]. Or you could create an object to serve as a namespace and declare all your stuff inside it; then the only "global" object you create is the namespace.
I learned that the Closure Compiler was eliminating critical constructors from the compressed code because no code had called this constructors. This seems like a ridiculous "feature" to me. I installed node-uglify using apt and this command is rocking:
uglifyjs -v --max-line-len 200000 -nc --no-dead-code -o ./output-file.js ./input-file.js