缓存过期标头CSS和JS无法正常工作

时间:2016-06-30 13:33:08

标签: javascript html css .htaccess caching

我一直在努力加速我的网站。

在Google上进行速度测试时出现的一个问题是“利用浏览器缓存”。我查了一下,并在我的htaccess中添加了一些代码。请参阅以下内容:

## 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 text/js "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 ##

这对我的一些文件有效,但不是全部,我仍然收到一条消息,说明我的js和css文件没有设置为过期。有人可以帮忙吗?我犯错的是他们没有过期?以下是来自Google的错误消息,即使是htaccess中的先前代码。

http://www.mydomain.co.uk/css/grid.css (expiration not specified)
http://www.mydomain.co.uk/js/addons.js (expiration not specified)
http://www.mydomain.co.uk/js/addons/jquery.cookiebar.js (expiration not specified)
http://www.mydomain.co.uk/js/addons/jquery.flexslider.js (expiration not specified)
http://www.mydomain.co.uk/js/base/html5shiv.js (expiration not specified)
http://www.mydomain.co.uk/js/base/modernizr.custom.js (expiration not specified)
http://www.mydomain.co.uk/js/nav.js (expiration not specified)
http://www.mydomain.co.uk/mailer/css/form-style.css (expiration not specified)
http://connect.facebook.net/en_US/sdk.js (20 minutes)
http://www.google-analytics.com/analytics.js (2 hours)
http://www.mydomain.co.uk/fonts/Effra_Std_Bd.woff (2 days)
http://www.mydomain.co.uk/fonts/Effra_Std_Lt.woff (2 days)
http://www.mydomain.co.uk/fonts/Effra_Std_Md.woff (2 days)
http://www.mydomain.co.uk/fonts/Effra_Std_Rg.woff (2 days)
http://www.mydomain.co.uk/mailer/js/jquery.validate.js (2 days)
http://www.mydomain.co.uk/mailer/js/validate.js (2 days)

1 个答案:

答案 0 :(得分:0)

查看上面的代码有一些缺失:

ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType application/x-javascript "access plus 1 month"
ExpiresByType text/javascript "access plus 1 year"
ExpiresByType application/x-ico "access plus 1 year"
ExpiresByType image/icon "access plus 1 year"

Facebook .js文件不是像其他人那样可以解决的,因为它是一个外部文件......你可以使用cron来解决这个问题,这基本上就是存储该文件并在本地加载它。您会发现Google Analytics,Lead Forensics等问题也存在同样的问题。

如果您想尝试使用cron,请查看本指南:http://diywpblog.com/leverage-browser-cache-optimize-google-analytics/

虽然我认为这是毫无意义的,特别是在谷歌分析方面......他们并不会因为自己的软件而惩罚你!