pmc-preload
Preload assets to improve performance.
Adding a new type of preloaded asset
To add a new type of preloaded asset:
- Check MDN to confirm the asset type is supported: https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types/preload#what_types_of_content_can_be_preloaded (opens in a new tab).
- Create a class that implements the
Queuetrait. - In the new class, implement a static
add()method that accepts the relevant arguments for the particular type of asset. - In the
add()method, compact the arguments into an array and pass it to theQueue::_add()method. - In the
_add()method, add appropriate validation for the arguments passed from theadd()method before adding the arguments array to the$_queuearray provided by theQueuetrait. - Implement the
_process_item()method to output the<link>tag for the asset.
If there may be multiple classes for a particular type of asset, create a
subdirectory for the asset type and add the class to that directory. See the
Images and Scripts classes for examples.
Why isn't the add() method an abstract method in the Queue trait?
Due to backwards-incompatible changes introduced in PHP 8, the Queue trait
cannot declare the add() method as an abstract method without limiting the
argument to an array. In the interest of a clear API for use in plugins and
themes, each class declares an add() method that accepts arguments
relevant to the type of asset is preloads, rather than accepting an array
that would require special knowledge of each class's accepted arguments.