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

> Discover how the OpenMage CSS/JS Minify module uses the http_response_send_before event to process responses. Optimize your site's performance by minifying assets before they reach the browser.

- Repository: [Fabrizio Balliano/openmage-cssjs-minify](https://github.com/fballiano/openmage-cssjs-minify)
- Tags: internals
- Published: 2026-03-01

---

**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`](https://github.com/fballiano/openmage-cssjs-minify/blob/main/config.xml)

The module registers its observer in the frontend scope of the Magento configuration. In [`app/code/community/Fballiano/CssjsMinify/etc/config.xml`](https://github.com/fballiano/openmage-cssjs-minify/blob/main/app/code/community/Fballiano/CssjsMinify/etc/config.xml), the `<events>` node maps the `http_response_send_before` event to the observer class:

```xml
<!-- 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`](https://github.com/fballiano/openmage-cssjs-minify/blob/main/Observer.php)

The observer class located at [`app/code/community/Fballiano/CssjsMinify/Model/Observer.php`](https://github.com/fballiano/openmage-cssjs-minify/blob/main/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:

```php
// 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

- The **OpenMage CSS/JS Minify module** hooks into Magento's **`http_response_send_before`** frontend event to intercept responses.
- Event registration occurs in [`app/code/community/Fballiano/CssjsMinify/etc/config.xml`](https://github.com/fballiano/openmage-cssjs-minify/blob/main/app/code/community/Fballiano/CssjsMinify/etc/config.xml), mapping the event to `Fballiano_CssjsMinify_Model_Observer`.
- The `httpResponseSendBefore` method in [`app/code/community/Fballiano/CssjsMinify/Model/Observer.php`](https://github.com/fballiano/openmage-cssjs-minify/blob/main/app/code/community/Fballiano/CssjsMinify/Model/Observer.php) performs the actual HTML rewriting and asset minification.
- This approach allows transparent optimization without modifying theme files or core code.

## 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`](https://github.com/fballiano/openmage-cssjs-minify/blob/main/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`](https://github.com/fballiano/openmage-cssjs-minify/blob/main/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`](https://github.com/fballiano/openmage-cssjs-minify/blob/main/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.