javascript - 缓存过期 header CSS 和 JS 不工作

标签 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)

最佳答案

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

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/

虽然我觉得这毫无意义,尤其是涉及到 Google Analytics 时......他们不会因为他们自己的软件而惩罚你!

关于javascript - 缓存过期 header CSS 和 JS 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38124682/

相关文章:

javascript - 如何从异步调用返回响应?

javascript - Jquery:什么 HTML 元素启动 ajax

python - Django 服务构建有许多 MIME 类型错误(sveltekit)

firefox - 多个 CSS 背景,图像上的颜色,被忽略

javascript - Backbone.js 使用下划线在模板中显示 JSON

javascript - 排除 Javascript/DOM 中的元素

javascript - css 中的 url 被 angular 重写

javascript - 使用 PreventDefault 覆盖打开新选项卡

javascript - 我想使用 style 标签覆盖 css 类属性。动态更改样式标签值

javascript - 如果我添加 div,insertBefore 将停止在表单中工作