浏览器如何处理过期标头、缓存控制标头、上次修改标头?

发布于 2024-08-30 00:26:05 字数 263 浏览 5 评论 0原文

我是一名 Web 开发人员,曾使用过 PHP 和 .NET。拥有一年多的网络工作经验,我一直无法彻底理解浏览器缓存功能,我希望这里的网络专家可以帮助我。我心中的问题是:

  • 浏览器实际上如何缓存内容,是否请求查看服务器上的缓存文件是否已更改,
  • 开发人员充分利用浏览器缓存的理想方式是什么,而且还能够毫无麻烦地在网站上推送新的更改。我认为如果浏览器以某种方式缓存我的 CSS、JS 和图像,然后每次检查它们对服务器的修改,这可以解决问题。但我不知道该怎么做。

I am a web developer, have worked with PHP and .NET both. having over a year of experience working on web I haven't been able to understand the browser caching features thoroughly, I hope Web Gurus here can help me with it. Questions I have in my mind are:

  • How does browser actually caches stuff, does it request for to see if the cached file has changed on the server or not,
  • What is the Ideal way for a developer to make use of browser caching to its full, but also to be able to push new changes on the site with no hassle at all. I think if browser somehow caches my CSS and JS and Images, and then just makes a checks for their modification to the server every time, this can sort the issue. but I am not sure how to do it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萤火眠眠 2024-09-06 00:26:05

这篇博文可能会有所帮助:

http:// /blog.httpwatch.com/2007/12/10/two-simple-rules-for-http-caching/

它解释了如何使用缓存,同时保持在需要时进行更改的能力。

This blog post may help:

http://blog.httpwatch.com/2007/12/10/two-simple-rules-for-http-caching/

It explains how to use of caching, whilst maintaining the ability to make changes when required.

猫瑾少女 2024-09-06 00:26:05

对于 CSS 文件,标准的“解决方法”是逐步向链接添加参数:

<link rel="stylesheet" href="http://mysite.com/styles.css?v=1">
<link rel="stylesheet" href="http://mysite.com/styles.css?v=2">
...

这将强制浏览器重新加载文件。

With CSS files a standard "workaround" is to incrementally add a parameter to a link:

<link rel="stylesheet" href="http://mysite.com/styles.css?v=1">
<link rel="stylesheet" href="http://mysite.com/styles.css?v=2">
...

This will force a browser to reload the file.

月亮邮递员 2024-09-06 00:26:05

如果您使用服务器端语言,则可以强制浏览器再次下载文件。

<script src="script.js?<?= uniqid(); ?>">
<img src="myfile.jpg?<?= uniqid(); ?>">

但是,如果您想拥有更快的站点,缓存是很好的选择,在开始禁用之前,请记住,不必再次下载任何缓存内容。

还有另一种方法可以控制需要缓存的内容,HTML5 应用程序缓存,转储您的将逻辑缓存到清单文件中。因此,清单文件中的每个文件都不会再次下载,直到清单文件本身发生更改。

请记住,如果您更改了 JS、CSS、图像文件中的某些内容,并且需要将该更改推送给经常访问者,您还需要更新清单文件。您可以添加发布日期,这样浏览器就知道这是一个新的清单,并且会再次下载所有内容。

If you are using a server side language, you can force the browser to download the file again.

<script src="script.js?<?= uniqid(); ?>">
<img src="myfile.jpg?<?= uniqid(); ?>">

But cache is good if you want to have a faster site, before you start disabling keep in mind that anything that is cache won't have to be downloaded again.

There is another way you can control what needs to be cached, HTML5 Application Cache, dump your cache logic into the manifest file. So every file in your manifest won't be downloaded again, until the manifest file itself is changed.

Just remember that if you changed something in a JS, CSS, Image file, and you need to push that change to recurrent visitors you need to also update your manifest file. You can add like the publish date that way the browser knows that it's a new manifest and it will download everything again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文