frenchie
frenchie

Reputation: 51947

gzip files as html source

Instead of relying on the server to gizip compress css and js file, is it a good/bad idea to gzip the file, store these on the server and link to those files in the html.

Instead of

<script src="../Scripts/compiled.js"></script>

Have this:

<script src="../Scripts/compiled.js.gzip"></script>

And same with CSS?

I tried but it's not working; the files don't seem to decompress. I get Resource interpreted as Stylesheet but transferred with MIME type application/x-gzip: Is this even possible?

Upvotes: 3

Views: 6085

Answers (1)

Marat Tanalin
Marat Tanalin

Reputation: 14123

Your Gzip file should have response header Content-Encoding: gzip while Content-Type should be text/javascript for JavaScript files or text/css for CSS files.

For me, following .htaccess rules does work:

AddEncoding x-gzip .gz

RewriteEngine On

RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.*)$ $1.gz [QSA,L]

<FilesMatch  \.js\.gz$>
    ForceType "text/javascript; charset=utf-8"
    Header set Cache-control: private
</FilesMatch>

Header set Vary: Accept-Encoding can be used instead of Header set Cache-control: private to prevent returning Gzipped version to user-agents that do not support Gzip compression.

.htaccess file should be placed in directory that contains js.

Gzipped and nongzipped versions should be placed side by side (filename for gzipped version contains .gz postfix). Gzip-encoded version is returned transparently (if .gz version exists and browser supports Gzip which Accept-encoding request header is responsible for) when requesting usual file without Gzip mentioned explicitly in its URL.

P.S. Ah, you are using ASP.net, and therefore it's apparently running under IIS. Well, recent versions of IIS have .htaccess-like functionality, AFAIK.

Upvotes: 3

Related Questions