PMC Plugins
Pmc Preload

pmc-preload

Preload assets to improve performance.

Adding a new type of preloaded asset

To add a new type of preloaded asset:

  1. 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).
  2. Create a class that implements the Queue trait.
  3. In the new class, implement a static add() method that accepts the relevant arguments for the particular type of asset.
  4. In the add() method, compact the arguments into an array and pass it to the Queue::_add() method.
  5. In the _add() method, add appropriate validation for the arguments passed from the add() method before adding the arguments array to the $_queue array provided by the Queue trait.
  6. 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.