samjuk/m2-module-fetch-priority

Adds ability to set fetch priority attributes, preload/prefetch links for admin content. As well as a public API for extension from 3rd party modules

v0.0.1 2025-02-25 11:53 UTC

This package is auto-updated.

Last update: 2025-02-25 11:54:30 UTC


README

Supported Magento Versions CI Workflow Status GitHub Release

This module adds options to set the fetch priority & lazy loading attribute on images added via the admin area.

It also provides a simple API to add preload & prefetch link tags to the header from other modules.

Installation

composer config repositories.samjuk-m2-module-fetch-priority vcs git@github.com:SamJUK/m2-module-fetch-priority.git
composer require samjuk/m2-module-fetch-priority:@dev
php bin/magento setup:upgrade && php bin/magento cache:flush

Link Tag Usage

To use this module to add preload/prefetch link tags

Example Preload Usage

class MyClassToAddPreloads
{
     public function __construct(
          private readonly \SamJUK\FetchPriority\Model\LinkStore $linkStore,
          private readonly \SamJUK\FetchPriority\Model\LinksPreloadFactory $preloadFactory
     ) { }

     public function execute()
     {
          // Do Stuff
          $preload = $this->preloadFactory->create([
               'href' => 'https://app.magento2.test/media/my_custom_entity/image1.jpg',
               'mimeType' => \SamJUK\FetchPriority\Enum\FetchPriority\Preload\MimeType::ImageJPEG,
               'asType' => \SamJUK\FetchPriority\Enum\FetchPriority\Preload\AsType::Image,
               'fetchPriority' => \SamJUK\FetchPriority\Enum\FetchPriority::High
          ]);
          $this->linkStore->add($preload);
     }
}

Example Prefetch Usage

class MyClassToAddPrefetches
{
     public function __construct(
          private readonly \SamJUK\FetchPriority\Model\LinkStore $linkStore,
          private readonly \SamJUK\FetchPriority\Model\LinksPrefetchFactory $preloadFactory
     ) { }

     public function execute()
     {
          // Do Stuff
          $prefetch = $this->preloadFactory->create([
               'href' => 'https://app.magento2.test/media/my_custom_entity/image1.jpg',
          ]);
          $this->linkStore->add($prefetch);
     }
}