719-286-0751 [email protected]

Fixing Slow Hyvä Page Builder Pages on S3 or R2 Remote Storage (Native Image Dimensions)

A content page with a lot of images takes many seconds to render whenever it is not in the page cache. Cached, it is fast, so the problem shows up at the worst moments: right after a cache flush, after an editor saves the page, and on the sale page that is being edited every hour during the sale.

We measured this on a Magento 2.4.8 store with the Hyvä theme (hyva-themes/magento2-theme-module 1.4.8) and its media in an S3-compatible bucket. A sale landing page with 82 Page Builder images took 10.5 seconds to render uncached. A profiler showed 62% of that time went into reading the width and height of those images.

Need help with your Hyvä store?
If this problem is costing you orders or staff time, our senior Magento and Hyvä team can help you debug it and deploy a fix safely.
Contact us →

Why it happens

Hyvä adds width and height attributes to Page Builder images that have the “use native image dimensions” option set. That is a good thing: the browser can reserve the space before the image arrives, and the layout does not jump.

The work is done by Hyva\Theme\Model\InjectImageDimensions, which a plugin on the Page Builder template filter runs over the rendered content. For every <img> tag carrying data-use-native-image-dimensions="true" it calls injectNativeDimensionsToImgTag(). That method checks that the file exists and opens it with Magento\Framework\Image to read its pixel size. Nothing is remembered between requests.

With media on local disk, that costs very little. With Magento’s remote storage module and the S3 driver, opening an image means downloading the whole file from the bucket into a temporary file and then decoding all of it, to read two numbers. On our store that took between 235 and 700 milliseconds per image, on every uncached render.

1. Confirm it on your store

You need both conditions: remote storage, and content that uses the option. Check the storage driver:

php -r '$e = include "app/etc/env.php"; var_export($e["remote_storage"]["driver"] ?? "local");'

Then count the content that carries the flag:

SELECT page_id, identifier,
       (LENGTH(content) - LENGTH(REPLACE(content, 'data-use-native-image-dimensions="true"', ''))) / 39 AS flagged_images
FROM cms_page
WHERE content LIKE '%data-use-native-image-dimensions="true"%'
ORDER BY flagged_images DESC
LIMIT 20;

Run the same search on cms_block and on the product and category description attributes if your descriptions are built with Page Builder. If nothing comes back, this is not your problem, and adding the plugin below gains you nothing. We ported it to a second Hyvä store and found it had no effect there for exactly that reason.

2. Cache Hyvä’s answer per image path

The fix is an around-plugin on injectNativeDimensionsToImgTag(). It stores what Hyvä answered for a media path, either the width and height or the fact that the file was not found, and on a cache hit it rebuilds the tag the same way Hyvä does.

<?php
declare(strict_types=1);

namespace Cadence\PageBuilderImages\Plugin\Hyva;

use Hyva\Theme\Model\InjectImageDimensions;
use Magento\Framework\App\Cache\StateInterface as CacheState;
use Magento\Framework\App\Cache\Type\Block as BlockCache;
use Magento\Framework\UrlInterface;

class CacheNativeImageDimensions
{
    private const KEY_PREFIX = 'CADENCE_PB_NATIVE_IMG_DIMS_';
    private const NOT_FOUND = '-';
    private const LIFETIME_FOUND = 2592000; // 30 days
    private const LIFETIME_NOT_FOUND = 86400; // 1 day
    private const PATTERN_HYVA_RESULT = '#^<img width="(\d+)" height="(\d+)" #';

    private ?string $mediaBaseUrl = null;

    /** @var array<string, string> */
    private array $memo = [];

    public function __construct(
        private BlockCache $cache,
        private CacheState $cacheState,
        private UrlInterface $url
    ) {
    }

