Merge pull request #21693 from ankush/perf/desk_assets

perf: accurate cache eviction for assets
This commit is contained in:
Ankush Menat 2023-07-15 22:22:18 +05:30 committed by GitHub
commit ba311a3ec8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 3 deletions

View file

@ -28,8 +28,11 @@ frappe.assets = {
}
if (localStorage._last_load) {
var not_updated_since = new Date() - new Date(localStorage._last_load);
if (not_updated_since < 10000 || not_updated_since > 86400000) {
let not_updated_since = new Date() - new Date(localStorage._last_load);
// Evict cache every 2 days
// Evict cache if page is reloaded within 10 seconds. Which could be user trying to
// refresh if things feel broken.
if ((not_updated_since < 10000 && is_reload()) || not_updated_since > 2 * 86400000) {
frappe.assets.clear_local_storage();
}
} else {
@ -184,3 +187,15 @@ frappe.assets = {
return path;
},
};
function is_reload() {
try {
return window.performance
?.getEntriesByType("navigation")
.map((nav) => nav.type)
.includes("reload");
} catch (e) {
// Safari probably
return true;
}
}

View file

@ -139,3 +139,8 @@ class TestPerformance(FrappeTestCase):
PathResolver(path).resolve()
with self.assertQueryCount(1):
PathResolver(path).resolve()
def test_consistent_build_version(self):
from frappe.utils import get_build_version
self.assertEqual(get_build_version(), get_build_version())

View file

@ -955,7 +955,7 @@ def get_file_size(path, format=False):
def get_build_version():
try:
return str(os.path.getmtime(os.path.join(frappe.local.sites_path, ".build")))
return str(os.path.getmtime(os.path.join(frappe.local.sites_path, "assets/assets.json")))
except OSError:
# .build can sometimes not exist
# this is not a major problem so send fallback