Mantra how-to guide

How to use Mantra Instance ID to avoid browser cache

Some times, browsers cache web application assets for higher performance, but this is a problem when the files changes from one update to another. In this article, we show solve this easily.

03-May-2022
A  A  A 

Most of the browsers usually cache some web assets (js, css, images files and the like) to avoid download them continuosly in each web request. And that's fine.

But some browsers only remembers and cache those files according to their names.

This can cause problems when you update your project files from a version to another. How does the browser know that the file ("usersclientjs", for instance) has changed in current version?

This provokes that in some projects updates, browsers don't update their previous cached assets.

In Mantra you can avoid this easily using a special Mustache ID called Mantra Instance ID.

During the process of Mantra application start up, Mantra generates a new Mantra Instance ID, an unique value different from one application start to another.

You can get that value using Mantra.GetInstanceId() API from the backend side, and you can use is in your HTML scripts with the name of miid Mustache variable that Mantra includes automatically, like this:

<img src="/logos/img/{{miid}}mantralogo.png" alt="logo">

Each time to run your Mantra application, Mantra Instance ID changes, and that's why the browsers will ask for those files again when you deploy a new version of your project.

On the other hand, there's a Mantra middleware that removes that miid value when a file is requested to the server, adding SanitizedPath to response object in a web request with the path requested without the miid value (if that was included).

Read more about Mantra Instace ID at official documentation.