簡體   English   中英

為什么不利用瀏覽器緩存不適用於Javascript?

[英]Why isn't Leverage browser caching not working for Javascript?

通過Google頁面速​​度測試運行我的網站時,它說我需要“利用瀏覽器緩存”在HTTP標頭中為靜態資源設置到期日期或最長期限,指示瀏覽器從本地磁盤而不是通過網絡加載以前下載的資源。 “

然后它列出了我的內部Javascript文件,為什么以下不起作用並修復此問題?

這是htaccess文件的外觀:

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##

<FilesMatch "\\.(js|css|html|htm|php|xml)$">
SetOutputFilter DEFLATE
</FilesMatch>

<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php|pl|jpg|png|gif)$
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>

謝謝

Javascript的官方mime類型是application/javascript ,試試這個。

來源http://www.rfc-editor.org/rfc/rfc4329.txt

x-代表“實驗性”,不再與Javascripts一起使用。

所有這些組合都已棄用,過時或過時:

text/x-javascript
text/javascript
application/x-javascript
application/ecmascript

配置中有更多錯誤的mime類型:

錯誤: image/x-icon右: image/vnd.microsoft.icon

資料來源: http//www.iana.org/assignments/media-types/image/vnd.microsoft.icon

錯誤: application/x-pdf右: application/pdf

資料來源: https//tools.ietf.org/html/rfc3778

您已指定text / x-javascript類型的文件應該是可緩存的,但您是否告訴Web服務器如何識別此類文件? 正如DanFromGermany所說,mimetype確實應該是application / javascript,但是在你的web服務器中,這只是一個任意標簽。 您的網絡服務器可能已經配置為將以.js結尾的文件識別為javascript,在這種情況下,只需更改mod_expires塊中的相應行即可。 您可以在firebug,iehttpheaders中查看webserver返回的mimetype,使用wireshark監視連接或使用命令行中的wget / curl。

如果您的網絡服務器返回的不是application/javascripttext/x-javascript那么請檢查包含您的apache配置的目錄中mime.types文件中的條目。

檢查您的托管配置。 我正在使用Cpanel的“SuperCacher服務”:靜態緩存動態緩存Memcache

他們覆蓋了我的.htaccess設置並導致了這個問題。 一旦我禁用它們,問題就消失了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM