Delay API requests for preloading metadata until user interaction

Instead of doing them right after page load, only do them on
hover/touch/focus, when we can expect that the user is about to
click/tap/activate a reply link, but which lets us start the work a
fraction of a second earlier.

Bug: T325598
Change-Id: Ida4cb70d8e9ab423ad2dabca7258f92e9fca3157
This commit is contained in:
Bartosz Dziewoński 2023-01-05 23:47:36 +01:00
parent d326fa4671
commit 360e3584c6
2 changed files with 21 additions and 9 deletions

View file

@ -27,6 +27,10 @@ function ReplyLinksController( $pageContainer ) {
replyButton.on( 'click', controller.onReplyButtonClickHandler, [ replyButton ] );
} );
this.$replyLinkSets.on( 'focusin mouseover touchstart', function () {
controller.emit( 'link-interact' );
} );
// "Add topic" link in the skin interface
if ( featuresEnabled.newtopictool ) {
// eslint-disable-next-line no-jquery/no-global-selector
@ -34,6 +38,10 @@ function ReplyLinksController( $pageContainer ) {
if ( $addSectionTab.length ) {
this.$addSectionLink = $addSectionTab.find( 'a' );
this.$addSectionLink.on( 'click keypress', this.onAddSectionLinkClickHandler );
this.$addSectionLink.on( 'focusin mouseover touchstart', function () {
controller.emit( 'link-interact' );
} );
}
// Handle events on all links that potentially open the new section interface,
// including links in the page content (from templates) or from gadgets.

View file

@ -252,6 +252,19 @@ function init( $container, state ) {
$pageContainer = $container;
linksController = new ReplyLinksController( $pageContainer );
linksController.on( 'link-interact', function () {
// Preload page metadata when the user is about to use a link, to make the tool load faster.
// NOTE: As of January 2023, this is an EXPENSIVE API CALL. It must not be done on every
// pageview, as that would generate enough load to take down Wikimedia sites (T325477).
// It would be barely acceptable to do it on every *discussion* pageview, but we're trying
// to be better and only do it when really needed (T325598).
getPageData(
mw.config.get( 'wgRelevantPageName' ),
mw.config.get( 'wgCurRevisionId' )
);
} );
var parser = new Parser( require( './parser/data.json' ) );
var commentNodes = $pageContainer[ 0 ].querySelectorAll( '[data-mw-thread-id]' );
@ -419,15 +432,6 @@ function init( $container, state ) {
}
} );
// Preload page metadata.
// TODO: Isn't this too early to load it? We will only need it if the user tries replying...
if ( mw.config.get( 'wgDiscussionToolsPageThreads' ) ) {
getPageData(
mw.config.get( 'wgRelevantPageName' ),
mw.config.get( 'wgCurRevisionId' )
);
}
// Page-level handlers only need to be setup once
if ( !pageHandlersSetup ) {
$( window ).on( 'popstate', function () {