? ? ? Success

User tests: Successful: Unsuccessful:

avatar mbabker
mbabker
7 May 2017

Summary of Changes

To support HTTP preloading, a new PreloadManager service is introduced and hooked into JDocument to allow developers to register preload links.

Testing Instructions

In JHtmlJquery, change the line actually loading the jQuery file to read this:

JHtml::_('script', 'vendor/jquery/jquery.min.js', array('version' => 'auto', 'relative' => true, 'detectDebug' => $debug, 'preload' => array('preload')));

With this patch applied, the base response should have a Link:</media/vendor/jquery/js/jquery.min.js>; rel="preload" header.

Documentation Changes Required

To use this API to preload stylesheet and JavaScript assets through either JHtml or JDocument directly, a new preload key is added to the $options array. This is an array of the preload types that should be used for an item. So 'preload' => array('preload') would only add the resource with rel="preload" and 'preload' => array('preload', 'prefetch') would add a value for both rel="preload" and rel="prefetch".

The Link header is computed as the last step of JDocument::render(), meaning this happens after the onBeforeCompileHead event has been run and the <head> element generated, but will be before the onAfterRender event. So this is hookable until pretty late in the request, but afterwards you would need to get the header and manually edit that.

The preload support is not limited to stylesheets and JavaScripts loaded through JDocument or JHtml. In fact, there is a new JDocument::getPreloadManager() method which will fetch the new PreloadManager service and through this you can add preloading requests for any supported reference type.

JDocument will support any Joomla\CMS\Document\PreloadManagerInterface implementation, allowing developers to create their own objects if desired. This can be injected through the options array or through the public set method.

Internally, the default Joomla\CMS\Document\PreloadManager implementation uses Fig\Link\GenericLinkProvider as the internal link provider but any provider implementing PSR-13's Psr\Link\EvolvableLinkProviderInterface can be set using the preload manager's setLinkProvider method.

Lastly, I am only doing this for 4.0 because all of the code required to make an API supporting this is already written elsewhere (Symfony's new WebLink component and the PHP-FIG's Link utilities package) and the dependencies aren't PHP 5.3 compatible. Sure, we can write all of the logic ourselves and get it into 3.x, but frankly I'd rather not do the work to sort out how to internally track these resources or build the Link header with the correct structure.

TODO

  • Sort out passing optional attributes into the preload manager service from JDocument
  • Automated testing
avatar mbabker mbabker - open - 7 May 2017
avatar mbabker mbabker - change - 7 May 2017
Status New Pending
avatar joomla-cms-bot joomla-cms-bot - change - 7 May 2017
Category Repository External Library Libraries
avatar mbabker mbabker - change - 7 May 2017
Labels Added: ? ?
avatar wilsonge
wilsonge - comment - 8 May 2017

Can you fix conflicts please

avatar mbabker
mbabker - comment - 8 May 2017

Rebased

avatar brianteeman brianteeman - change - 8 Jun 2017
Milestone Added:
avatar brianteeman brianteeman - change - 8 Jun 2017
Milestone Added:
avatar mbabker
mbabker - comment - 17 Jun 2017

@wilsonge Just rebased this again. Please have this one tested/reviewed/merged before I re-sync the FW database PR (the less Composer related conflicts I can deal with, the better).

avatar wilsonge wilsonge - change - 18 Jun 2017
Status Pending Fixed in Code Base
Closed_Date 0000-00-00 00:00:00 2017-06-18 21:14:56
Closed_By wilsonge
Labels Added: ?
avatar wilsonge wilsonge - close - 18 Jun 2017
avatar wilsonge wilsonge - merge - 18 Jun 2017

Add a Comment

Login with GitHub to post a comment