How can I gzip my JavaScript and CSS files?

后端 未结 6 1007
独厮守ぢ
独厮守ぢ 2021-02-05 07:52

I have a problem, I have to gzip a prototype Lib, but i totaly have no idea how to do this, where to start and how does it works. :)

I find some tutorials but that wasn\

相关标签:
6条回答
  • 2021-02-05 08:02

    You can use apache's mod_deflate to automatically compress your files on the fly.

    Example:

    AddOutputFilterByType DEFLATE text/html text/xml text/css text/javascript 
    

    [edit]

    To check if your apache server has already output compression enabled, put the example above into an .htaccess file. Then load an html or js file via the server and check the headers for "Content-Encoding", if it says gzip or deflate, it is enabled.

    0 讨论(0)
  • 2021-02-05 08:04

    Hi If you are using Nginx server the above will not help. Please edit the with command vi /etc/nginx/nginx.conf and add the below lines.

    gzip on;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types    text/plain text/html text/css
              application/x-javascript text/xml
              application/xml application/xml+rss
              text/javascript;
    

    Restart the nginx with command /etc/init.d/nginx reload . It will compress the JS and CSS files.

    0 讨论(0)
  • 2021-02-05 08:14

    You need to handle this stuff using the configuration for HTTP server that you are using.

    Could you please tell me which server you are using ? IIS/Apache ?

    Following is link for compression with the IIS

    0 讨论(0)
  • 2021-02-05 08:15

    I above example similar to this worked great but didn't compress javascript. I needed to add application/javascript.

    AddOutputFilterByType DEFLATE text/html text/xml text/css text/javascript application/javascript 
    
    0 讨论(0)
  • 2021-02-05 08:20

    add this code to your .htaccess it will gzip all your css and js files.

    # BEGIN GZIP
    <ifmodule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
    </ifmodule>
    # END GZIP
    
    0 讨论(0)
  • 2021-02-05 08:24

    Perhaps you should look at the mod_deflate module for Apache: http://httpd.apache.org/docs/2.0/mod/mod_deflate.html

    0 讨论(0)
提交回复
热议问题