Turn off browser image caching

In order to provide users with fresh weather updates, my Earth weather web application receives new weather maps every six hours as images. I want to prevent the browser from caching these images to ensure that the user always sees the most current data, rather than cached images from previous days. Here is my setup:

  • I have an Angular 2 application built using the --prod parameter and uploaded to hosting via FTP.
  • I utilize a Python "backend" that downloads grib data once every six hours, converts it to images, and uploads them to the FTP server.

I attempted to add the following meta tags to the header of index.html:

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

The textures are loaded in this manner:

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);
}

Despite my efforts, the textures continue to be cached. This requires me to manually clear the cache to view the updated weather maps.

UPDATE Upon receiving feedback from Reactgular and gman, I began exploring alternative methods for loading images. Instead of using require(), I decided to add

<img hidden="true" [src] = "urlwith?timestamp">
tags in my component's HTML template and append a timestamp after the URL. The rest of the process remains the same, where the image's onload function triggers the loading of the texture. However, due to the performance demands of my WebGL2 application, I prefer to avoid additional images being rendered in the HTML.

Answer №1

It seems like the issue lies with the <img> element, not the texture itself. To solve this problem, you should ensure that the correct headers are sent from your server to instruct the browser not to cache the texture. If setting headers is not an option, a workaround is to include query parameters in the image URL.

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

By appending a timestamp to the URL, the browser will view each request as unique.

Keep in mind that using require may not be suitable for this situation.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Host app is failing to render shared components in SSR

Encountering an issue while implementing SSR with module federation? Check out my code example Steps to Begin: Run yarn install:all command Execute yarn shell:server:build task Start the server using yarn shell:server:start Initiate remote services with y ...

Encountering a syntax error when attempting to utilize the colon symbol for specifying data types

Currently, I am a novice who is delving into the world of TypeScript. Here is a snippet of code that I have written: let num: number = 123; console.log(123); However, when attempting to run this file using Node.js and saving it as test.ts, I encounter the ...

The initial Get request does not receive data upon first attempt

In the process of developing an Angular project, I am faced with the task of retrieving data from my backend by making requests to an API. However, before the backend can fetch the required data, certain parameters must be sent through a post request. Once ...

Developing a loader feature in React

I've been working on incorporating a loader that displays when the component has not yet received data from an API. Here's my code: import React, {Component} from 'react' import './news-cards-pool.css' import NewsService fro ...

Set up Angular 2 Universal on your system

Update: I attempted to set up Angular 2 Universal following this guide, but encountered errors when executing the command below. Here is the command I ran: typings install node express body-parser serve-static dexpress-serve-static-core mime --global -- ...

What is the best way to determine the property type dynamically in TypeScript based on the value of another property?

I have been working with Polymorphic relationships and currently have the following TypeScript interface defined: interface SubjectA {} interface SubjectB {} interface SubjectC {} enum SubjectType { SubjectA = 'Subject A', SubjectB = 'S ...

What is the best way to show/hide group items in a PrimeNG dropdown menu?

Is it possible to show or hide group items when clicking on the group header if the group contains items? For instance, I would like to display 3 items (AA, BB, CC) in a dropdown menu. The first 2 options (AA and BB) should be selectable, but when I click ...

Storing a reference globally in React and Typescript: Best practices

In my application, I have multiple instances of a specific component called <Item>. Each <Item> needs to display a dynamic tooltip when hovered over. To achieve this, I am utilizing semantic-ui-react and its Popup component. The conventional m ...

Potential issue with Angular material autocomplete not displaying suggestions when using input.setValue()

After clicking on the input element, the autocomplete options are displayed. However, if I dynamically modify the input element's value, the autocomplete options do not appear. <mat-form-field> <input type="text" [formControl]="d ...

I encountered an error with Firebase when attempting to run functions on my local machine

Encountering a Firebase error when running the function locally using emulator in CLI $ firebase emulators:start --only functions Initiating emulators: ["functions"] functions: Using node@8 from host. functions: Emulator started at http://localhost:50 ...

Encountered an issue when attempting to create a new Angular project using the

Just starting out with Angular and encountered an issue while trying to execute this command ng new my-dream-app The error message I received was: npm ERR! cb() never called! npm ERR! This is an error with npm itself. Please report this error at: npm ER ...

Leveraging Angular 4-5's HttpClient for precise typing in HTTP requests

Utilizing a helper service to simplify httpClient calls, I am eager to enforce strong typing on the Observable being returned. In my service where I utilize the api Service and attempt to obtain a strongly typed observable that emits: export class ApiU ...

The 'asObservable' property is not present on the type '() => any'.ts(2339)

Error: Property 'asObservable' does not exist on type '() => any'.ts(2339) Error: Property 'subscribe' does not exist on type 'Subscription'. Did you mean 'unsubscribe'?ts(2551) Error: Property 'sub ...

Error TS 2322 - The property 'id' is not present in the object of type '{ id: number'

Just starting out with Angular and TypeScript. I created a model with the same properties but encountered an error and am struggling to find a solution: TS2322: Type '{ id: number; model: string; plate: string; deliveryDate: string; deadline: st ...

What is the best way to define 'this' context and reference an instance of an Angular 6 component?

I have successfully created a demo featuring an Earth globe using D3 and JS. Now, I am exploring the process of transforming it into an Angular 6 component. Below is the full demo without Angular: import * as d3 from 'd3v4'; import { Component ...

Understanding TypeScript's ability to infer types in generics

Exploring the world of TypeScript through a robustly typed system for REST requests. Let's dive into the code: This type is used to establish the connection between routes and their respective object types: export interface RoutesMapping { api1: ...

Animations in Angular fail to operate within mat-tabs after switching back when custom components are being utilized

I am facing a similar issue as Olafvv with my angular 16 project. The problem occurs in a mat-tab-group where the :enter animations do not work when navigating away from a tab and then returning to it. However, in my case, the issue lies within a custom su ...

Ways to refresh Prism.js upon ngModel update in Angular 5

Currently, I am in the process of developing a CMS and facing an issue with re-rendering Prism.js to display syntax highlighting in the preview whenever there is a change in the body of the article. Below is the template code: <textarea [(ngModel ...

What are the benefits of adding member functions to the data structures of React.js store?

Using React.js and Typescript, I store plain Javascript objects in the React.js store. These objects are sometimes received from the server without any member functions, but I wish to add functions for better organization. Instead of having to rely on exte ...

Issue with ambient contexts error in TypeScript constructor

What is the correct way to create a constructor in TypeScript? I have been researching and trying different approaches, but it seems like the syntax has changed. Here is my latest attempt: car.d.ts declare class Car { constructor(public engine: string ...