views:

260

answers:

2

I have an html file saved in gzip format. The browser displays the html file but without the javascript and CSS. Non-zipped html files in the same directory do display correctly. In addition, I saved the source from the compressed html file and it reopened correctly, with JS and CSS applied.

What is different about displaying the zipped html that would not allow it to pick up the JS and CSS?

+2  A: 

The basic problem is you can't just serve a gzip file where the browser expects CSS. By itself, that does not work any more than if you return a JPEG or a ham sandwich.

When content gets zipped on the fly, the response is somewhat different -- the response says "I am text/css, but, happen to be encoded with gzip for transfer". The browser can figure that one out.

Some web servers like Apache will do that sort of thing for you if you supply gzipped files locally, too. But I imagine your server isn't.

Why does it work for HTML? Hmm, I don't know, maybe your browser actually manages to figure it out in that particular case?

What you ultimately want to do is serve the response with Content-Type: text/css and Content-Encoding: gzip to have it recognized correctly.

Sean Owen
+1  A: 

it you're working on Localhost on your own server (like XAMPP)
then you need to configure the .htaccess file to send the right
headers that said that files might be gziped.

try adding this to you main .htaccess file :

AddEncoding x-gzip .gz
AddType text/html .gz

and make sure your gziped compressed
files are endind with the .gz extention.

also, always run this in a server.. :)

vsync