http 标头可以加快图像加载速度?

发布于 2024-09-07 01:56:21 字数 398 浏览 4 评论 0原文

我需要在网站的单个页面中加载近 100 张图像。 (产品图片,很多)。每当产品的图像发生变化时,该图像的 url 也会发生变化。即对于一个 url 来说,对应的 iamge 永远不会改变。现在我想确保对于同一个人浏览网站,图像不得再次加载。

过期标头足够好吗? (有时浏览器仍然尝试在手动刷新时加载它)

在服务器端,我的Python脚本在修改时总是返回304,因为标头可用而不检查其值,因为每个图像都有唯一的url。它通常工作良好,但有时当图像部分加载并且用户移动到另一个页面时,用户下次登陆同一页面时服务器返回 304 时会看到一半下载的图像。那么,如果修改了 header,那么可靠性如何?还有什么其他选择?

谢谢。

编辑2:

304响应的内容长度应该是多少?应该是原始内容还是实际内容(即0)?

I need to load almost 100's of images in a single page on my website. ( product image, lots of them ). Whenever image change for product, url for that image also changes. i.e. for one url corresponding iamge never changes. Now I want to make sure that for a same person browsing through website, the image must not load again.

Is expire header is good enough ? ( sometime the browser still try to load it when refreshed manually )

on server side my python script always return 304 when if modified since header available without checking its value as each image has unique url. It normally works good but sometime when images are partially loaded and user move to another page, user see half downloaded images next time he land on the same page as server return 304. so how reliable if modified since header is ? and what are other alternative ?

Thanks.

edit 2:

what should be the content length for 304 response ? should it be the original content or actual content ( i.e. 0 ) ??

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

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

发布评论

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

评论(1

不醒的梦 2024-09-14 01:56:21

当您手动重新加载时,大多数浏览器都会忽略缓存标头,因此这是正常且预期的行为。

Expire 标头应该足以满足此目的。

对于部分加载,可能会出现图片加载过程中TCP连接断开的情况;如果发送图像时未指定 Content-Length,某些浏览器可能会认为连接已关闭,因为所有数据均已发送。如果指定了 Content-Length(也称为图像大小,以字节为单位),则浏览器应注意何时没有完整的图像,并将在下次无条件地重新下载它。

发送304 Not Modified时,不需要发送Content-Length。

When you reload manually, most browsers disregard the caching headers, so this is normal and expected behavior.

The Expire header should be sufficient for this purpose.

As for the partial load, it can happen that the TCP connection gets broken while the image is loading; if you didn't specify Content-Length when sending the image, some browsers may assume that the connection was closed because all the data were sent. If Content-Length (a.k.a. image size in bytes) is specified, the browser should note when it doesn't have the complete image and will re-download it unconditionally the next time.

When sending the 304 Not Modified, it's not necessary to send Content-Length.

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