简体   繁体   English

Chrome / Web浏览器重新缓存错觉

[英]Chrome / Web Browser Re-Cache Illusion

Okay, so I came up with a cool way of forcing a file to re-cache itself only if it's modified. 好的,所以我想出了一个很酷的方法,即仅在文件被修改时才强制文件重新缓存自身。 Below is the following code in JS: 以下是JS中的以下代码:

function getCookie(name) {
    var value = "; " + document.cookie;
    var parts = value.split("; " + name + "=");
    if (parts.length == 2) {
        return parts.pop().split(";").shift()
    } else {
        return ''
    }
}
function fetchHeader(url, wch) {
    try {
        var req = new XMLHttpRequest();
        req.open("HEAD", url + '?t=' + Date.now(), false);
        req.send(null);
        if (req.status == 200) {
            return req.getResponseHeader(wch);
        } else return false;
    } catch (er) {
        return er.message;
    }
}
if (['npcdata.txt'].indexOf(u) != -1) {
    lastModified = fetchHeader(u, 'Last-Modified');

    if (lastModified != getCookie(u)) {
        fc = '?=' + Date.now() + '';
        document.cookie = u + '=' + lastModified + '';
    }
} else {
    fc = ''
}
x = this.ActiveXObject;
x = new(x ? x : XMLHttpRequest)('Microsoft.XMLHTTP');
x.open(d ? 'POST' : 'GET', u + fc, 1);
--sniped code, rest of ajax request data here like (x.send)--

Now, forgive me for the long code. 现在,请原谅我的冗长代码。 Basically it performs a HEAD HTTP Request to check the files header and last modified date. 基本上,它执行HEAD HTTP Request来检查文件头和上次修改日期。 Then, it compares it with the last modified date of that file stored in the users cookie, if it's different, I apply the ?=Date.now() string to the end of the AJAX URL With my x parameter so it re downloads the new file. 然后,将其与用户cookie中存储的文件的最后修改日期进行比较(如果不同),我使用x参数将?=Date.now()字符串应用于AJAX URL的末尾,以便重新下载新文件。 It works fine! 工作正常! and it shows the new modified file contents. 并显示新的修改文件内容。

The issue is... For some very odd reason, after it downloads the new file with the ?t=Date.now() it reads the new changes of the file, but then that data is not being cached after another refresh. 问题是...由于某些非常奇怪的原因,在使用?t=Date.now()下载新文件之后,它会读取文件的新更改,但随后的数据在再次刷新后不会被缓存。 It goes back to the original npcdata.txt values before I even applied the ?t=Date.now() . 在我什至应用?t=Date.now()之前,它都会返回到原始npcdata.txt值。 That doesn't make any sense to me. 这对我来说毫无意义。

How come adding the ?t=Date.now() string to the end of the url grabs the new data, but doesn't re-cache the new file? ?t=Date.now()在URL末尾添加?t=Date.now()字符串来获取新数据,但又不re-cache新文件? But it's giving me the illusion it is because when the file is request with AJAX with that ?t= string, it shows the updated version but then boom, right after the ?t=Date.now() string is removed it goes back to the original cached version? 但这给我一种错觉,因为当使用带有?t=字符串的AJAX请求文件时,它显示了更新的版本,但随后繁荣了,在删除?t=Date.now()字符串之后,它立即返回原始的缓存版本? What? 什么?

So, it's impossible to re-cache the file only when it's been modified by checking the last modified date? 因此, 通过检查上次修改日期对文件进行修改后,才能重新缓存文件吗? There has to be a way using Javascript. 必须有一种使用Javascript的方法。

I found the issue. 我发现了问题。

I guess Chrome saves whatever file it requested with, with the ?t= as it's own cached, and doesn't re-cache the origin of the file . 我想Chrome会使用?t=来保存它请求的任何文件,因为它是自己缓存的,并且不会重新缓存文件的来源 I suspect that would be a performance issue with chrome re-updating the cache each time for each new cached file with a new timestamp on it recursively. 我怀疑这是chrome每次为每个新的缓存文件递归更新每个新缓存文件时都会重新更新缓存的性能问题。 So, that makes sense. 因此,这很有意义。

The fix is I simply need to add the timestamp that was saved in the users cookie to the ?t= paremeter. 解决方法是,我只需要将保存在用户cookie中的时间戳添加到?t=参数中。 So, for example in my code it would be: 因此,例如在我的代码中它将是:

fc='?='+getCookie(u)+'';

This way, that file will always have the same exact version number and would stay cached, and if modified, it will be updated and changed. 这样,该文件将始终具有相同的确切版本号并保持高速缓存,并且如果被修改,它将被更新和更改。

As pertinent to that screenshot, it looks a bit ugly, but we can change the data values around in Javascript later to make the URL prettier. 该屏幕快照有关 ,它看起来有些丑陋,但是我们可以稍后在Javascript中更改数据值,以使URL更漂亮。

What went wrong here? 这里出了什么问题? I just thought that applying the ?t='+Date.now()+' would re-cache the ORIGINAL file, it does not. 我只是认为应用?t='+Date.now()+'会重新缓存原始文件,事实并非如此。 You need to keep having that same value (wherever you stored it), and keep it applied to the ?t= parameter. 您需要保持相同的值(无论将其存储在何处),并将其应用于?t=参数。 For the new cached file to have it's effect. 为了使新的缓存文件生效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM