缓存不同域名的静态资源的可能方式是什么?

huangapple go评论132阅读模式
英文:

What are the possible way to cache static assets from different domains?

问题

如何在www.abc.ru从www.foo.ru缓存JavaScript静态文件?

我尝试通过脚本标签(设置src属性)加载它,但当我访问www.abc.ru时,请求会再次发送,缓存被忽略。浏览器是否按来源分隔缓存或其他方式?

英文:

How can i cache javascript static files on www.foo.ru from www.abc.ru ?

I try load it by script tag (set src attribute), but when i go to www.abc.ru requests is sending again, cache is ignored. Does browser separate cache by origin or something else?

答案1

得分: 0

Terry在评论中所说,你无法直接这样做。以前是可能的,但这是一种信息泄漏(http://malicious-site-example.com可以看到它对http://example.com/some-asset的响应非常快,然后使用这些信息[可能与其他类似的启发式方法结合使用]来推断你最近访问了http://example.com)。所以现在,缓存的响应仅用于最初请求它的源,即不同的源实际上有不同的缓存。

据推测,您只想在您控制两者的一对站点上执行此操作。在这种情况下,您可以在foo.ru的页面上使用一个iframe,直接加载来自abc.ru的页面进行加载。然后是abc.ru发出请求,它被缓存以便与abc.ru连接,而不是与foo.ru连接。您可以通过将iframe设置为零高度或将其移出页面之外等方式隐藏它。

英文:

As Terry said in a comment, you can't do that directly. It used to be possible, but it was an information leak (http://malicious-site-example.com could see that it gets a really fast response to http://example.com/some-asset and use that information [probably in combination with other similar heuristics] to infer that you've been on http://example.com lately). So now, the cached response is only used for the origin that originally requested it — that is, effectively different origins have different caches.

Presumably you only want to do this on a pair of sites where you control both of them. In that case, you might use an iframe on foo.ru's page that directly loads a page from abc.ru that does the load. Then it's abc.ru that's doing the request, and it's cached such that it's connected to abc.ru, not foo.ru. You can hide the iframe by making it zero-height or off the page or similar.

huangapple
  • 本文由 发表于 2023年2月6日 20:48:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361513.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定