指示浏览器缓存忽略某些URL参数 [英] Instructing browser cache to ignore certain URL params

查看:183
本文介绍了指示浏览器缓存忽略某些URL参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要两个iframe

 < iframe src = www.URL.com?name=benny&runtime= 1231231> 
< iframe src = www.URL.com?name=benny&runtime=757847584>

在不同时间加载,以映射到浏览器中的相同缓存值。服务器与此无关。基本上,有一种方法可以让chrome,firefox等...在查找src时,缓存api忽略某些参数(在本例中为运行时)。



在用例中,传递单独的参数或单独调用运行时将不起作用

方案

那是不可能的。您需要从 src 中删除​​具有不同值的参数,或者使用 XMLHttpRequest 和blob来请求资源,并从中删除参数使用JavaScript请求URI。如果要使用 XMLHttpRequest ,则还需要考虑同源策略。



浏览器缓存键是以下各项的组合HTTP方法和URI。查询参数是URI的一部分。 HTTP 1.1规范的相关摘录:


主缓存键由请求方法和目标URI组成。
但是,由于当今常用的HTTP缓存通常限制
来缓存对GET的响应,因此许多缓存只是拒绝其他方法
,而仅使用URI作为主要缓存键。


有关更多信息,请检查 XMLHttpRequest2中的新技巧,作者:Eric Bidelman, XMLHttpRequest页面 Blob Web API页面和在MDN上同源政策页面。 p>

如果您控制提供资源的服务器,还可以对其进行调整,以通过某些参数改变缓存条目。例如,在ASP.NET MVC中,可以使用 OutputCache 属性的 VaryByParam 属性。有关更多信息,请检查 Microsoft ASP.NET团队通过输出缓存提高性能


I want the two iframes

 <iframe src="www.URL.com?name=benny&runtime=1231231>
 <iframe src="www.URL.com?name=benny&runtime=757847584>

which are loaded at different times to be mapped to the same cached value in the browser. The server has nothing to do with this. Basically, is there a way to have chrome, firefox, etc... cache api ignore certain parameters (in this case 'runtime') when looking up a src.

Passing a separate param or making a separate call for runtime will not work in the use case

解决方案

That is not possible. You need to either remove parameters with different values from src or use XMLHttpRequest and blobs to request resource, and remove parameters from request URI using JavaScript. If you will use XMLHttpRequest you also need to consider Same-origin policy.

Browser cache key is a combination of HTTP method and URI. Query parameters are a part of an URI. Relevant excerpt from HTTP 1.1 specification:

The primary cache key consists of the request method and target URI. However, since HTTP caches in common use today are typically limited to caching responses to GET, many caches simply decline other methods and use only the URI as the primary cache key.

For more information check New Tricks in XMLHttpRequest2 article by Eric Bidelman, XMLHttpRequest page, Blob Web APIs page and Same-origin policy page on MDN.

If you control server that serves your resources you can also adjust it to vary cache entries by certain parameters. For example in ASP.NET MVC you can use VaryByParam property of a OutputCache attribute. For more information check Improving Performance with Output Caching article by Microsoft ASP.NET team.

这篇关于指示浏览器缓存忽略某些URL参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