2016-04-20 56 views
8

我正在使用Google頁面速​​度洞察來提高性能,但無論我做什麼,我都無法獲得'槓桿瀏覽器緩存'消失。利用瀏覽器緩存 - 更新不起作用

我修改了幾個在線文章中描述的htaccess文件,並添加了我可以的每個組合。在頁面洞察中,它抱怨CSS/PNG/JS/JPG文件。

# Expires Caching Start # 
<IfModule mod_expires.c> 
ExpiresActive On 
ExpiresByType text/css "access 2 day" 
ExpiresByType text/html "access 2 day" 
ExpiresByType image/gif "access 1 year" 
ExpiresByType image/png "access 1 year" 
ExpiresByType image/jpg "access 1 year" 
ExpiresByType image/jpeg "access 1 year" 
ExpiresByType image/x-icon "access 1 year" 
ExpiresByType application/pdf "access 1 month" 
ExpiresByType application/javascript "access 1 month" 
ExpiresByType text/x-javascript "access 1 month" 
ExpiresByType application/x-shockwave-flash "access 1 month" 
ExpiresDefault "access 1 month" 
ExpiresByType text/x-javascript "access plus 1 month" 
ExpiresByType application/x-javascript "access plus 1 month" 
ExpiresByType text/x-javascript "access 1 month" 
</IfModule> 
# Expires Caching End # 

回答

6

你似乎很想念你,加上在許多行中的「訪問加1個月」。 默認應該是你的可讀性二線

這裏是h5bp模板的一部分:

ExpiresActive on 
ExpiresDefault          "access plus 1 month" 
ExpiresByType text/css        "access plus 1 year" 
ExpiresByType text/html        "access plus 0 seconds" 
ExpiresByType application/javascript    "access plus 1 year" 
ExpiresByType application/x-javascript    "access plus 1 year" 
ExpiresByType text/javascript      "access plus 1 year" 

... https://github.com/h5bp/html5-boilerplate/blob/master/dist/.htaccess

5

如果你使用Apache,你可能想要運行這些因爲這對我有效:

sudo a2enmod headers 
sudo a2enmod expires 
service apache2 restart 

您也可以嘗試下面的代碼:

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

該代碼似乎沒有什麼,我已經非常不同。 – connersz

+0

@connersz你試過運行命令嗎? – Edward

0

也許試試這個:

# 1 Month for most static assets 
<filesMatch ".(css|jpg|jpeg|png|gif|js|ico)$"> 
Header set Cache-Control "max-age=2592000, public" 
</filesMatch>