Rather than upsert module store, explicitly remove old value and then add new one
authorOri Livneh <ori@wikimedia.org>
Tue, 12 Nov 2013 19:19:57 +0000 (11:19 -0800)
committerOri Livneh <ori@wikimedia.org>
Tue, 12 Nov 2013 19:19:57 +0000 (11:19 -0800)
Replacing the content of the module store might fail if the new contents would
exceed the browser's localStorage size limit. To avoid clogging the browser
with stale data, always remove the old value before attempting to set the new
one.

Change-Id: I91d01d845a1a633414b94cc02e142a9956955c9d

resources/mediawiki/mediawiki.js

index 1f89792..328ba83 100644 (file)
@@ -1944,14 +1944,19 @@ var mw = ( function ( $, undefined ) {
                                                var timer;
 
                                                function flush() {
-                                                       var data;
+                                                       var data, key = mw.loader.store.getStoreKey();
                                                        if ( mw.loader.store.enabled !== true ) {
                                                                return false;
                                                        }
                                                        mw.loader.store.prune();
                                                        try {
+                                                               // Replacing the content of the module store might fail if the new
+                                                               // contents would exceed the browser's localStorage size limit. To
+                                                               // avoid clogging the browser with stale data, always remove the old
+                                                               // value before attempting to set the new one.
+                                                               localStorage.removeItem( key );
                                                                data = JSON.stringify( mw.loader.store );
-                                                               localStorage.setItem( mw.loader.store.getStoreKey(), data );
+                                                               localStorage.setItem( key, data );
                                                        } catch (e) {}
                                                }