# How the OpenMage CSS JS Minify Module Integrates with and Modifies the HTML Response

> Learn how the OpenMage CSS JS Minify module intercepts HTML output, parses markup, and minifies JS/CSS assets with hashed URLs before sending the response.

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

---

**The fballiano/openmage-cssjs-minify module intercepts the final HTML output by observing the `http_response_send_before` event, then parses the markup to minify JavaScript and CSS assets and rewrite their URLs to hashed cache-busting paths before the response reaches the browser.**

The **CssjsMinify** extension for OpenMage (Magento 1) provides a zero-configuration approach to front-end optimization. According to the source code in the `fballiano/openmage-cssjs-minify` repository, the module leverages Magento’s event-driven architecture to transform the HTML response body after the full page has been rendered but before it is transmitted to the client.

## Event-Driven Response Interception

The integration begins with declarative event registration. 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 module subscribes to the `http_response_send_before` event—the final hook in Magento’s request dispatch cycle.

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

```

When Magento dispatches the response, it instantiates `Fballiano_CssjsMinify_Model_Observer` and invokes `httpResponseSendBefore`, passing a `Varien_Event_Observer` object containing the `Mage_Core_Controller_Response_Http` instance.

## The Observer Logic and Response Body Extraction

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 observer extracts the response object and retrieves the full HTML markup using `$response->getBody()`.

```php
// app/code/community/Fballiano/CssjsMinify/Model/Observer.php
public function httpResponseSendBefore(Varien_Event_Observer $observer)
{
    $response = $observer->getResponse();
    $html = $response->getBody();
    
    // Processing logic follows...
}

```

The module then prepares the caching infrastructure. It resolves the base directory (checking for `MAHO_PUBLIC_DIR` or falling back to `Mage::getBaseDir()`), ensures the `media/fbminify/` directory exists, and initializes the minification engine.

### Processing JavaScript Assets

For JavaScript minification, the observer applies a regex pattern to capture all `<script>` tags containing a `src` attribute ending in `.js`.

```php
// Regex to identify external JS files
preg_match_all('/<script[^>]+src=["\']([^"\']+\.js)["\']/', $html, $matches);

```

For each matched file, the module:
1. **Resolves the physical path** and reads the file modification time.
2. **Generates a unique hash** using `md5($path) . "-$time.js"` to create a cache-busting filename.
3. **Checks for existing minified copies** in `media/fbminify/`.
4. **Applies minification** using `MatthiasMullie\Minify\JS`, unless the filename already contains "min" or "pack" (indicating pre-minified sources).
5. **Rewrites the HTML** to replace the original `src` URL with the hashed path in `/media/fbminify/`.

### Processing CSS Stylesheets

The CSS workflow mirrors the JavaScript implementation. The observer searches for `<link>` tags with `href` attributes pointing to `.css` files.

```php
// Regex to identify external CSS files
preg_match_all('/<link[^>]+href=["\']([^"\']+\.css)["\']/', $html, $matches);

```

Using `MatthiasMullie\Minify\CSS`, the module minifies each stylesheet, generates hashed filenames based on the file path and modification time, and updates the HTML to reference the optimized assets in `media/fbminify/`.

### Finalizing the Modified Response

After processing both JavaScript and CSS assets, the observer writes the transformed HTML back to the response object.

```php
// Write the mutated HTML back to the response
$response->setBody($html);

```

At this point, Magento sends the modified markup to the browser, containing references to minified, cache-busted assets rather than the original source files.

## Maintenance and Cleanup

The module includes a housekeeping mechanism to prevent the `media/fbminify/` directory from growing indefinitely. In [`config.xml`](https://github.com/fballiano/openmage-cssjs-minify/blob/main/config.xml), a daily cron job is registered:

```xml
<crontab>
    <jobs>
        <fballiano_cssjsminify_daily_cron>
            <schedule>
                <cron_expr>0 0 * * *</cron_expr>
            </schedule>
            <run>
                <model>fballiano_cssjsminify/observer::dailyCron</model>
            </run>
        </fballiano_cssjsminify_daily_cron>
    </jobs>
</crontab>

```

The `dailyCron` method in [`Observer.php`](https://github.com/fballiano/openmage-cssjs-minify/blob/main/Observer.php) scans the minified file directory and removes older duplicate files, ensuring the cache remains lean while preserving recently generated assets.

## Summary

- **Event interception**: The module registers an observer for `http_response_send_before` 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) to capture the final HTML output.
- **Response body extraction**: The `httpResponseSendBefore` method in [`Observer.php`](https://github.com/fballiano/openmage-cssjs-minify/blob/main/Observer.php) retrieves the full HTML using `$response->getBody()`.
- **Asset minification**: JavaScript files are processed with `MatthiasMullie\Minify\JS` and CSS with `MatthiasMullie\Minify\CSS`, with logic to skip already-minified sources.
- **URL rewriting**: Original asset URLs are replaced with hashed, cache-busted paths pointing to `media/fbminify/`, ensuring browsers load optimized files.
- **Response mutation**: The modified HTML is written back to the response object via `$response->setBody()` before Magento sends it to the client.
- **Automated cleanup**: A daily cron job removes stale minified files to prevent storage bloat.

## Frequently Asked Questions

### How does the CssjsMinify module intercept the HTML response without modifying core files?

The module uses Magento’s event-observer pattern. By declaring an observer for the `http_response_send_before` event 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), Magento automatically invokes the `httpResponseSendBefore` method in [`Observer.php`](https://github.com/fballiano/openmage-cssjs-minify/blob/main/Observer.php) immediately before transmitting the response. This approach requires no core file edits and remains compatible with standard Magento and OpenMage deployments.

### What happens to JavaScript and CSS files that are already minified?

The observer includes logic to detect pre-minified assets. If a filename contains the substrings "min" or "pack", the module assumes the file is already optimized and copies it directly to `media/fbminify/` without running `MatthiasMullie\Minify\JS` or `MatthiasMullie\Minify\CSS`. This prevents double-processing and preserves vendor-provided minified libraries.

### How does the module ensure browsers load the latest versions of minified assets?

The module implements automatic cache busting by generating filenames using an MD5 hash of the file path combined with the file modification timestamp (`md5($path) . "-$time.js"`). When a source file changes, its modification time updates, producing a new hashed filename in `media/fbminify/`. The HTML is rewritten to reference this new URL, forcing browsers to bypass cached versions and request the updated asset.

### Where are the minified files stored, and how is storage managed?

Minified assets are written to the `media/fbminify/` directory within the Magento base directory. To prevent unlimited growth, the module registers a daily cron job (`dailyCron`) that scans this directory and removes older duplicate files. This housekeeping ensures the cache remains lean while preserving recently generated minified assets for active pages.