大网数据广告活动
网站运营与故障问答

Prevent browser from caching text file

· 52CE 我爱测

I have a web application that runs on an Apache server on the intranet of an organisation. In this web application there is a JavaScript function that loads a specific text file via JavaScript xhttp = new XMLHttpRequest(); var da…

问题原文

原帖链接 · nutgoodinuff · CC BY-SA 3.0

I have a web application that runs on an Apache server on the intranet of an organisation. In this web application there is a JavaScript function that loads a specific text file via JavaScript

xhttp = new XMLHttpRequest();
var data = xhttp.open("GET", "data.txt", false);

During the use of the webapp this text file may change its contents. These changes need to be reflected in the web app, i.e. the text file must be reloaded, and not be read from browser cache. However, the browser (esp. Chromium) caches the text file. What can be done that the browser always requests the textfile from the server and never caches it?

Can this always reload be done purely with HTML/JavaScript (or perl)?

I don't have access to the apache server configuration. The app uses a lot of perl scripts to assemble the webpage that I'm not familiar with but have access to.

已采纳解答

原帖链接 · Antony Gibbs · CC BY-SA 3.0

You have two approaches to this problem.

The clean way is to set the headers returned for GET data.txt preventing the browser (or proxy) from storing the response in cache. Using apache, this can usually be done by adding a few lines in a .htaccess file. If this data.txt is a static file, this is the only way you could control the headers. From https://stackoverflow.com/questions/11532636/prevent-http-file-caching-in-apache-httpd-mamp

<filesMatch "data\.txt$">
  FileETag None
  <ifModule mod_headers.c>
     Header unset ETag
     Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
     Header set Pragma "no-cache"
     Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
  </ifModule>
</filesMatch>

If GET data.txt is handled by a script (perl, php, java, etc...) then setting those header can be done from the script itself (has to be executed before any content is sent out). Here an example in php

<?php
header("Cache-Control: max-age=0, no-cache, no-store, must-revalidate"); // HTTP/1.1
header("Expires: Wed, 11 Jan 1984 05:00:00 GMT"); // Date in the past
?>

A perl example can be found here https://stackoverflow.com/questions/18056127/set-http-header-in-perl-to-force-download-the-content

If none of those applie, here comes...

The quick and dirty way.

If you can't prevent the call from being cache, then you just change the call and 'trick' the cache. GET data.txt?anyting-different-here-will-do-the-job.just-needs-to-be-different-each-time

A common way is either to use a random number, of to use the current time in milliseconds, or a combination of both.

var ms = Date.now();
var xhttp = new XMLHttpRequest();
var data = xhttp.open("GET", "data.txt?dummy="+ms, false);

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2016-05-23)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。