[英]How do I put a gzip Javascript into website.
I am newby to speeding up web pages and I was wondering if anyone could tell me how to put a compressed gzip javascript file into my html? 我是来加速网页的新手,我想知道是否有人可以告诉我如何将压缩的gzip javascript文件放入我的html中?
I have compressed the js using YUI Compressor which gave me a gzip file and my current js is located at: 我已经使用YUI Compressor压缩了js,这给了我一个gzip文件,而我当前的js位于:
<script type="text/javascript" src="js/jquery.flexslider.js">
Do I just redirect the src to the new gzip file? 是否只将src重定向到新的gzip文件? And if I do that what happens if a browser can't read the file? 如果我这样做,如果浏览器无法读取文件,该怎么办? Any help would be greatly appreciated and thanks in advance! 任何帮助将不胜感激,并在此先感谢!
I have also gzipped the rest of the site by putting the following into the htaccess: 我还通过将以下内容放入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
Gzip compression happens on the fly, you don't need to change the file paths or src. Gzip压缩是即时进行的,您无需更改文件路径或src。 It happens automatically between your server and the browser. 它会在服务器和浏览器之间自动发生。 There is no need to worry about browsers that don't support gzip, it's very likely they don't exist anymore , such as Netscape 4.0, Opera 4.0 and IE 4.0. 无需担心不支持gzip的浏览器,很有可能它们不再存在 ,例如Netscape 4.0,Opera 4.0和IE 4.0。
Your code .htaccess code above should work fine. 您上面的代码.htaccess代码应该可以正常工作。
Update: 更新:
I ran a quick test and currently your HTML is successfully being transmitted with gzip, seems like only JS files are not yet compressed. 我进行了快速测试,目前您的HTML已成功通过gzip传输,似乎只有JS文件尚未压缩。 This line in your .htaccess
is the culprit <files *.html>
, it is only selecting HTML files for gzip compression. .htaccess
中的这一行是罪魁祸首<files *.html>
,它只是选择HTML文件进行gzip压缩。
I updated your config, try this one: 我更新了您的配置,请尝试以下操作:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.