yireo / Yireo_LinkPreload

Magento 2 extension to set HTTP Link headers for primary resources to allow for HTTP/2 Server Push
Open Software License 3.0
79 stars 18 forks source link

Added preloads are not used when pagecache is enabled #38

Open MichielVrins opened 2 years ago

MichielVrins commented 2 years ago

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.

Quazz commented 1 year ago

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

jissereitsma commented 1 year ago

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?

Morgy93 commented 8 months ago

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.

jissereitsma commented 8 months ago

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.

Morgy93 commented 8 months ago

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 commented 5 months ago

@jissereitsma Any insights for my previous comment?