I currently have my Unity WebGL build deployed into one of my company servers. The hosting provider is Aruba.
The issue I'm encountering is that when I release a new build onto the same address (I delete and paste the new build files), the old build files get cached permanently for the users.
So it's necessary for them to hard refresh the browser (usually Chrome) to get the latest version of the game. Also, if they normally refresh after a hard refresh the old build gets loaded instead, so it's necessary to always hard refresh when landing on the page.
I've tried many options, for instance, now I am using versioning to avoid this issue, but that does not prevent anything. I've tried to disable data caching from inside Unity, but I would still like the build to be cached (otherwise the loading time at each session would be impossible to afford), but also when an update comes online the browser should automatically detect that.
This is a sample of what I currently do:
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
...
<script src="Build/EyeRidersX.loader.js?v=1.0.5"></script>
<link rel="stylesheet" href="TemplateData/style.css">
<link rel="manifest" href="manifest.webmanifest">
...
</head>
<body>
<div id="unity-container">
<canvas id="unity-canvas" width=960 height=600></canvas>
<div id="unity-loading-bar">
<div id="unity-logo"></div>
<div id="unity-progress-bar-empty">
<div id="unity-progress-bar-full"></div>
</div>
</div>
<div id="unity-warning"> </div>
</div>
<div id="unity-progress-bar-full"></div>
</div>
</div>
<div id="unity-warning"> </div>
</div>
<script>
window.addEventListener("load", function () {
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("ServiceWorker.js");
}
});
var container = document.querySelector("#unity-container");
var canvas = document.querySelector("#unity-canvas");
var loadingBar = document.querySelector("#unity-loading-bar");
var progressBarFull = document.querySelector("#unity-progress-bar-full");
var warningBanner = document.querySelector("#unity-warning");
function unityShowBanner(msg, type) {
function updateBannerVisibility() {
warningBanner.style.display = warningBanner.children.length ? 'block' : 'none';
}
var div = document.createElement('div');
div.innerHTML = msg;
warningBanner.appendChild(div);
if (type == 'error') div.style = 'background: red; padding: 10px;';
else {
if (type == 'warning') div.style = 'background: yellow; padding: 10px;';
setTimeout(function() {
warningBanner.removeChild(div);
updateBannerVisibility();
}, 5000);
}
updateBannerVisibility();
}
var buildUrl = "Build";
var loaderUrl = buildUrl + "/EyeRidersX.loader.js?v=1.0.5";
var config = {
dataUrl: buildUrl + "/EyeRidersX.data?v=1.0.5",
frameworkUrl: buildUrl + "/EyeRidersX.framework.js?v=1.0.5",
codeUrl: buildUrl + "/EyeRidersX.wasm?v=1.0.5",
streamingAssetsUrl: "StreamingAssets",
...
productVersion: "1.1.1",
showBanner: unityShowBanner,
};
...
</script>
</body>
</html>
Unity officially states this:
the issue in my case is that one .js (
***.framework.js) file is cached and is not updated when a new build is released.For this reason, this code that should avoid caching does not work:
For my web build Progressive Web App (PWA) there's a file called
ServiceBuilder.jsthat rules caching operations. The original file always contained the same name for the cache, which prevented the browser from refreshing all the files. Below I implemented this procedure at each new update of a build I assign a unique version name for each release (something that I wasn't doing) then I check if a previously cached version exists if so I erase it and start the process of caching (obviously .br depends on you compression format, I went for brotli):