阻止浏览器缓存文本文件 [英] stop browser from caching a text file

查看:107
本文介绍了阻止浏览器缓存文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在圣诞节的十二天里,我正在运行这个简单的家庭网站。作为其中的一部分,我每天都会编辑一个文本文件,每天都会给出不同的答案/内容。好吧,我有一个问题,我必须告诉大家删除缓存以获取新信息。我以前从来没有过这个。我刚刚添加了这两个语句。

For the twelve days of Christmas I'm running this simple family website. As part of it, everyday I edit a text file that will give different answers/content each day. Well I'm having a problem where I have to tell everyone to delete there cache to get the new info. I've never had this before. I just barely added these two statements.

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />

response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
response.setDateHeader("Expires", 0);

找到如何在所有浏览器中控制网页缓存?

我测试过所有东西仍然可以正常加载,但我不想通过改变实际内容来测试它直到明天。我的问题是,有没有办法确保浏览器不使用此javascript代码缓存我使用的文本文件

I tested that everything still loads fine but I do not want to test it by changing the actual content until tomorrow. My question is, is there any way to make sure that the browser does not cache the text files that I am pulling in using using this javascript code

function readInFile(file)
{
    var rawFile = new XMLHttpRequest();

    rawFile.open("GET", file, true);
    rawFile.onreadystatechange = function()
    {
        if (rawFile.readyState === 4)
        {
            if (rawFile.status === 200 || rawFile.status == 0)
            {
                parseFile(rawFile.responseText, document.getElementById("answerText").value)
            }
        } 
    };
    rawFile.send(null);
}

我不希望浏览器停止缓存html / js但继续缓存文本文件。我现在拥有的东西会阻止浏览器这样做吗?我之前从来没有遇到这样的问题>这次我也使用了HTML5 Boilerplate只是为了尝试一下,如果这有任何区别的话。我最终删除了很多。

I don't want the browsers to stop caching the html/js but continue caching the text files. Will what I have now prevent the browsers from doing such? I've never had a problem like this before> I also used the HTML5 Boilerplate this time around just to try it out if that makes any difference. I ended up removing alot of it though.

推荐答案

Cache-busting

将随机参数添加到网址的末尾,与当前时间戳一样。使它看起来像:

Add a random parameter to the end of the url, like the current timestamp. Make it look like:

http://yourdomain.com/path/to/file.txt?t=1873261823

您可以使用 new Date()。getTime()或更新的 Date.now()或滚动您自己的随机数生成器作为参数的值。

You can use new Date().getTime() or the more recent Date.now() or roll your own random number generator for the value of the parameter.

这篇关于阻止浏览器缓存文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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