    public function aroundInjectNativeDimensionsToImgTag(
        InjectImageDimensions $subject,
        callable $proceed,
        string $imgTag,
        string $src
    ): string {
        if (!$this->cacheState->isEnabled(BlockCache::TYPE_IDENTIFIER)) {
            return $proceed($imgTag, $src);
        }

        $pathInMedia = substr($src, strlen($this->getMediaBaseUrl()));
        $key = self::KEY_PREFIX . sha1($this->getMediaBaseUrl() . "\n" . $pathInMedia);

        $answer = $this->memo[$pathInMedia] ?? $this->cache->load($key);
        if (is_string($answer) && $answer !== '') {
            $this->memo[$pathInMedia] = $answer;
            if ($answer === self::NOT_FOUND) {
                return '';
            }
            [$width, $height] = array_map('intval', explode('x', $answer, 2));
            if ($width > 0 && $height > 0) {
                $withoutDimensions = preg_replace('# (?:width|height)="[^"]*"#', '', $imgTag);

                return sprintf('<img width="%d" height="%d" %s', $width, $height, substr($withoutDimensions, 4));
            }
        }

        $result = $proceed($imgTag, $src);

        if ($result === '') {
            $answer = self::NOT_FOUND;
            $lifetime = self::LIFETIME_NOT_FOUND;
        } elseif (preg_match(self::PATTERN_HYVA_RESULT, $result, $m)) {
            $answer = $m[1] . 'x' . $m[2];
            $lifetime = self::LIFETIME_FOUND;
        } else {
            return $result; // Not a shape we can reproduce, so never cache it.
        }

        $this->memo[$pathInMedia] = $answer;
        $this->cache->save($answer, $key, [], $lifetime);

        return $result;
    }

    private function getMediaBaseUrl(): string
    {
        return $this->mediaBaseUrl ??= $this->url->getBaseUrl(['_type' => UrlInterface::URL_TYPE_MEDIA]);
    }
}

Declare it in etc/frontend/di.xml. Hyvä’s own caller is registered for the frontend area only, so that is where the plugin belongs.

<type name="Hyva\Theme\Model\InjectImageDimensions">
    <plugin name="cadence_pagebuilderimages_cache_native_image_dimensions"
            type="Cadence\PageBuilderImages\Plugin\Hyva\CacheNativeImageDimensions"/>
</type>
Need help with your Hyvä store?
If this problem is costing you orders or staff time, our senior Magento and Hyvä team can help you debug it and deploy a fix safely.
Contact us →

3. Design choices worth knowing

  • **The entries live in the block_html cache type.** bin/magento cache:clean block_html and a full flush drop them, and with that cache type turned off the plugin steps aside.
  • A found size is kept for 30 days, a “not found” answer for one day. An image replaced at the same path keeps its old dimensions until the entry expires or the cache is cleaned. A file that did not exist yet is picked up the next day at the latest.
  • We store Hyvä’s answer instead of reading Magento’s media gallery table. That table holds the size of the original upload, while content often points at a generated rendition with different dimensions. Using the table would have changed the boxes on the page.

4. Verify

Before deploying, we ran the plugin against all 82 flagged images of the slow page, using production data. The tags were identical to Hyvä’s output, byte for byte, on both a cache miss and a cache hit. The 82 lookups took 10.9 seconds without the cache and 3 milliseconds with it.

On your own store, time an uncached render of the heaviest page before and after (flush the page cache, then request the page twice with the block_html cache warm the second time), and compare the <img> tags in the two responses.

Notes before you ship this

  • This is not fixed by upgrading yet. As of December 2026, injectNativeDimensionsToImgTag() is unchanged in the newest theme module releases, 1.4.11 and 1.5.5.
  • The same pattern is worth looking for elsewhere. Code that opens a media file during a page render is cheap on local disk and expensive on remote storage, and it rarely shows up until the content team builds a page with eighty images on it.
  • A CDN or image service in front of the bucket does not help here. The download happens on the server while it builds the HTML, not in the visitor’s browser.
Install our webapp on your iPhone! Tap and then Add to homescreen.
Share This