Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想知道某些文件(如 css、html 和 js)需要被.htaccess设置缓存多长时间,以及为什么每种文件类型的时间设置不同?
在几个例子中,我看到有人将 html 缓存 10 分钟,将 js 缓存一个月,将图像缓存一年。
我认为这取决于资源更新的频率。您的 HTML 内容可能是动态的,因此您不能长时间缓存它。否则,访问者会在长时间延迟后看到更改。
另一方面,图片很少更新,所以可以设置更长的缓存时间。
JavaScript 文件通常会针对新功能或错误修复进行更新。也许您可以为此文件使用版本号 (core.js?v=12323),以便您可以更改 HTML 内容中的数字,以让访问者刷新它们。这样,您也可以将它们缓存更长的时间。