postEdit: Only call mw.cookie.get() if needed
authorTimo Tijhof <krinklemail@gmail.com>
Wed, 8 Mar 2017 04:57:16 +0000 (20:57 -0800)
committerKrinkle <krinklemail@gmail.com>
Tue, 14 Mar 2017 19:19:27 +0000 (19:19 +0000)
Avoid synchronous document.cookie reads unconditionally during page load.
At least avoid the read where it isn't needed, such as non-view actions
and special pages.

Ideally this would happen in mw.requestIdleCallback, but unfurtunately
various extensions have a fragile dependency on reading mw.config
wgPostEdit at initialitation time which would likely fail if this is no
longer set synchronously. To be revisited...

Change-Id: Ib655bf6507333c1a9845a05eb436dca522efbab7

resources/src/mediawiki.action/mediawiki.action.view.postEdit.js

index b339371..5dfdede 100644 (file)
@@ -25,8 +25,7 @@
        var config = mw.config.get( [ 'wgAction', 'wgCurRevisionId' ] ),
                // This should match EditPage::POST_EDIT_COOKIE_KEY_PREFIX:
                cookieKey = 'PostEditRevision' + config.wgCurRevisionId,
-               cookieVal = mw.cookie.get( cookieKey ),
-               $div, id;
+               cookieVal, $div, id;
 
        function removeConfirmation() {
                $div.remove();
 
        mw.hook( 'postEdit' ).add( showConfirmation );
 
-       if ( config.wgAction === 'view' && cookieVal ) {
-               mw.config.set( 'wgPostEdit', true );
-
-               mw.hook( 'postEdit' ).fire( {
-                       // The following messages can be used here:
-                       // postedit-confirmation-saved
-                       // postedit-confirmation-created
-                       // postedit-confirmation-restored
-                       message: mw.msg(
-                               'postedit-confirmation-' + cookieVal,
-                               mw.user
-                       )
-               } );
-               mw.cookie.set( cookieKey, null );
+       // Only when viewing wiki pages, that exist
+       // (E.g. not on special pages or non-view actions)
+       if ( config.wgCurRevisionId && config.wgAction === 'view' ) {
+               cookieVal = mw.cookie.get( cookieKey );
+               if ( cookieVal ) {
+                       mw.config.set( 'wgPostEdit', true );
+
+                       mw.hook( 'postEdit' ).fire( {
+                               // The following messages can be used here:
+                               // postedit-confirmation-saved
+                               // postedit-confirmation-created
+                               // postedit-confirmation-restored
+                               message: mw.msg(
+                                       'postedit-confirmation-' + cookieVal,
+                                       mw.user
+                               )
+                       } );
+
+                       mw.cookie.set( cookieKey, null );
+               }
        }
 
 }( mediaWiki, jQuery ) );