sua*_*kim 7 javascript svg google-chrome cache-control browser-cache
我刚刚发现Chrome不会缓存放置在SVG中的图像,如果它们的cache-control
标题设置为no-cache
.Firefox和IE10似乎忽略了这个设置.
我用静态SVG创建了一个小测试页面:
HTML:
<div style="width: 500px; text-align: center;">
<input id="move-left-btn" type="button" value="<<">
<input id="move-right-btn" type="button" value=">>">
</div>
<div class="svgwrapper" style="width: 500px; height: 250px; background-color: lightgrey;">
<svg id="svg" version="1.1" xmlns="http://www.w3.org/2000/svg" width="500" height="250">
<g id="svggroup" class="transition-on" transform="matrix(0.2,0,0,0.2,80,35)">
<image width="1672" height="887" opacity="1" xlink:href="https://dl.dropboxusercontent.com/sh/q7htlj5h8qqfhjf/SVDuynM7R3/car.png"></image>
</g>
</svg>
</div>
Run Code Online (Sandbox Code Playgroud)
使用Javascript:
$(document).ready(function() {
var curXPos = 80;
// Local test function which represent some server calls in my "real life" scenario
// Just updates the x-position in the transform matrix in this test case
function updateSvgText(svgText, posXDelta) {
curXPos += posXDelta;
if (curXPos < 0) {
curXPos = 160;
} else if (curXPos > 160) {
curXPos = 0;
}
return svgText.replace(/matrix\(.*\)/, 'matrix(0.2,0,0,0.2,' + curXPos + ',35)');
}
// Fetch the new SVG (in real life from server) and rerender it
function moveSvg(posXDelta) {
var svg = $('#svg'),
svgText = updateSvgText($('.svgwrapper').html(), posXDelta);
svg.empty();
svg.append($(svgText).children());
}
$('#move-left-btn').click($.proxy(moveSvg, this, -20));
$('#move-right-btn').click($.proxy(moveSvg, this, 20));
});
Run Code Online (Sandbox Code Playgroud)
cache-control
将源图像标题设置为的工作示例no-cache
(每次按下"移动"按钮后在chrome中闪烁):http:
//jsfiddle.net/zF6NF/4/
cache-control
标题设置为max-age=315360000,public
(无闪烁)的不同源图像的相同示例:http:
//jsfiddle.net/zF6NF/5/
在Chrome中,您可以在第一个示例中看到每个按钮上的图像重新加载(图像的"闪烁"并且在开发工具的网络选项卡中可见),而Firefox在两个示例中都可以顺利地重新呈现SVG而无需重新加载.
一些其他信息:
这只是一个例子.在我的"真实场景"中,我从服务器接收一个新的SVG(而不是updateSvgText
方法调用),这意味着我不能通过改变变换矩阵属性的值来执行SVG的部分更新,但必须每次都重新渲染整个SVG(至少现在......).
我无法控制图像来自哪里意味着两件事:
cache-control
标题有没有办法要么......
cache-control
即使图像来自不受控制的远程位置,也会在本地覆盖/否决标题?毋庸置疑,其他解决方案也非常受欢迎!
谢谢
不幸的是,当涉及到缓存时,99% 都是服务器的工作。
内部指南:这里
浏览器将始终根据某些条件查找文件的最新版本:
就解决方案而言,您有: