英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论