Search by

yireo / magento2-serverpush

yireo

This package is abandoned and no longer maintained. The author suggests using the yireo/magento2-linkpreload package instead.

Magento 2 extension to set HTTP Link headers for primary resources to allow for HTTP/2 preloading

Package info

github.com/yireo/Yireo_LinkPreload

Homepage

Type:magento2-module

pkg:composer/yireo/magento2-serverpush

Fund package maintenance!

yireo

www.paypal.me/yireo

Statistics

Installs: 6 260

Dependents: 0

Suggesters: 0

Stars: 93

Open Issues: 0

1.5.1 2026-12-02 15:13 UTC

README

Magento version PHP version License Latest Version

Magento 2 extension to set HTTP Link headers for primary resources to allow for HTTP/2 preloading.

Note: This module was formally known as Yireo_ServerPush but the name gave a wrong impression (referring to the HTTP/2 PUSH feature). Hence this module was renamed.

Installation

First of all, make sure your Magento 2 hosting environment supports HTTP/2. If not, this extension will not aid anything.

To install this extension, use the following command:

composer require yireo/magento2-linkpreload

Afterwards, enable this module, run the setup scripts and flush the cache:

bin/magento module:enable Yireo_LinkPreload
bin/magento setup:upgrade
bin/magento cache:flush

Next, head over to the Store Configuration Advanced > System > Yireo LinkPreload to configure things.

Note that this module is compatible with both Luma and Hyvä.

Usage

Once the extension is installed, you will still need to enable it via Store > Configuration > System > Yireo LinkPreload.

Once enabled, you should be able to see new Link: headers by debugging the HTTP headers of the HTML source of your Magento pages. This allows your browser to request for some initial CSS and JavaScript, once the HTML document has been received.

Only HTML documents (a text/html response starting with <!doctype html>) are processed. To keep the Link header within the limits of proxies like nginx and Varnish, the options Maximum Links (default 30) and Maximum Header Length (default 4096 bytes) limit the header. Use 0 for unlimited. Resources from the XML layout get priority, followed by stylesheets, scripts and images.

Large headers and 502 errors

The Link header is not the only big header that Magento sends: CSP, cookies and cache headers add up as well. If your webserver responds with a 502 and logs upstream sent too big header while reading response header from upstream, increase the header buffers of the webserver instead of only lowering the limits of this extension.

For nginx with PHP-FPM:

fastcgi_buffer_size 32k;
fastcgi_buffers 16 16k;

For nginx as a reverse proxy (for instance in front of Varnish):

proxy_buffer_size 32k;
proxy_buffers 16 16k;

For Varnish, raise the runtime parameters http_resp_hdr_len (maximum length of a single header, default 8k) and http_resp_size (all headers together, default 32k), for instance with varnishd -p http_resp_hdr_len=16k -p http_resp_size=64k.

To find out whether resources are left out of the Link header because of Maximum Links or Maximum Header Length, enable the option Log Dropped Links. A single line per request is then logged in var/log/system.log, mentioning the number of dropped links, the limit that was hit and the request path (without query string). Only enable this for debugging.

Optional XML layout configuration

Optionally, you can add additional links via the XML layout, which is mostly useful for JavaScript loaded via RequireJS, or webfonts loaded via css:

<referenceBlock name="link-preload">
    <arguments>
        <argument name="scripts" xsi:type="array">
            <item name="my-script-name" xsi:type="string">my-script.js</item>
        </argument>
        <argument name="fonts" xsi:type="array">
            <item name="my-font-name" xsi:type="string">my-font.woff2</item>
        </argument>
        <argument name="styles" xsi:type="array">
            <item name="my-style-name" xsi:type="string">my-style.css</item>
        </argument>
        <argument name="images" xsi:type="array">
            <item name="my-image-name" xsi:type="string">my-image.png</item>
        </argument>
    </arguments>
</referenceBlock>

Please note that RequireJS bundling has a similar effect and should be the preferred way to go.

Removing values

Sometimes a resource is automatically preloaded because it is referenced in the HTML document, while you actually do not want it to be preloaded. You can skip such a resource by setting its value in the XML layout to false:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="link-preload">
            <arguments>
                <argument name="styles" xsi:type="array">
                    <item name="Loki_Flatpickr/css/flatpickr.min.css" xsi:type="boolean">false</item>
                </argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

The item name is matched against the end of the preloaded URL, so the name does not need to include the theme or locale.

Responsive and print stylesheets

When a stylesheet has a media attribute, its media query is added to the preload as well. The browser then only preloads the stylesheet when the media query matches. For instance, the Luma stylesheet css/styles-l.css (with media="screen and (min-width: 768px)") is not preloaded on mobile devices. Stylesheets with media="print" are not preloaded at all.

If you want to stop preloading a stylesheet completely (for instance css/styles-l.css in a mobile-only theme), remove it via the XML layout of that theme:

<referenceBlock name="link-preload">
    <arguments>
        <argument name="styles" xsi:type="array">
            <item name="css/styles-l.css" xsi:type="boolean">false</item>
        </argument>
    </arguments>
</referenceBlock>

Backgrounds

With HTTP/2 a lot of cool things have been added to the web. However, for things to work really optimal a HTML document could mark additional resources (like CSS or JavaScript) with Link headers in the HTTP response. This extension simply adds these Link headers to the HTTP response, allowing your Magento 2 site to be loaded faster under HTTP/2.

About the browser cache

Please note that this extension does not implement a PUSH initiated from the server, which would bypass the browser cache. Instead, it implements a method that respects the browser cache, which resembles more of a pull than a push.

Use a cookie?

This module also ships with a setting Use Cookie. Keep it disabled unless you know what you are doing. Theoretically, there could be a reverse-proxy between the browser and Magento that doesn't forward Link header but instead uses it to initiate a real PUSH, so that it bypasses the browser cache. This depends entirely on the configuration of this reverse-proxy. But in this specific case, the cookie feature needs to be enabled.

When the cookie linkpreload is set to 1, the Link header of this module is not sent. Note that on pages rendered via the XML layout (and therefore on pages served from the Full Page Cache), the <link rel="preload"> tags in the HTML are still there: Only the Link header is stripped.

Full Page Cache

The Link header and the <link rel="preload"> tags are added as soon as Magento renders the XML layout into the response, which happens before the built-in Full Page Cache saves the page. Because of this, a page that is served from the built-in Full Page Cache contains the same preloads (including the resources added via the XML layout) as the uncached page, without the HTML being parsed again. For the same reason, preloads are also added to XML layout pages that are rendered during an AJAX request: The built-in Full Page Cache does not distinguish between AJAX and regular requests.

After installing or upgrading this module, flush the Full Page Cache, so that pages that were cached without preloads are regenerated:

bin/magento cache:clean full_page

Cached pages keep their preloads. Therefore, after changing the settings of this module (Enabled, Skip Images, Maximum Links, Maximum Header Length or critical CSS), flush the Full Page Cache as well.

With Varnish (or another reverse proxy) in front of Magento, the cookie linkpreload is only applied to responses that actually reach Magento. Pages that are served from the cache of the reverse proxy are not filtered per request.

Notes

RequireJS is not covered

This extension does not support scripts that are loaded via RequireJS. RequireJS itself (the core libraries) is served using a Link header though. If you want to optimize RequireJS sources, make sure to look into bundling instead.

PWA compatibility

This frontend extension will be completely replaced by React or Vue functionality, and therefore, this extension will not be made compatible with PWA.

Current status

Static Tests Unit Tests Integration Tests Playwright DI Compilation