How to Gzip a javascript file? - javascript

179KB of jQuery is Minified and Gzipped to 26KB. I am trying to do the same compressions to my javascript files and found the Yuicompressor to Minify it. But I am still searching for a way to Gzip it.
How can I Gzip my Javascript files?

If you are using it in a webpage, gzip is a configuration method in your web server. The file is gzipped by the server, sent to the browser. No manual action is needed. For Apache: http://httpd.apache.org/docs/2.0/mod/mod_deflate.html.
If you are delivering the code to developers, you could use the gzip command.

You would use gzip, the GNU compression utility. Luckily, the gzip algorithm and file structure is implemented by numerous other tools, such as 7zip (for Windows). You can configure your server (via mod_deflate or others) to compress files on the fly, but for static content its a waste of CPU power.
Here is an article which shows how to transparently serve pre-compressed gzip to browsers which support it: http://blog.alien109.com/2009/03/17/gzip-your-javascript/

You could also use zbugs.com - just provide your website url and it will do everything for you.

Related

Gitlab: can I configure a served javascript file to be served as the client can execute it?

Version control repositories started to serve raw files as text/plain and to add the header content-type-options: nosniff so they can't be used as static hosting. I have an internal GitLab installation that I want to use to host some javascript (it would be used to access GitLab own API).
Is it possible to serve a raw file as Javascript? I'd like to turnoff the http header or change the mime-type of the serverd file.
No, it isn't possible to serve directly from the repository, since GitLab won't serve the correct mime type. If you want to do it, you probably need to use GitLab Pages.

Tomcat and compression: CSS/JS compression works ... sometimes

I have a strange problem with my Tomcat 7 (both on Ubuntu and on Windows 7, no Apache in front of Tomcat) compressing CSS/JS.
It works sometimes:
I have a big third party CSS (comprising of Bootstrap, Angular) and JS (Bootstrap, Angular, JQuery), which is NOT compressed. Tomcat doesn't send an
content-encoding: gzip
But my very own CSS/JS are gzipped, as they should (Tomcat sends an content-encoding: gzip to browser in this case).
So this is the server.xml of my Tomcat:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
compression="force"
compressionMinSize="2048"
compressableMimeType="text/html,text/xml,application/javascript,text/css"/>
So in short:
js/thirdParty.js is uncompressed: FAIL!,
js/own.js is compressed: OK
What could be the reason behind that?
Thanks,
Bernhard
I found it out by myself. I have to add the attribute
useSendfile="false"
to the Connector tag. If I don't, Tomcat will not compress
files greather than 48kb (when I use NIO, which is standard for Tomcat7+).

gzip compression tool usage

I just got the GZip software from this site http://www.gzip.org/#intro and was looking at it and tried to convert a javascript file which it just converted directly and changed the extension to gz.Can someone tell me how can we create a new file without modifying the original file using GZip command line and also is this GZip file the same which say a web server like IIS creates and sends to the client when compression is enabled.
I am thinking of GZipping all our JS and CSS and HTMl files before hand so the web server can directly render it .I know the web server by itself only renders these zip fles if the client supports but I m just trying some new stuff.
Assuming you're on a *NIX machine, you can use
gzip -c FILE > FILE.gz
to write the gzipped data to a different file. The -c writes to stdout, and the > redirects stdout to a file. If you have many you could try a loop in Bash:
for file in *.js
do
gzip -c "$file" > "${file}.gz"
done
Also, be really really certain your server falls back on the nongzipped versions if the client doesn't support it!

Compress JavaScript with gzip

I ran Google PageSpeed Insights to optimize my site and it recommended archiving the numerous JavaScript files with gzip.
How does this work? How are the files imported/included as an archive? Can they all be inside one big archive, or should they be individual archives?
You can configure your web server to do the compression for you; in the case of Apache:
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
I think they were referring to having static content compressed by the web server. The files themselves are the same, but you may need to do some configuration with whatever web server you have.
See this for more info.
Most web servers (Apache, IIS, Node) support this feature internally or with plugins. You usually just have to enable it on the web server, you don't actually create the zip files or change anything manually. Take a look at http://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/

Unexpected token ILLEGAL with gzip compression

I am having a problem with gzip compression on my site. I've combined several javascript files to one files and they work ok without compression. Afterwards I've compressed them (gzip) and tried to run the site again, but get the following error:
Uncaught SyntaxError: Unexpected token ILLEGAL
I've compressed the file using several methods, one using gzip software and I also tried other gzip online compression tools like this one.
I can't understand why I get this error why it doesn't work, because the non-compressed version does work. I ran the site on Chrome, latest version.
It also tells me that the error is in line 1
all_js.js.gz:1
Of course the compressed code has many lines, not just one.
I've read a few answers, but nothing regarding gzip compression.
I get this error when working with Visual Studio (ASP.NET) on local machine.
I tried serving the file from S3 with 'application/x-gzip' headers, but it still didn't work. I want to serve my files via CDN after compressing, so I don't search for server compression solutions.
You cannot compress a javascript file with gzip or any other zip routine and load it into your webpage.
You can do one of two things:
Use a javascript code compression tool like the YUI compressor.
Configure your webserver to compress the files (please read the documentation of your webserver, it should not be very difficult)

Categories