【问题标题】:How do I set an expiry date for each asset's cache in my website?如何在我的网站中为每个资产的缓存设置到期日期?
【发布时间】:2016-10-29 17:55:42
【问题描述】:

我在网上搜索了一段时间以寻找答案,发现this 等等。

但是,我没有发现任何关于如何为每个资产(样式表、图像等)设置特定且单独的到期日期的见解。

<head> 标记中使用<meta http-equiv="cache-control" content="max-age=n"> 似乎不会产生预期的结果,因为Google 的PageSpeed Insights 由于缺乏缓存控制而对我的网站的评分仍然很低。

顺便说一句,我的网站是纯 HTML。

【问题讨论】:

  • 我不知道 标签。但可以通过使用 .htaccess 文件为每个资产创建单独的到期日期来完成。

标签: html caching http-headers meta-tags cache-control


【解决方案1】:

感谢nayabbashasayed 为我指明了正确的方向。

我创建了一个 .htaccess 文件并使用了我找到的以下代码here

# Check for files with the listed extensions
<filesMatch ".(css|jpg|png|gif|js|woff)$">
# If a file with any of the above extensions are found, set the maximum age for the asset in seconds
Header set Cache-Control "max-age=86400, public"
</filesMatch>

当设定的时间过去后,用户将不得不从服务器检索资产,而不考虑之前为该文件缓存的内容。更多信息可以在我在问题中发布的链接中找到(也是正确的here)。

编辑:

要像我最初要求的那样为特定文件设置 max-age,请在 .htaccess 文件中使用 &lt;IfModule&gt; 标记。更多信息here

【讨论】:

  • 不客气。对于我的网站,我为每个资产创建了.htaccess 条目。它在谷歌洞察中显示了 95% 的优化。
猜你喜欢
  • 2011-11-04
  • 1970-01-01
  • 1970-01-01
  • 2021-11-15
  • 2011-12-27
  • 1970-01-01
  • 2017-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多