简体   繁体   English

禁用浏览器中的图像缓存

[英]Disable images caching in browser

I have Earth weather web application that once per six hours receives new weather maps (as images). 我有一个地球天气Web应用程序,该应用程序每六个小时接收一次新的天气图(作为图像)。 I want to browser to not cache these images to allow user experience fresh weather not cached from previous days. 我希望浏览器不要缓存这些图像,以允许用户体验前几天未缓存的新鲜天气。 My setup is: 我的设置是:

  • Angular 2 application build with --prod param and uploaded to hosting via ftp 使用--prod参数构建Angular 2应用程序,并通过ftp上传到托管
  • Python "backend" that once per six hours download grib data, convert it to images and upload to ftp. 每六个小时下载一次grib数据,将其转换为图像并上传到ftp的Python“后端”。

I've tried to add to header of index.html meta data: 我试图添加到index.html元数据的标头中:

  <meta http-equiv='cache-control' content='no-cache'>
  <meta http-equiv='expires' content='0'>
  <meta http-equiv='pragma' content='no-cache'>

These textures are loaded this way: 这些纹理以这种方式加载:

   this.ParticlesVelocitiesImage = new Image();
   this.ParticlesVelocitiesImage.src = require('./Textures/dirswindsigma995.jpg');
   this.ParticlesVelocitiesImage.onload = () => {
       this.ParticlesVelocities = this.gl.createTexture();
       this.gl.bindTexture(this.gl.TEXTURE_2D, this.ParticlesVelocities);
       this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, this.ParticlesVelocitiesImage.width,
       this.ParticlesVelocitiesImage.height, 0, this.gl.RGBA, this.gl.UNSIGNED_BYTE, this.ParticlesVelocitiesImage)
       this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MAG_FILTER, this.gl.LINEAR);
       this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MIN_FILTER, this.gl.LINEAR);
    }

However textures are still cached. 但是纹理仍然被缓存。 I need to clear cache manually to experience freshly updated weather maps. 我需要手动清除缓存才能体验到最新更新的天气图。

UPDATE After Reactgular and gman responses I started to look after another way of loading images. 更新在Reactgular和gman响应之后,我开始寻找另一种加载图像的方式。 So instead of require() I added <img hidden="true" [src] = "urlwith?timestamp"> tags in my component's HTML template and provide url with timestamp after question mark. 因此,我在组件的HTML模板中添加了<img hidden="true" [src] = "urlwith?timestamp">标签,而不是require()并在问号后为URL提供了时间戳。 Then the rest is the same, onload function of image is triggered and my texture is loaded. 然后其余的相同,图像的onload函数被触发,我的纹理被加载。 However because my webgl2 application is quite performance needy I'd rather avoid another images being rendered in HTML. 但是,由于我的webgl2应用程序非常需要性能,因此我宁愿避免其他图像以HTML呈现。

This is not a texture issue it's an <img> issue. 这不是纹理问题,而是<img>问题。 The supposed solution is you need to send the correct headers from your server to tell the browser not to cache the texture. 假定的解决方案是您需要从服务器发送正确的标头,以告知浏览器不要缓存纹理。 If you can't set the correct headers a workaround is to add query parameters to your image URL. 如果无法设置正确的标题,一种解决方法是将查询参数添加到图像URL。

const imageUrl = `./Textures/dirswindsigma995.jpg?${Date.now()}`;

Now every time you request the image the browser will see a different URL 现在,每次您请求图像时,浏览器都会看到一个不同的URL

Note that it's unlikely you can use require for this. 请注意,您不太可能使用require

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

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