How the OpenMage CSS/JS Minify Module Uses the `http_response_send_before` Event

The OpenMage CSS/JS Minify module observes Magento's http_response_send_before frontend event to intercept HTML responses and replace asset URLs with minified versions before they reach the browser.

The OpenMage CSS/JS Minify module (available in the fballiano/openmage-cssjs-minify repository) transparently optimizes frontend performance by hooking into Magento's event-driven architecture. It automatically processes HTML responses to minify JavaScript and CSS assets without requiring manual template modifications.

The http_response_send_before Event: Frontend Hook for Response Modification

Magento dispatches the http_response_send_before event immediately before the HTTP response is sent to the client. This event provides the final opportunity to modify response content, making it the ideal hook for the OpenMage CSS/JS Minify module to rewrite HTML and swap asset URLs.

The event carries the response object, allowing observers to retrieve the raw HTML body, process it, and set the modified content back into the response before transmission.

Observer Configuration in config.xml

The module registers its observer in the frontend scope of the Magento configuration. In app/code/community/Fballiano/CssjsMinify/etc/config.xml, the <events> node maps the http_response_send_before event to the observer class:

<!-- app/code/community/Fballiano/CssjsMinify/etc/config.xml -->
<frontend>
    <events>
        <http_response_send_before>
            <observers>
                <fballiano_cssjsminify>
                    <class>fballiano_cssjsminify/observer</class>
                    <method>httpResponseSendBefore</method>
                </fballiano_cssjsminify>
            </observers>
        </http_response_send_before>
    </events>
</frontend>

This configuration ensures that whenever Magento dispatches http_response_send_before on the frontend, the method httpResponseSendBefore in Fballiano_CssjsMinify_Model_Observer executes.

Response Processing Logic in Observer.php

The observer class located at app/code/community/Fballiano/CssjsMinify/Model/Observer.php contains the core minification logic. The httpResponseSendBefore method retrieves the response object, extracts the HTML body, and processes it to minify assets:

// app/code/community/Fballiano/CssjsMinify/Model/Observer.php
public function httpResponseSendBefore(Varien_Event_Observer $observer): void
{
    $response = $observer->getResponse();   // the HTTP response object
    $html     = $response->getBody();       // raw HTML output
    
    // Scan for <script src="..."> and <link href="..."> tags
    // Minify referenced JavaScript/CSS files
    // Replace original URLs with minified versions in $html
    
    $response->setBody($html);              // replace body before sending
}

The method uses regular expressions to locate <script src="…"> and <link href="…"> tags, minifies the referenced files, and updates the HTML to point to the optimized assets. This transformation occurs transparently, ensuring the browser receives minified resources without requiring changes to theme templates.

Summary

Frequently Asked Questions

What specific event does the OpenMage CSS/JS Minify module use to process responses?

The module observes the http_response_send_before event in Magento's frontend area. This event fires immediately before the HTTP response is transmitted to the browser, providing the final opportunity to modify response content.

Where is the observer for this event registered?

The observer is registered in app/code/community/Fballiano/CssjsMinify/etc/config.xml within the <frontend><events> node. The configuration maps the http_response_send_before event to the fballiano_cssjsminify/observer class and its httpResponseSendBefore method.

How does the module identify which assets to minify?

Inside app/code/community/Fballiano/CssjsMinify/Model/Observer.php, the httpResponseSendBefore method retrieves the HTML response body and uses regular expressions to scan for <script src="…"> and <link href="…"> tags. It extracts the referenced file paths, minifies the physical files, and updates the HTML to reference the minified versions.

Does this module affect admin panel (backend) responses?

No, the observer is specifically scoped to the frontend area via the <frontend> tag in config.xml. The http_response_send_before event observer does not trigger for adminhtml requests, ensuring that backend performance and functionality remain unaffected by the minification process.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →