Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.0] Add a preload manager #15880

Merged
merged 6 commits into from
Jun 18, 2017
Merged

[4.0] Add a preload manager #15880

merged 6 commits into from
Jun 18, 2017

Conversation

mbabker
Copy link
Contributor

@mbabker mbabker commented May 7, 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

* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • @license GNU General Public License version 2 or later; see LICENSE**.txt**

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have so many inconsistent file headers in the repo? (Fixed)

* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LICENSE.txt

@wilsonge
Copy link
Contributor

wilsonge commented May 8, 2017

Can you fix conflicts please

@mbabker
Copy link
Contributor Author

mbabker commented May 8, 2017

Rebased

@brianteeman brianteeman modified the milestone: Joomla 4.0 Jun 8, 2017
@mbabker
Copy link
Contributor Author

mbabker commented Jun 17, 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).

@wilsonge wilsonge merged commit 9c2c12b into joomla:4.0-dev Jun 18, 2017
@mbabker mbabker deleted the http2-assets branch June 18, 2017 21:51
@mbabker mbabker moved this from Testing/Review to Completed in [4.0] General Jun 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
[4.0] General
  
Completed
Development

Successfully merging this pull request may close these issues.

None yet

5 participants