From 0b1a7d4c59c83958024bf6e201fd93db37f6229b Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Mon, 18 Sep 2017 18:37:13 +0100 Subject: [PATCH] resourceloader: Support isKnownEmpty for general modules It was already supported for only=scripts and only=styles, but not yet for general modules loaded as 'combined'. This is especially useful for general modules that are private/embedded, as this allows us to skip the loader.implement() boilerplate. Bug: T176159 Change-Id: I83e9f74dad75867c10ebd0a94b87c31e4adbb57f --- includes/resourceloader/ResourceLoaderClientHtml.php | 7 +++++++ .../resourceloader/ResourceLoaderClientHtmlTest.php | 3 +++ 2 files changed, 10 insertions(+) diff --git a/includes/resourceloader/ResourceLoaderClientHtml.php b/includes/resourceloader/ResourceLoaderClientHtml.php index 06f9841d44..c76ed91f70 100644 --- a/includes/resourceloader/ResourceLoaderClientHtml.php +++ b/includes/resourceloader/ResourceLoaderClientHtml.php @@ -149,6 +149,13 @@ class ResourceLoaderClientHtml { continue; } + $context = $this->getContext( $module->getGroup(), ResourceLoaderModule::TYPE_COMBINED ); + if ( $module->isKnownEmpty( $context ) ) { + // Avoid needless request or embed for empty module + $data['states'][$name] = 'ready'; + continue; + } + if ( $module->shouldEmbedModule( $this->context ) ) { // Embed via mw.loader.implement per T36907. $data['embed']['general'][] = $name; diff --git a/tests/phpunit/includes/resourceloader/ResourceLoaderClientHtmlTest.php b/tests/phpunit/includes/resourceloader/ResourceLoaderClientHtmlTest.php index 3530d3c1d3..eaae9dc4ca 100644 --- a/tests/phpunit/includes/resourceloader/ResourceLoaderClientHtmlTest.php +++ b/tests/phpunit/includes/resourceloader/ResourceLoaderClientHtmlTest.php @@ -43,6 +43,7 @@ class ResourceLoaderClientHtmlTest extends PHPUnit_Framework_TestCase { 'test.top' => [ 'position' => 'top' ], 'test.private.top' => [ 'group' => 'private', 'position' => 'top' ], 'test.private.bottom' => [ 'group' => 'private', 'position' => 'bottom' ], + 'test.shouldembed.empty' => [ 'shouldEmbed' => true, 'isKnownEmpty' => true ], 'test.shouldembed' => [ 'shouldEmbed' => true ], 'test.styles.pure' => [ 'type' => ResourceLoaderModule::LOAD_STYLES ], @@ -115,6 +116,7 @@ class ResourceLoaderClientHtmlTest extends PHPUnit_Framework_TestCase { 'test.private.bottom', 'test.private.top', 'test.top', + 'test.shouldembed.empty', 'test.shouldembed', 'test.unregistered', ] ); @@ -138,6 +140,7 @@ class ResourceLoaderClientHtmlTest extends PHPUnit_Framework_TestCase { 'states' => [ 'test.private.top' => 'loading', 'test.private.bottom' => 'loading', + 'test.shouldembed.empty' => 'ready', 'test.shouldembed' => 'loading', 'test.styles.pure' => 'ready', 'test.styles.user.empty' => 'ready', -- 2.20.1