From 8bb8d213db7077791e571adb1dcf594e5446ddfb Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Wed, 13 Feb 2019 07:37:03 +0800 Subject: [PATCH 1/2] MDL-64835 JS: Stop using the jsrev in the jsrevPrefix We only use the jsrevPrefix to determine if the cache should be invalidated, but the prefix that we were using is based on the new jsrev. For example, the jsrevPrefix will be: hash(wwwroot + '/ + config.jsrev) + '/jsrev' Where config.jsrev is the _current_ (new) jsrev. As a result when searching for the jsrev used to store the data which is currently in the storage cache, no key is returned, and we instead set an 'initial' value and the cache is not cleared This patch changes the jsrevPrefix to be: hash(wwwroot) + '/jsrev' Since the wwwroot does not change, the key remains static for the current site. As a result, when the jsrev is bumped via a Moodle cache purge, we are able to correctly fetch the old jsrev from the cache, determine that the jsrev has changed, and purge the cache. --- lib/amd/build/storagewrapper.min.js | 2 +- lib/amd/src/storagewrapper.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/amd/build/storagewrapper.min.js b/lib/amd/build/storagewrapper.min.js index 01fd0c64179..edc052e7304 100644 --- a/lib/amd/build/storagewrapper.min.js +++ b/lib/amd/build/storagewrapper.min.js @@ -1 +1 @@ -define(["core/config"],function(a){var b=function(b){this.storage=b,this.supported=this.detectSupport(),this.hashSource=a.wwwroot+"/"+a.jsrev,this.hash=this.hashString(this.hashSource),this.prefix=this.hash+"/",this.jsrevPrefix=this.hash+"/jsrev"};return b.prototype.detectSupport=function(){if(a.jsrev==-1)return!1;if("undefined"==typeof this.storage)return!1;var b="test";try{return null!==this.storage&&(this.storage.setItem(b,"1"),this.storage.removeItem(b),!0)}catch(c){return!1}},b.prototype.prefixKey=function(a){return this.prefix+a},b.prototype.validateCache=function(){var b=this.storage.getItem(this.jsrevPrefix);if(null===b)return void this.storage.setItem(this.jsrevPrefix,a.jsrev);var c=a.jsrev;c!=b&&(this.storage.clear(),this.storage.setItem(this.jsrevPrefix,a.jsrev))},b.prototype.hashString=function(a){var b,c,d,e=0;if(0===a.length)return e;for(b=0,d=a.length;b Date: Wed, 13 Feb 2019 07:45:02 +0800 Subject: [PATCH 2/2] MDL-64835 JS: Reduce cache invalidation checks The cache invalidation check was previously occuring every time either the `set` or `get` function was called on the cache. However, the cache invalidation check is based on the jsrev which is static for the lifetime of the page. This change moved the invalidation to happen during the setup of the AMD module such that it only happens one time per storage type (Local + Session). --- lib/amd/build/storagewrapper.min.js | 2 +- lib/amd/src/storagewrapper.js | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/amd/build/storagewrapper.min.js b/lib/amd/build/storagewrapper.min.js index edc052e7304..a82abd9898f 100644 --- a/lib/amd/build/storagewrapper.min.js +++ b/lib/amd/build/storagewrapper.min.js @@ -1 +1 @@ -define(["core/config"],function(a){var b=function(b){this.storage=b,this.supported=this.detectSupport(),this.hashSource=a.wwwroot+"/"+a.jsrev,this.hash=this.hashString(this.hashSource),this.prefix=this.hash+"/",this.jsrevPrefix=this.hashString(a.wwwroot)+"/jsrev"};return b.prototype.detectSupport=function(){if(a.jsrev==-1)return!1;if("undefined"==typeof this.storage)return!1;var b="test";try{return null!==this.storage&&(this.storage.setItem(b,"1"),this.storage.removeItem(b),!0)}catch(c){return!1}},b.prototype.prefixKey=function(a){return this.prefix+a},b.prototype.validateCache=function(){var b=this.storage.getItem(this.jsrevPrefix);if(null===b)return void this.storage.setItem(this.jsrevPrefix,a.jsrev);var c=a.jsrev;c!=b&&(this.storage.clear(),this.storage.setItem(this.jsrevPrefix,a.jsrev))},b.prototype.hashString=function(a){var b,c,d,e=0;if(0===a.length)return e;for(b=0,d=a.length;b