fix: Add version tag for non-bundled assets

This commit is contained in:
Gavin D'souza 2023-09-20 17:02:55 +05:30
parent 895d68f41d
commit 13e04999d2
No known key found for this signature in database
GPG key ID: 3A7BF4D4340DE6F7

View file

@ -124,20 +124,34 @@ frappe.assets = {
// this is virtual page load, only get the the source
// *without* the template
let itemsProcessed = 0;
if (items.length) frappe.dom.freeze();
if (items.length === 0) {
callback();
return;
}
items.forEach((item, idx) => {
fetch(item)
.then((r) => r.text())
.then((body) => {
frappe.assets.add(item, body);
itemsProcessed++;
if (itemsProcessed === items.length) {
frappe.dom.unfreeze();
callback();
}
});
const versionString = frappe.boot.developer_mode
? Date.now()
: frappe.boot.metadata_version;
async function fetchItem(item) {
// Add the version to the URL to bust the cache for non-bundled assets
let url = new URL(item, window.location.origin);
if (item.indexOf(".bundle.") === -1 && !url.searchParams.get("v")) {
url.searchParams.append("v", versionString);
}
// Force use cache because we are already busting the cache with the version
const headers = { cache: "force-cache" };
const response = await fetch(url.toString(), headers);
const body = await response.text();
frappe.assets.add(item, body);
}
frappe.dom.freeze();
const fetchPromises = items.map(fetchItem);
Promise.all(fetchPromises).then(() => {
frappe.dom.unfreeze();
callback();
});
},