Yireo_LinkPreload icon indicating copy to clipboard operation
Yireo_LinkPreload copied to clipboard

Added preloads are not used when pagecache is enabled

Open MichielVrins opened this issue 2 years ago • 6 comments

The preload tags and http header links are added onto the http response (presumably) after magento saves it into page cache. With page cache enabled the first load will have all the preloads in the http header and in the

but on the second load, when the page is cached in full_page cache, some of these preloads disappear.

A little bit of debugging brought me to the following piece of code.

    public function parse(HttpResponse $response)
    {
        $this->addLinkHeadersFromResponse($response);
        $this->addLinkHeadersFromLayout();
        $this->processHeaders($response);
        $this->processBody($response);
        $this->reset();
    }

Here it attempts to add the links from the layout by retrieving the 'link-preload' block

$block = $this->layout->getBlock('link-preload');

This is however not possible, since the layout doesn't get generated since we're loading a page that is in full_page cache. This results in the added layout items not being added.

I believe a way of solving this is by caching the links in magento somewhere (specific to a page) and then using that cache to build the preload links when a page is in full_page cache.

MichielVrins avatar May 31 '22 10:05 MichielVrins

Best option is likely to move the responseplugin to hook in on Magento\Framework\View\Result\Layout instead (with high sortorder so it goes last), this one will get cached by Pagecache

Quazz avatar Apr 17 '23 10:04 Quazz

Sorry for not having responded for quite some time. I've tested this behaviour back then, but could not find any issues. And I checked it again, but am not able to duplicate. In short, if I'm enabling FPC, reload the page so that the X-Magento-Cache-Debug header shows a HIT, then I can also see the Link headers added through this extension. This is under Magento 2.4.3 and 2.4.6 (in both Developer Mode and Production Mode).

Could you share your details and environment specifics?

jissereitsma avatar May 26 '23 07:05 jissereitsma

I don't think it's about the link header data because that is present all the time. But the HTML preload link is affected by this issue described here.

So, clearing the full_page cache and loading the page:

image

After reloading the page:

image

Those are added like:

<referenceBlock name="link-preload">
            <arguments>
                <argument name="fonts" xsi:type="array">
                    <item name="foo" xsi:type="string">fonts/foo.woff2</item>
... and so on ...
                </argument>
            </arguments>
        </referenceBlock>

In the custom-theme/Magento_Theme/layout/hyva_default.xml.

Morgy93 avatar Mar 01 '24 16:03 Morgy93

Maybe to comment on this, the links that @Morgy93 are not added by this extension, but are added by the Magento core via \Magento\Framework\View\Page\Config\Renderer::renderAssets. And indeed, I have tested for this, the assets are added by Magento but then removed with FPC enabled. This seems to be a core bug.

jissereitsma avatar Mar 04 '24 10:03 jissereitsma

Maybe to comment on this, the links that @Morgy93 are not added by this extension, but are added by the Magento core via \Magento\Framework\View\Page\Config\Renderer::renderAssets. And indeed, I have tested for this, the assets are added by Magento but then removed with FPC enabled. This seems to be a core bug.

Are you sure?

Because if I disable $response->setBody($body); via https://github.com/yireo/Yireo_LinkPreload/blob/1.4.22/Link/LinkParser.php#L120 there is no output at all. So, for me it seems that it is added by this extension.

Just the $this->links is different with and without cache. Maybe that's the core bug you mentioned?

Morgy93 avatar Mar 12 '24 08:03 Morgy93

@jissereitsma Any insights for my previous comment?

Morgy93 avatar Jun 06 '24 06:06 Morgy93