X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=includes%2Fresourceloader%2FResourceLoaderModule.php;h=b3c1cd14b7406365d11defd95b78dba320d1de7c;hp=743b69b3fee4d96d19abb711a568c2f29ad4320b;hb=6c9a2923fe1ee3a65cb027be5e781772f2b12fbd;hpb=b3f0a59256a821e9f446d2201b401a77ffb93331 diff --git a/includes/resourceloader/ResourceLoaderModule.php b/includes/resourceloader/ResourceLoaderModule.php index 743b69b3fe..b3c1cd14b7 100644 --- a/includes/resourceloader/ResourceLoaderModule.php +++ b/includes/resourceloader/ResourceLoaderModule.php @@ -461,7 +461,6 @@ abstract class ResourceLoaderModule implements LoggerAwareInterface { * @param array $localFileRefs List of files */ protected function saveFileDependencies( ResourceLoaderContext $context, $localFileRefs ) { - try { // Related bugs and performance considerations: // 1. Don't needlessly change the database value with the same list in a @@ -587,6 +586,81 @@ abstract class ResourceLoaderModule implements LoggerAwareInterface { $this->msgBlobs[$lang] = $blob; } + /** + * Get headers to send as part of a module web response. + * + * It is not supported to send headers through this method that are + * required to be unique or otherwise sent once in an HTTP response + * because clients may make batch requests for multiple modules (as + * is the default behaviour for ResourceLoader clients). + * + * For exclusive or aggregated headers, see ResourceLoader::sendResponseHeaders(). + * + * @since 1.30 + * @param ResourceLoaderContext $context + * @return string[] Array of HTTP response headers + */ + final public function getHeaders( ResourceLoaderContext $context ) { + $headers = []; + + $formattedLinks = []; + foreach ( $this->getPreloadLinks( $context ) as $url => $attribs ) { + $link = "<{$url}>;rel=preload"; + foreach ( $attribs as $key => $val ) { + $link .= ";{$key}={$val}"; + } + $formattedLinks[] = $link; + } + if ( $formattedLinks ) { + $headers[] = 'Link: ' . implode( ',', $formattedLinks ); + } + + return $headers; + } + + /** + * Get a list of resources that web browsers may preload. + * + * Behaviour of rel=preload link is specified at . + * + * Use case for ResourceLoader originally part of T164299. + * + * @par Example + * @code + * protected function getPreloadLinks() { + * return [ + * 'https://example.org/script.js' => [ 'as' => 'script' ], + * 'https://example.org/image.png' => [ 'as' => 'image' ], + * ]; + * } + * @encode + * + * @par Example using HiDPI image variants + * @code + * protected function getPreloadLinks() { + * return [ + * 'https://example.org/logo.png' => [ + * 'as' => 'image', + * 'media' => 'not all and (min-resolution: 2dppx)', + * ], + * 'https://example.org/logo@2x.png' => [ + * 'as' => 'image', + * 'media' => '(min-resolution: 2dppx)', + * ], + * ]; + * } + * @encode + * + * @see ResourceLoaderModule::getHeaders + * @since 1.30 + * @param ResourceLoaderContext $context + * @return array Keyed by url, values must be an array containing + * at least an 'as' key. Optionally a 'media' key as well. + */ + protected function getPreloadLinks( ResourceLoaderContext $context ) { + return []; + } + /** * Get module-specific LESS variables, if any. * @@ -712,6 +786,11 @@ abstract class ResourceLoaderModule implements LoggerAwareInterface { $content['templates'] = $templates; } + $headers = $this->getHeaders( $context ); + if ( $headers ) { + $content['headers'] = $headers; + } + $statTiming = microtime( true ) - $statStart; $statName = strtr( $this->getName(), '.', '_' ); $stats->timing( "resourceloader_build.all", 1000 * $statTiming ); @@ -757,7 +836,6 @@ abstract class ResourceLoaderModule implements LoggerAwareInterface { // (e.g. startup module) iterate more than once over all modules to get versions. $contextHash = $context->getHash(); if ( !array_key_exists( $contextHash, $this->versionHash ) ) { - if ( $this->enableModuleContentVersion() ) { // Detect changes directly $str = json_encode( $this->getModuleContent( $context ) ); @@ -920,6 +998,20 @@ abstract class ResourceLoaderModule implements LoggerAwareInterface { return false; } + /** + * Check whether this module should be embeded rather than linked + * + * Modules returning true here will be embedded rather than loaded by + * ResourceLoaderClientHtml. + * + * @since 1.30 + * @param ResourceLoaderContext $context + * @return bool + */ + public function shouldEmbedModule( ResourceLoaderContext $context ) { + return $this->getGroup() === 'private'; + } + /** @var JSParser Lazy-initialized; use self::javaScriptParser() */ private static $jsParser; private static $parseCacheVersion = 1;