如何将gzip Javascript放入网站。

时间:2014-03-06 02:19:47

标签: javascript html gzip gzipfile

我刚开始加速网页,我想知道是否有人能告诉我如何将压缩的gzip javascript文件放入我的html中?

我使用YUI Compressor压缩了js,它给了我一个gzip文件,我当前的js位于:

<script type="text/javascript" src="js/jquery.flexslider.js">

我只是将src重定向到新的gzip文件吗?如果我这样做,如果浏览器无法读取文件会发生什么?任何帮助将不胜感激,并提前感谢!

我还通过将以下内容放入htaccess来解压缩网站的其余部分:

<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

(hashtag) compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

(hashtag) Or, compress certain file types by extension:
<files *.html>
SetOutputFilter DEFLATE
</files>

(hashtag) remove browser bugs
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

(hashtag) compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

(hashtag) Or, compress certain file types by extension:
<files *.html>
SetOutputFilter DEFLATE
</files>

(hashtag) remove browser bugs
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent 

1 个答案:

答案 0 :(得分:1)

Gzip压缩即时发生,您无需更改文件路径或src。它会在您的服务器和浏览器之间自动发生。没有必要担心不支持gzip的浏览器,很可能是don't exist anymore,例如Netscape 4.0,Opera 4.0和IE 4.0。

上面的代码.htaccess代码应该可以正常工作。


更新

我运行了一个快速测试,目前您的HTML已成功通过gzip传输,似乎只有JS文件尚未压缩。 .htaccess中的这一行是罪魁祸首<files *.html>,它只选择gzip压缩的HTML文件。

我更新了你的配置,试试这个:

<IfModule mod_deflate.c>
    #The following line is enough for .js and .css
    AddOutputFilter DEFLATE js css
    AddOutputFilterByType DEFLATE text/plain text/xml application/xhtml+xml text/css text/javascript application/xml application/rss+xml application/atom_xml application/javascript application/x-javascript application/x-httpd-php application/x-httpd-fastphp text/html

    #The following lines are to avoid bugs with some browsers
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>