0

I'm about to release a web application with a few pages. Each page is a Vue.js bundle. So on each page, there is a single javascript bundle & a single CSS file included, and a single div with a unique ID in the page where the app elements get mounted.

I need to be able to make updates to the static CSS/JS files without major service disruption. I'm using a Google Firebase backend for the application data, so if the client code doesn't update when an update is deployed, it could try to write to the database in the wrong format in an incorrect way. So, caching of the script files has been a problem.

I was initially under the impression that caches are invalidated when the hash of the file contents changes, but apparently that is not true. So, the core question is: How can I invalidate the browser cache of these files every time the content is updated?

What makes things complicated is that the web application may be embedded on clients' websites, by adding a small snippet to the page. And, I don't want to modify these snippets for every update - so I can't to change the filename with each version. E.g. in someoneswebsite.com.au/app/index.html:

<div id="my-app-mount"></div>
<script src="https://mywebapp.com.au/app/homepage.js"></script>

What won't work for me

  • Adding a query string or changing the filename with every update (Or other server-side tricks in PHP): I can't use any preprocessor as the snippet needs to be embeddable on other sites in HTML only.

  • Just setting a short TTL in the cache for these items. I need updates to work overnight, so I'd have to go down to just an hour or two. And this leads into the next point;

  • Disabling caching completely for these items: I don't want to flog my server with the extra traffic.

  • Just telling my users to do a hard-reload if they have any issues - this is a client-facing product.

My ideas for a solution so far

  • Change the filename with each upgrade app-1.0.js, app-1.1.js and so on, and add a 'bootstrap' script that gets the latest version based on a version string read from Firebase. However, this adds extra latency to every single page load as we need to hear from the database before loading the main JS payload.

  • In each javascript bundle, add a check to compare the app version with a version number retrieved from Firebase. If the script is out of date, we can programatically invalidate the cache and refresh the page (but how to do this?)

  • Some combination of HTTP cache headers, to always invalidate the cached copy if the hashed contents don't match the server.

Mack
  • 691
  • 3
  • 7
  • https://stackoverflow.com/questions/118884/how-to-force-the-browser-to-reload-cached-css-js-files Does it answer to your question ? – pragmethik Apr 18 '20 at 11:26
  • @pragmethik Unfortunately not. I need to serve the script from the same URL to avoid having to change filenames etc. on multiple sites. So no PHP autoversioning and the like. – Mack Apr 18 '20 at 11:50

0 Answers0