From 13e04999d2b20ee07285a7d92ac982d5fc2ce7df Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Wed, 20 Sep 2023 17:02:55 +0530 Subject: [PATCH] fix: Add version tag for non-bundled assets --- frappe/public/js/frappe/assets.js | 40 +++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/frappe/public/js/frappe/assets.js b/frappe/public/js/frappe/assets.js index 5f9153d749..6e5f38e394 100644 --- a/frappe/public/js/frappe/assets.js +++ b/frappe/public/js/frappe/assets.js @@ -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(); }); },