Cache Bust Self Hosting

Hi everyone,

I’ve moved my project to self-hosting and everything basically works as I’d want it to, but there are the occasional inconveniences. One in particular is the lack of auto-cache busting when downloading the self-hosted files. As far as I can tell, all the key files (ones that change frequently, or might change frequently) like __game-scripts.js or any of the scene jsons have no way of cache busting.

It is possible to solve it if I go in and physically change __game-scripts.js to something like __game-scripts-0.js and then alter the links in the config.json file, but that’s impractical if I’m doing rapidfire updates to live (and otherwise just kind of a pain to do repeatedly).

I was wondering, and I suppose this might fit better under Suggestions and Feedback, if there could be a tickbox, or even just an automatic hashing added to every file when downloading for self-publishing? Alternatively, if someone knows of a quick and easy way to automate it that doesn’t end up taking more than like 5 minutes of time, that might work as well.

1 Like

I think caching configuration depends on your server, where you host the app. If it is your server, then you can disable caching for development environment. If the server is not yours, you can probably open the app in browser’s incognito mode.

The server hosts the live environment, in this particular case. I’m mainly looking for something more convenient than me modifying all the files every time I want to patch to live. Otherwise I don’t want to outright disable caching, for the obvious reasons.

I think the ideal route is to find how to clear the server cache for whatever server you have. ie, I know Cloudflare caches by default, but you can manually clear it or temporarily disable it for development. Apache and Nginx probably have similar capabilities.

Regardless, I’m in the same boat for a client request. It looks like PlayCanvas does do cache busting for most assets, but yeah __game-scripts.js, your scene settings and asset config file don’t have any cache busting. It would be nice if the engine made it as easy to add a postfix to asset paths as it is to add a prefix. But, this is what I ended up doing:

For non-cache-busted script assets like __game-scripts.js, I just created a bash script to add a version query to all assets in the config.json file. At first I did it only to js files, but then was like fk it and did it to all assets, the engine handles query parameters in the URLs without issue, so it’s not a problem. The script below looks for "url":" and appends the query parameter v with the value being the datetime.

VERSION=$(date +'%Y.%m.%d.%H.%M.%S');sed -i "s/\(\"url\":\"[^\"?]*\)\"/\1?v=${VERSION}\"/g" config.json

That leaves the config.json itself and your scene config <scene integer ID>.json (I don’t know how projects with multiple scenes are handled though). Because these files appear as filenames in the __settings__.js file, I added a random query in the index.html specifically for those files. This is what that looks like:

    <script>
      var VERSION = Math.random().toString(16).substr(2,5);
      CONFIG_FILENAME = 'config.json?v=' + VERSION;
      SCENE_PATH = '940622.json?v=' + VERSION;
    </script>

For completeness I manually added a parameter onto the <script> src’s in the index.html, but those files don’t update much so probably won’t update them often.

    <script src="./playcanvas/__settings__.js?v=2020.8.10"></script>