Tobias Baumeister
Tobias Baumeister

Reputation: 2147

Gzipped js files long loading time

I gzipped some of my sitewide js-files/css-files and added this to my htaccess-file:

# BEGIN Gzip Compression
AddEncoding gzip .gz
<filesmatch "\.js\.gz$">
AddType "text/javascript" .gz
</filesmatch>
<filesmatch "\.css\.gz$">
AddType "text/css" .gz
</filesmatch>
<ifmodule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME} \.(js|css)$
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.*)$ $1.gz [QSA,L]
</ifmodule>
# END Gzip Compression

# 1 YEAR
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
Header set Cache-Control "max-age=29030400, public"
</FilesMatch>

# 1 HOUR
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=3600, public"
</FilesMatch>

# 1 HOUR
<FilesMatch "\.(txt|xml|js|css)$">
Header set Cache-Control "max-age=3600"
</FilesMatch>

# NEVER CACHE - notice the extra directives
<FilesMatch "\.(html|htm|php|cgi|pl)$">
Header set Cache-Control "max-age=0, private, no-store, no-cache, must-revalidate"
</FilesMatch>

Unfortunately, the js-files need MUCH time to load now (more than 10 seconds)

What could be the problem?

//Update: And I forgot to mention: It seems like no files is cached.

Upvotes: 0

Views: 124

Answers (2)

tlogbon
tlogbon

Reputation: 1302

To add to the answer given, I think your Rewrite rule is causing a redirect on all requests to the static files, you should use mod_deflate instead. Apache handles gzip compression very with mod_deflate, so I don't think there's really any reason to have separate compressed version of the resources.

For the cache policy, I think you should use mod_expires which, see sample below

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/gif "access plus 5 months"
  ExpiresByType image/jpeg "access plus 5 months"
  ExpiresByType image/png "access plus 5 months"
  ExpiresByType text/css "access plus 1 week"
  ExpiresByType application/x-javascript "access plus 1 week"
  ExpiresByType application/javascript "access plus 1 week"
  ExpiresByType text/javascript "access plus 1 week"
  ExpiresByType image/x-icon "access plus 5 months"
</IfModule>

Upvotes: 0

San Bluecat
San Bluecat

Reputation: 167

Try this, and remove the rewrite rules

#Gzip
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript text/javascript
</ifmodule>
#End Gzip

Upvotes: 1

Related Questions