Forum Moderators: phranque

Message Too Old, No Replies

gzip js files

         

junglesnail

10:24 am on Feb 20, 2010 (gmt 0)

10+ Year Member



Hello,
I've tried everything and it seems that nothing works for me.
I'm using prototype with my own ajax shopping cart + i have google maps on my website so each of my pages is ~2mb.
I tried in Cpanel to use the "Optimize Website" tool but it doesn't seem to work. I don't have access to my provider's server.
I'm currently running on Windows 7, i found a gzip tool and i now have in my /scripts/ folder the *.js and *.js.gz files.
Still i tried rewriting rules i found on this forum, on google but nothing works. I still get the .js files instead of the .js.gz.
Can you please provide a .htacces to get this work ?

PS: I'm newbie with apache rules and mod_rewrite.

Thanks a lot.

londrum

12:20 pm on Feb 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



probably not the best way of doing it, but i do it like this... i serve all my javascript with a .php extension and include a
header('Content-type: text/javascript;charset: UTF-8');
at the top.
then you can gzip them the normal way.

i serve them as .php so i can run a little script over it before i gzip it, removing all the whitespace and comments and other stuff like that.

junglesnail

1:05 pm on Feb 20, 2010 (gmt 0)

10+ Year Member



i'm not sure i'm getting the idea.
You're saying that instead of having example.js i will have example.php with the header included.
And what after? What do you mean by gzip the normal way? :)

junglesnail

1:19 pm on Feb 20, 2010 (gmt 0)

10+ Year Member



i'm now trying this :
ForceType text/javascript
Header set Content-Encoding: gzip

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} !”.*Safari.*”
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule (.*)\.js$ $1\.js.gz [L]
ForceType text/javascript

but also doesn't seem to work :)
i have all my .js.gz files in the same folder

jdMorgan

6:53 pm on Feb 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't see anything wrong with the rewriterule except that the exclusion for Safari is broken and there's a spurious "escape" on the period in the substitution path. The ForceType does need to be qualified, though -- something you can do in the rule itself. I'd suggest:

RewriteEngine On
#
# rewrite .js requests to compressed .js.gz files if they exist, except for Safari clients
RewriteCond %{HTTP_USER_AGENT} !Safari
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.+\.js)$ /$1.gz [T=text/javascript,L]

If this doesn't help, we'll need more information... I'm afraid you've brought your car to the automotive repair shop, but have left immediately after saying only "It's broken." We have no idea what that means, or whether to only change the windscreen wipers or to replace the entire engine and drive-train...

How did you test? - What URL did you request?
What did you expect?
What was the result? (browser view, access log entry, error log entry, Live HTTP Headers view?)
How did the actual result differ from your expectations?
Did you delete your browser cache before testing the new code?
Do you have other previously-working rewriterules in this .htaccess file?
Where, in the server filesystem, is this .htaccess file located?
Where in the server filesystem are the .js and .js.gz files located?

Jim

junglesnail

10:57 pm on Feb 20, 2010 (gmt 0)

10+ Year Member



Jim thanks for answering and you're right. i didn't provide enough information.
I finally got it working. For people who will have trouble with this :
I have a .htaccess file in my /Scripts/ folder which contains all mai .js files and the gzipped .js.gz files.
The .htaccess contains :
ForceType text/javascript
Header set Content-Encoding: gzip

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} !”.*Safari.*”
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule (.*)\.js$ $1\.js.gz [L]
ForceType text/javascript

It seems to work fine with safari also.

Jim your code doesn't seem to work, as you described it sound more rational to me as a newbie but i guess there's a misspelling somewhere. ( which i cannot find :) ).
I checked the 2 variants with some tools i found with google
www.websiteoptimization.com/.../analyze/
<snip>
also with a plugin for mozzila firefox. I don't know if this is the proper testing mode. Please tell me.

Thank you again for your answer and help.

[edited by: jdMorgan at 2:42 am (utc) on Feb 22, 2010]
[edit reason] Sorry, no links, See TOS and Charter. [/edit]