Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added preloads are not used when pagecache is enabled #38

Open
MichielVrins opened this issue May 31, 2022 · 6 comments
Open

Added preloads are not used when pagecache is enabled #38

MichielVrins opened this issue May 31, 2022 · 6 comments

Comments

@MichielVrins
Copy link

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
Copy link
Contributor

Quazz commented Apr 17, 2023

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
Copy link
Collaborator

jissereitsma commented May 26, 2023

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
Copy link

Morgy93 commented Mar 1, 2024

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
Copy link
Collaborator

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
Copy link

Morgy93 commented Mar 12, 2024

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
Copy link

Morgy93 commented Jun 6, 2024

@jissereitsma Any insights for my previous comment?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants