Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Html Minify Laravel Package

fitztrev/laravel-html-minify

Laravel HTML Minify is middleware that automatically compresses HTML responses by removing extra whitespace, comments, and optional attributes, reducing page size and improving load times with simple installation and configuration.

View on GitHub
Deep Wiki
Context7

Getting Started

Install the package with Composer:

composer require fitztrev/laravel-html-minify

Register the service provider in app/config/app.php:

'providers' => array(
    // ...
    'Fitztrev\LaravelHtmlMinify\LaravelHtmlMinifyServiceProvider',
),

There is no config file to publish. The provider registers a minify macro on Laravel's Response class, so the package has no runtime configuration surface of its own.

The first use case is a single route or controller action:

Route::get('/', function () {
    return Response::make(View::make('welcome'))->minify();
});

Or in a controller:

public function index()
{
    return Response::make(View::make('dashboard'))->minify();
}

Where to look first:

  1. The README for the exact macro name and install steps.
  2. LaravelHtmlMinifyServiceProvider to confirm what is registered and whether the minifier is resolved from the container.
  3. The minifier class itself — read the regex rules. They define exactly what gets stripped and are the best predictor of which pages will break.

One important caveat up front: the package describes itself as targeting Laravel 4. Response macros exist in later Laravel versions, but the provider may depend on Laravel 4 APIs. Verify it boots before adopting it on a modern application.

Implementation Patterns

Controller-level minification. The most explicit pattern is to call ->minify() at the return site:

public function show($id)
{
    return Response::make(View::make('posts.show', compact('post')))->minify();
}

This is easy to audit and keeps minification opt-in per route.

Base controller helper. Rather than repeating Response::make(View::make(...))->minify(), centralize it:

protected function minified($view, array $data = [], $status = 200)
{
    $response = Response::make(View::make($view, $data), $status);

    return App::environment('production') ? $response->minify() : $response;
}

This gives you environment gating and a single place to disable minification when debugging.

Global after filter. For site-wide minification, use Laravel 4's after filter:

App::after(function ($request, $response) {
    if (! App::environment('production')) {
        return;
    }

    if (! $response instanceof Illuminate\Http\Response) {
        return;
    }

    if (strpos($response->headers->get('Content-Type'), 'text/html') !== 0) {
        return;
    }

    $response->minify();
});

The content-type guard is essential — without it you will attempt to minify JSON, XML, file downloads, and redirect bodies.

Environment strategy. Minify in production, leave local and testing output readable. Raw HTML in local makes view debugging and assertSee failures far easier to diagnose.

Cache-friendly placement. Minify after your response caching layer, or cache the already-minified string. Running a regex over a large page on every request is pure overhead if the page content rarely changes.

Testing pattern. Assert on text content, not on markup formatting:

$response = $this->call('GET', '/');
$this->assertResponseOk();
$this->assertContains('Welcome to the dashboard', $response->getContent());

Because whitespace is collapsed, assertions that depend on indentation or blank lines will fail.

Combining with view data. View::make($view, $data) is evaluated before Response::make, so view composers, shared data, and Blade rendering behave normally. Minification happens only on the final response body.

Gotchas and Tips

Whitespace-sensitive elements break first. <pre>, <textarea>, and <code> blocks can lose meaningful whitespace. Any page showing code samples, formatted text, or user-authored HTML should be tested carefully, and possibly excluded.

Inline whitespace between elements matters. Collapsing </span> <span> into </span><span> changes rendering for inline elements. Aggressive regex minifiers frequently introduce subtle layout shifts on text-heavy pages.

Comments are removed. That includes HTML comments you may still rely on, such as IE conditional comments (<!--[if IE]>). Check whether the minifier preserves them; if not, do not apply it to templates that need them.

Inline scripts and styles are risky. A naive regex minifier can corrupt JavaScript containing // comments, < comparisons, or template strings, and CSS with unusual formatting. Prefer external assets, or verify inline snippets after minification.

Never minify non-HTML responses. Response::json() returns a response that still inherits the macro. Calling ->minify() on JSON will corrupt the payload. Guard by content type, not by assuming the response is a view.

Performance overhead is real. Every minified response pays the regex cost. For high-traffic pages, minify once and cache, or accept the tradeoff knowingly.

Gzip matters more. Minification reduces bytes, but HTTP compression typically yields a larger win. Enable gzip/compression at the server or via middleware; treat this package as a complement, not a replacement.

Content-Length and headers. If you set Content-Length manually before minifying, update or remove it afterward. Most Laravel responses do not set it explicitly, but proxy or custom middleware might.

Debugging technique. Log $response->getContent() before and after minification, or diff the two in a local request. When a page renders strangely in production but fine locally, minification is a prime suspect.

Laravel version compatibility. The package is written for Laravel 4. On Laravel 5+, response macros still exist, but the service provider registration and response lifecycle differ. If you migrate, the durable approach is to reimplement the same behavior as middleware that transforms the response content on the way out.

Extension points. Check whether the provider binds the minifier in the container. If it does, you can swap in your own implementation with a targeted regex or a more robust parser. If it does not, plan to fork the minifier class — which is small — rather than fight the package.

Redirects and streamed responses. Skip them. They either have empty bodies or bodies that should not be transformed.

Character encoding. Verify that non-ASCII content survives unchanged. Simple regex minifiers are usually byte-safe, but test real content with accented characters, emoji, or non-Latin scripts before trusting it in production.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
tequilarapido/trackit
gregsphotography/site-builder-filament
cabaoctavio/laravel-query-log
gregsphotography/site-builder-components
io-digital/clickatell
snowsoft/laravel-admin
oxygen/auth
relicsoftware/barakah-payment-gateway
tobya/work-with-composer
equipc/laravel-email-validator
equipc/laravel-email-database-log
adedsec/nova-translated-labels
wazza/laravel-db-encryption
hokny/laravel-generator
vonsogt/backup
jengo/queues
jeremykenedy/laravel-observability
coleus/frame
thtg88/journalism
filagora/filagora-ai-forums